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
40 #include "oic_malloc.h"
41 #include "oic_string.h"
43 #include "cainterface.h"
48 #include "srmresourcestrings.h"
49 #include "doxmresource.h"
50 #include "pstatresource.h"
51 #include "credresource.h"
52 #include "aclresource.h"
53 #include "ownershiptransfermanager.h"
54 #include "securevirtualresourcetypes.h"
55 #include "oxmjustworks.h"
57 #include "pmutility.h"
58 #include "srmutility.h"
59 #include "provisioningdatabasemanager.h"
60 #include "oxmrandompin.h"
61 #include "ocpayload.h"
62 #include "payload_logging.h"
67 * Array to store the callbacks for each owner transfer method.
69 static OTMCallbackData_t g_OTMDatas[OIC_OXM_COUNT];
72 * Variable for storing provisioning tool's provisioning capabilities
73 * Must be in decreasing order of preference. More prefered method should
74 * have lower array index.
76 static OicSecDpom_t gProvisioningToolCapability[] = { SINGLE_SERVICE_CLIENT_DRIVEN };
79 * Number of supported provisioning methods
80 * current version supports only one.
82 static size_t gNumOfProvisioningMethodsPT = 1;
85 * Variables for pointing the OTMContext to be used in the DTLS handshake result callback.
87 static OTMContext_t* g_otmCtx = NULL;
90 * Function to select appropriate provisioning method.
92 * @param[in] supportedMethods Array of supported methods
93 * @param[in] numberOfMethods number of supported methods
94 * @param[out] selectedMethod Selected methods
95 * @return OC_STACK_OK on success
97 static OCStackResult SelectProvisioningMethod(const OicSecOxm_t *supportedMethods,
98 size_t numberOfMethods,
99 OicSecOxm_t *selectedMethod)
101 OIC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
103 if(numberOfMethods == 0 || !supportedMethods)
105 OIC_LOG(WARNING, TAG, "Could not find a supported OxM.");
106 return OC_STACK_ERROR;
109 *selectedMethod = supportedMethods[0];
110 for(size_t i = 0; i < numberOfMethods; i++)
112 if(*selectedMethod < supportedMethods[i])
114 *selectedMethod = supportedMethods[i];
122 * Function to select operation mode.This function will return most secure common operation mode.
124 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
125 * @param[out] selectedMode selected operation mode
126 * @return OC_STACK_OK on success
128 static void SelectOperationMode(const OCProvisionDev_t *selectedDeviceInfo,
129 OicSecDpom_t *selectedMode)
131 OIC_LOG(DEBUG, TAG, "IN SelectOperationMode");
136 while (i < gNumOfProvisioningMethodsPT && j < selectedDeviceInfo->pstat->smLen)
138 if (gProvisioningToolCapability[i] < selectedDeviceInfo->pstat->sm[j])
142 else if (selectedDeviceInfo->pstat->sm[j] < gProvisioningToolCapability[i])
146 else /* if gProvisioningToolCapability[i] == deviceSupportedMethods[j] */
148 *selectedMode = gProvisioningToolCapability[j];
152 OIC_LOG_V(DEBUG, TAG, "Selected Operation Mode = %d", *selectedMode);
154 OIC_LOG(DEBUG, TAG, "OUT SelectOperationMode");
158 * Function to start ownership transfer.
159 * This function will send the first request for provisioning,
160 * The next request message is sent from the response handler for this request.
162 * @param[in] ctx context value passed to callback from calling function.
163 * @param[in] selectedDevice selected device information to performing provisioning.
164 * @return OC_STACK_OK on success
166 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
169 * Function to update owner transfer mode
171 * @param[in] otmCtx Context value of ownership transfer.
172 * @return OC_STACK_OK on success
174 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx);
177 * Function to send request to resource to get its pstat resource information.
179 * @param[in] otmCtx Context value of ownership transfer.
180 * @return OC_STACK_OK on success
182 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx);
186 * Function to send uuid of owner device to new device.
187 * This function would update 'owner of doxm' as UUID for provisioning tool.
189 * @param[in] otmCtx Context value of ownership transfer.
190 * @return OC_STACK_OK on success
192 static OCStackResult PutOwnerUuid(OTMContext_t* otmCtx);
195 * Function to update the operation mode. As per the spec. Operation mode in client driven
196 * single service provisioning it will be updated to 0x3
198 * @param[in] otmCtx Context value of ownership transfer.
199 * @return OC_STACK_OK on success
201 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx);
204 * Function to update the owner credential to new device
206 * @param[in] otmCtx Context value of ownership transfer.
207 * @param[in] selectedOperationMode selected operation mode
208 * @return OC_STACK_OK on success
210 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx);
213 * Function to send ownerShip info.
214 * This function would update 'owned of doxm' as true.
216 * @param[in] otmCtx Context value of ownership transfer.
217 * @return OC_STACK_OK on success
219 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx);
222 * Function to update pstat when finalize provisioning.
223 * This function would update 'cm' as bx0011,1100 and 'tm' as bx0000,0000.
225 * @param[in] ctx context value passed to callback from calling function.
226 * @param[in] selectedDevice selected device information to performing provisioning.
227 * @return OC_STACK_OK on success
229 static OCStackResult PutProvisioningStatus(OTMContext_t* otmCtx);
231 static bool IsComplete(OTMContext_t* otmCtx)
233 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
235 if(OC_STACK_CONTINUE == otmCtx->ctxResultArray[i].res)
245 * Function to save the result of provisioning.
247 * @param[in,out] otmCtx Context value of ownership transfer.
248 * @param[in] res result of provisioning
250 static void SetResult(OTMContext_t* otmCtx, const OCStackResult res)
252 OIC_LOG_V(DEBUG, TAG, "IN SetResult : %d ", res);
256 OIC_LOG(WARNING, TAG, "OTMContext is NULL");
260 if(otmCtx->selectedDeviceInfo)
262 //Revert psk_info callback and new deivce uuid in case of random PIN OxM
263 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
265 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
267 OIC_LOG(WARNING, TAG, "Failed to revert is DTLS credential handler.");
269 OicUuid_t emptyUuid = { .id={0}};
270 SetUuidForRandomPinOxm(&emptyUuid);
273 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
275 if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
276 otmCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
278 otmCtx->ctxResultArray[i].res = res;
279 if(OC_STACK_OK != res)
281 otmCtx->ctxHasError = true;
288 //If all request is completed, invoke the user callback.
289 if(IsComplete(otmCtx))
291 otmCtx->ctxResultCallback(otmCtx->userCtx, otmCtx->ctxResultArraySize,
292 otmCtx->ctxResultArray, otmCtx->ctxHasError);
293 OICFree(otmCtx->ctxResultArray);
298 if(OC_STACK_OK != StartOwnershipTransfer(otmCtx,
299 otmCtx->selectedDeviceInfo->next))
301 OIC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
306 OIC_LOG(DEBUG, TAG, "OUT SetResult");
310 * Function to handle the handshake result in OTM.
311 * This function will be invoked after DTLS handshake
312 * @param endPoint [IN] The remote endpoint.
313 * @param errorInfo [IN] Error information from the endpoint.
316 void DTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
318 if(NULL != g_otmCtx && NULL != g_otmCtx->selectedDeviceInfo &&
319 NULL != endpoint && NULL != info)
321 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
322 endpoint->addr, endpoint->port, info->result);
324 OicSecDoxm_t* newDevDoxm = g_otmCtx->selectedDeviceInfo->doxm;
326 if(NULL != newDevDoxm)
328 OicUuid_t emptyUuid = {.id={0}};
330 //Make sure the address matches.
331 if(strncmp(g_otmCtx->selectedDeviceInfo->endpoint.addr,
333 sizeof(endpoint->addr)) == 0 &&
334 g_otmCtx->selectedDeviceInfo->securePort == endpoint->port)
336 OCStackResult res = OC_STACK_ERROR;
338 //If temporal secure sesstion established successfully
339 if(CA_STATUS_OK == info->result &&
340 false == newDevDoxm->owned &&
341 memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) == 0)
343 //Send request : PUT /oic/sec/doxm [{... , "devowner":"PT's UUID"}]
344 res = PutOwnerUuid(g_otmCtx);
345 if(OC_STACK_OK != res)
347 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
348 SetResult(g_otmCtx, res);
351 //In case of authentication failure
352 else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
354 //in case of error from owner credential
355 if(memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) != 0 &&
356 true == newDevDoxm->owned)
358 OIC_LOG(ERROR, TAG, "The owner credential may incorrect.");
360 if(OC_STACK_OK != RemoveCredential(&(newDevDoxm->deviceID)))
362 OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
364 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
366 //in case of error from wrong PIN, re-start the ownership transfer
367 else if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
369 OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
371 memcpy(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
372 newDevDoxm->owned = false;
373 g_otmCtx->attemptCnt++;
375 if(WRONG_PIN_MAX_ATTEMP > g_otmCtx->attemptCnt)
377 res = StartOwnershipTransfer(g_otmCtx, g_otmCtx->selectedDeviceInfo);
378 if(OC_STACK_OK != res)
380 SetResult(g_otmCtx, res);
381 OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
386 OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
387 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
392 OIC_LOG(ERROR, TAG, "Failed to establish secure session.");
393 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
402 * Function to save ownerPSK at provisioning tool end.
404 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
405 * @return OC_STACK_OK on success
407 static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
409 OIC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
411 OCStackResult res = OC_STACK_ERROR;
413 CAEndpoint_t endpoint;
414 memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
415 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
416 endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
417 endpoint.port = selectedDeviceInfo->securePort;
419 OicUuid_t ptDeviceID = {.id={0}};
420 if (OC_STACK_OK != GetDoxmDeviceID(&ptDeviceID))
422 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
426 uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
428 //Generating OwnerPSK
429 CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
430 (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
431 strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
432 ptDeviceID.id, sizeof(ptDeviceID.id),
433 selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
434 ownerPSK, OWNER_PSK_LENGTH_128);
436 if (CA_STATUS_OK == pskRet)
438 OIC_LOG(INFO, TAG,"ownerPSK dump:\n");
439 OIC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
440 //Generating new credential for provisioning tool
443 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
444 SYMMETRIC_PAIR_WISE_KEY, NULL,
445 ownerPSK, ownLen, &ptDeviceID);
446 VERIFY_NON_NULL(TAG, cred, ERROR);
448 res = AddCredential(cred);
449 if(res != OC_STACK_OK)
451 DeleteCredList(cred);
457 OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
460 OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
466 * Callback handler for OwnerShipTransferModeHandler API.
468 * @param[in] ctx ctx value passed to callback from calling function.
469 * @param[in] UNUSED handle to an invocation
470 * @param[in] clientResponse Response from queries to remote servers.
471 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
472 * and OC_STACK_KEEP_TRANSACTION to keep it.
474 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
475 OCClientResponse *clientResponse)
477 OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
479 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
480 VERIFY_NON_NULL(TAG, ctx, WARNING);
482 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
484 if(clientResponse->result == OC_STACK_OK)
486 OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
487 //Send request : GET /oic/sec/pstat
488 OCStackResult res = GetProvisioningStatusResource(otmCtx);
489 if(OC_STACK_OK != res)
491 OIC_LOG(WARNING, TAG, "Failed to get pstat information");
492 SetResult(otmCtx, res);
497 OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
498 clientResponse->result);
499 SetResult(otmCtx, clientResponse->result);
502 OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
505 return OC_STACK_DELETE_TRANSACTION;
509 * Callback handler for ProvisioningStatusResouceHandler API.
511 * @param[in] ctx ctx value passed to callback from calling function.
512 * @param[in] UNUSED handle to an invocation
513 * @param[in] clientResponse Response from queries to remote servers.
514 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
515 * and OC_STACK_KEEP_TRANSACTION to keep it.
517 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
518 OCClientResponse *clientResponse)
520 OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
522 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
523 VERIFY_NON_NULL(TAG, ctx, WARNING);
525 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
527 if (OC_STACK_OK == clientResponse->result)
529 if (NULL == clientResponse->payload)
531 OIC_LOG(INFO, TAG, "Skiping Null payload");
532 SetResult(otmCtx, OC_STACK_ERROR);
533 return OC_STACK_DELETE_TRANSACTION;
536 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
538 OIC_LOG(INFO, TAG, "Unknown payload type");
539 SetResult(otmCtx, OC_STACK_ERROR);
540 return OC_STACK_DELETE_TRANSACTION;
543 OicSecPstat_t* pstat = NULL;
544 OCStackResult result = CBORPayloadToPstat(
545 ((OCSecurityPayload*)clientResponse->payload)->securityData1,
547 if(NULL == pstat && result != OC_STACK_OK)
549 OIC_LOG(ERROR, TAG, "Error while converting cbor to pstat.");
550 SetResult(otmCtx, OC_STACK_ERROR);
551 return OC_STACK_DELETE_TRANSACTION;
553 otmCtx->selectedDeviceInfo->pstat = pstat;
555 //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
556 SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
558 //Send request : PUT /oic/sec/pstat [{"om":"bx11", .. }]
559 OCStackResult res = PutUpdateOperationMode(otmCtx);
560 if (OC_STACK_OK != res)
562 OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
563 SetResult(otmCtx, res);
568 OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
569 clientResponse->result);
570 SetResult(otmCtx, clientResponse->result);
573 OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
575 return OC_STACK_DELETE_TRANSACTION;
579 * Response handler for update owner uuid request.
581 * @param[in] ctx ctx value passed to callback from calling function.
582 * @param[in] UNUSED handle to an invocation
583 * @param[in] clientResponse Response from queries to remote servers.
584 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
585 * and OC_STACK_KEEP_TRANSACTION to keep it.
587 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
588 OCClientResponse *clientResponse)
590 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
591 VERIFY_NON_NULL(TAG, ctx, WARNING);
593 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
595 OCStackResult res = OC_STACK_OK;
596 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
598 if(OC_STACK_OK == clientResponse->result)
600 if(otmCtx && otmCtx->selectedDeviceInfo)
602 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
603 if(OC_STACK_OK != res)
605 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
606 SetResult(otmCtx, res);
607 return OC_STACK_DELETE_TRANSACTION;
610 //PUT owner credential to new device according to security spec B.
611 res = PutOwnerCredential(otmCtx);
612 if(OC_STACK_OK != res)
615 "OwnerUuidUpdateHandler:Failed to send PUT request for onwer credential");
616 SetResult(otmCtx, res);
617 return OC_STACK_DELETE_TRANSACTION;
623 res = clientResponse->result;
624 OIC_LOG_V(ERROR, TAG, "OwnerUuidHandler : Unexpected result %d", res);
625 SetResult(otmCtx, res);
628 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
631 return OC_STACK_DELETE_TRANSACTION;
635 * Response handler for update operation mode.
637 * @param[in] ctx ctx value passed to callback from calling function.
638 * @param[in] UNUSED handle to an invocation
639 * @param[in] clientResponse Response from queries to remote servers.
640 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
641 * and OC_STACK_KEEP_TRANSACTION to keep it.
643 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
644 OCClientResponse *clientResponse)
646 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
648 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
649 VERIFY_NON_NULL(TAG, ctx, WARNING);
651 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
653 if (OC_STACK_OK == clientResponse->result)
655 OCStackResult res = OC_STACK_ERROR;
656 OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
658 //Load secret for temporal secure session.
659 if(g_OTMDatas[selOxm].loadSecretCB)
661 res = g_OTMDatas[selOxm].loadSecretCB(otmCtx);
662 if(OC_STACK_OK != res)
664 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
665 SetResult(otmCtx, res);
666 return OC_STACK_DELETE_TRANSACTION;
670 //It will be used in handshake event handler
673 //Try DTLS handshake to generate secure session
674 if(g_OTMDatas[selOxm].createSecureSessionCB)
676 res = g_OTMDatas[selOxm].createSecureSessionCB(otmCtx);
677 if(OC_STACK_OK != res)
679 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
680 SetResult(otmCtx, res);
681 return OC_STACK_DELETE_TRANSACTION;
687 OIC_LOG(ERROR, TAG, "Error while update operation mode");
688 SetResult(otmCtx, clientResponse->result);
691 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
694 return OC_STACK_DELETE_TRANSACTION;
698 * Response handler for update owner crendetial request.
700 * @param[in] ctx ctx value passed to callback from calling function.
701 * @param[in] UNUSED handle to an invocation
702 * @param[in] clientResponse Response from queries to remote servers.
703 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
704 * and OC_STACK_KEEP_TRANSACTION to keep it.
706 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
707 OCClientResponse *clientResponse)
709 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
710 VERIFY_NON_NULL(TAG, ctx, WARNING);
712 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
714 OCStackResult res = OC_STACK_OK;
715 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
717 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
719 if(otmCtx && otmCtx->selectedDeviceInfo)
721 //Close the temporal secure session to verify the owner credential
722 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
723 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
724 CAResult_t caResult = CACloseDtlsSession(endpoint);
725 if(CA_STATUS_OK != caResult)
727 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
728 SetResult(otmCtx, caResult);
729 return OC_STACK_DELETE_TRANSACTION;
733 * If we select NULL cipher,
734 * client will select appropriate cipher suite according to server's cipher-suite list.
736 caResult = CASelectCipherSuite(TLS_NULL_WITH_NULL_NULL);
737 if(CA_STATUS_OK != caResult)
739 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
740 SetResult(otmCtx, caResult);
741 return OC_STACK_DELETE_TRANSACTION;
745 * in case of random PIN based OxM,
746 * revert get_psk_info callback of tinyDTLS to use owner credential.
748 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
750 OicUuid_t emptyUuid = { .id={0}};
751 SetUuidForRandomPinOxm(&emptyUuid);
753 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
755 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
756 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
757 return OC_STACK_DELETE_TRANSACTION;
761 //PUT /oic/sec/doxm [{ ..., "owned":"TRUE" }]
762 res = PutOwnershipInformation(otmCtx);
763 if(OC_STACK_OK != res)
765 OIC_LOG(ERROR, TAG, "Failed to put ownership information to new device");
766 SetResult(otmCtx, res);
767 return OC_STACK_DELETE_TRANSACTION;
773 res = clientResponse->result;
774 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
775 SetResult(otmCtx, res);
778 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
781 return OC_STACK_DELETE_TRANSACTION;
786 * Response handler for update owner information request.
788 * @param[in] ctx ctx value passed to callback from calling function.
789 * @param[in] UNUSED handle to an invocation
790 * @param[in] clientResponse Response from queries to remote servers.
791 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
792 * and OC_STACK_KEEP_TRANSACTION to keep it.
794 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
795 OCClientResponse *clientResponse)
797 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
798 VERIFY_NON_NULL(TAG, ctx, WARNING);
800 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
802 OCStackResult res = OC_STACK_OK;
803 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
805 if(OC_STACK_OK == clientResponse->result)
807 if(otmCtx && otmCtx->selectedDeviceInfo)
809 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
810 OIC_LOG(INFO, TAG, "Start defualt ACL & commit-hash provisioning.");
812 res = PutProvisioningStatus(otmCtx);
813 if(OC_STACK_OK != res)
815 OIC_LOG(ERROR, TAG, "Failed to update pstat");
816 SetResult(otmCtx, res);
822 res = clientResponse->result;
823 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
824 SetResult(otmCtx, res);
827 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
830 return OC_STACK_DELETE_TRANSACTION;
834 * Response handler of update provisioning status.
836 * @param[in] ctx ctx value passed to callback from calling function.
837 * @param[in] UNUSED handle to an invocation
838 * @param[in] clientResponse Response from queries to remote servers.
839 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
840 * and OC_STACK_KEEP_TRANSACTION to keep it.
842 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
843 OCClientResponse *clientResponse)
845 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
847 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
848 VERIFY_NON_NULL(TAG, ctx, ERROR);
850 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
853 if (OC_STACK_OK == clientResponse->result)
855 OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
856 if (OC_STACK_OK == res)
858 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
859 SetResult(otmCtx, OC_STACK_OK);
860 return OC_STACK_DELETE_TRANSACTION;
864 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
869 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
870 clientResponse->result);
871 SetResult(otmCtx, clientResponse->result);
876 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
877 return OC_STACK_DELETE_TRANSACTION;
880 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx)
882 OIC_LOG(DEBUG, TAG, "IN PutOwnerCredential");
884 if(!otmCtx || !otmCtx->selectedDeviceInfo)
886 OIC_LOG(ERROR, TAG, "Invalid parameters");
887 return OC_STACK_INVALID_PARAM;
890 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
891 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
893 if(!PMGenerateQuery(true,
894 deviceInfo->endpoint.addr, deviceInfo->securePort,
895 deviceInfo->connType,
896 query, sizeof(query), OIC_RSRC_CRED_URI))
898 OIC_LOG(ERROR, TAG, "PutOwnerCredential : Failed to generate query");
899 return OC_STACK_ERROR;
901 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
902 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
905 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
906 return OC_STACK_NO_MEMORY;
909 //Generate owner credential for new device
910 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
911 OicSecCred_t* ownerCredential =
912 GetCredResourceData(&(deviceInfo->doxm->deviceID));
915 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
916 return OC_STACK_NO_RESOURCE;
919 OicUuid_t credSubjectId = {.id={0}};
920 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
922 OicSecCred_t newCredential;
923 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
924 newCredential.next = NULL;
926 //Set subject ID as PT's ID
927 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
929 //Fill private data as empty string
930 newCredential.privateData.data = NULL;
932 //Send owner credential to new device : PUT /oic/sec/cred [ owner credential ]
934 if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData1, &size))
937 OIC_LOG(ERROR, TAG, "Error while converting bin to json");
938 return OC_STACK_ERROR;
940 OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData);
942 OCCallbackData cbData;
943 cbData.cb = &OwnerCredentialHandler;
944 cbData.context = (void *)otmCtx;
946 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
947 &deviceInfo->endpoint, (OCPayload*)secPayload,
948 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
949 if (res != OC_STACK_OK)
951 OIC_LOG(ERROR, TAG, "OCStack resource error");
956 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
957 return OC_STACK_NO_RESOURCE;
960 OIC_LOG(DEBUG, TAG, "OUT PutOwnerCredential");
965 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx)
967 OIC_LOG(DEBUG, TAG, "IN PutOwnerTransferModeToResource");
969 if(!otmCtx || !otmCtx->selectedDeviceInfo)
971 OIC_LOG(ERROR, TAG, "Invalid parameters");
972 return OC_STACK_INVALID_PARAM;
975 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
976 OicSecOxm_t selectedOxm = deviceInfo->doxm->oxmSel;
977 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
979 if(!PMGenerateQuery(false,
980 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
981 deviceInfo->connType,
982 query, sizeof(query), OIC_RSRC_DOXM_URI))
984 OIC_LOG(ERROR, TAG, "PutOwnerTransferModeToResource : Failed to generate query");
985 return OC_STACK_ERROR;
987 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
988 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
991 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
992 return OC_STACK_NO_MEMORY;
994 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
995 secPayload->securityData = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx);
996 if (NULL == secPayload->securityData)
999 OIC_LOG(ERROR, TAG, "Error while converting bin to json");
1000 return OC_STACK_ERROR;
1003 OCCallbackData cbData;
1004 cbData.cb = &OwnerTransferModeHandler;
1005 cbData.context = (void *)otmCtx;
1007 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
1008 &deviceInfo->endpoint, (OCPayload*)secPayload,
1009 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1010 if (res != OC_STACK_OK)
1012 OIC_LOG(ERROR, TAG, "OCStack resource error");
1015 OIC_LOG(DEBUG, TAG, "OUT PutOwnerTransferModeToResource");
1020 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1022 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1024 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1026 OIC_LOG(ERROR, TAG, "Invailed parameters");
1027 return OC_STACK_INVALID_PARAM;
1030 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1031 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1032 if(!PMGenerateQuery(false,
1033 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1034 deviceInfo->connType,
1035 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1037 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1038 return OC_STACK_ERROR;
1040 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1042 OCCallbackData cbData;
1043 cbData.cb = &ListMethodsHandler;
1044 cbData.context = (void *)otmCtx;
1046 OCStackResult res = OCDoResource(NULL, OC_REST_GET, query, NULL, NULL,
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 GetProvisioningStatusResource");
1058 static OCStackResult PutOwnerUuid(OTMContext_t* otmCtx)
1060 OIC_LOG(DEBUG, TAG, "IN PutOwnerUuid");
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(true,
1071 deviceInfo->endpoint.addr, deviceInfo->securePort,
1072 deviceInfo->connType,
1073 query, sizeof(query), OIC_RSRC_DOXM_URI))
1075 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1076 return OC_STACK_ERROR;
1078 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1080 //PUT PT's uuid to new device
1081 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1084 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1085 return OC_STACK_NO_MEMORY;
1087 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1088 secPayload->securityData =
1089 g_OTMDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(otmCtx);
1090 if (NULL == secPayload->securityData)
1092 OICFree(secPayload);
1093 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1094 return OC_STACK_INVALID_PARAM;
1096 OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData);
1098 OCCallbackData cbData;
1099 cbData.cb = &OwnerUuidUpdateHandler;
1100 cbData.context = (void *)otmCtx;
1103 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1104 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1105 if (res != OC_STACK_OK)
1107 OIC_LOG(ERROR, TAG, "OCStack resource error");
1110 OIC_LOG(DEBUG, TAG, "OUT PutOwnerUuid");
1115 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx)
1117 OIC_LOG(DEBUG, TAG, "IN PutOwnershipInformation");
1119 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1121 OIC_LOG(ERROR, TAG, "Invailed parameters");
1122 return OC_STACK_INVALID_PARAM;
1125 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1126 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1127 if(!PMGenerateQuery(true,
1128 deviceInfo->endpoint.addr, deviceInfo->securePort,
1129 deviceInfo->connType,
1130 query, sizeof(query), OIC_RSRC_DOXM_URI))
1132 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1133 return OC_STACK_ERROR;
1135 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1137 //OwnershipInformationHandler
1138 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1141 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1142 return OC_STACK_NO_MEMORY;
1145 otmCtx->selectedDeviceInfo->doxm->owned = true;
1146 secPayload->securityData = BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
1147 if (NULL == secPayload->securityData)
1149 OICFree(secPayload);
1150 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1151 return OC_STACK_INVALID_PARAM;
1153 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1155 OCCallbackData cbData;
1156 cbData.cb = &OwnershipInformationHandler;
1157 cbData.context = (void *)otmCtx;
1160 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1161 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1162 if (res != OC_STACK_OK)
1164 OIC_LOG(ERROR, TAG, "OCStack resource error");
1167 OIC_LOG(DEBUG, TAG, "OUT PutOwnershipInformation");
1172 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx)
1174 OIC_LOG(DEBUG, TAG, "IN PutUpdateOperationMode");
1176 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1178 return OC_STACK_INVALID_PARAM;
1181 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1182 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1183 if(!PMGenerateQuery(false,
1184 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1185 deviceInfo->connType,
1186 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1188 OIC_LOG(ERROR, TAG, "PutUpdateOperationMode : Failed to generate query");
1189 return OC_STACK_ERROR;
1191 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1193 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1196 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1197 return OC_STACK_NO_MEMORY;
1199 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1201 OCStackResult res = PstatToCBORPayload(deviceInfo->pstat, &secPayload->securityData1,
1203 if (OC_STACK_OK != res)
1205 OCPayloadDestroy((OCPayload *)secPayload);
1206 OIC_LOG(ERROR, TAG, "Error while converting pstat to cbor.");
1207 return OC_STACK_INVALID_PARAM;
1210 OCCallbackData cbData;
1211 cbData.cb = &OperationModeUpdateHandler;
1212 cbData.context = (void *)otmCtx;
1214 res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1215 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1216 if (res != OC_STACK_OK)
1218 OIC_LOG(ERROR, TAG, "OCStack resource error");
1221 OIC_LOG(DEBUG, TAG, "OUT PutUpdateOperationMode");
1226 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
1228 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
1229 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1230 otmCtx->selectedDeviceInfo = selectedDevice;
1232 //Set to the lowest level OxM, and then find more higher level OxM.
1233 OCStackResult res = SelectProvisioningMethod(selectedDevice->doxm->oxm,
1234 selectedDevice->doxm->oxmLen,
1235 &selectedDevice->doxm->oxmSel);
1236 if(OC_STACK_OK != res)
1238 OIC_LOG(ERROR, TAG, "Failed to select the provisioning method");
1239 SetResult(otmCtx, res);
1242 OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
1244 //Send Req: PUT /oic/sec/doxm [{..."OxmSel" :g_OTMDatas[Index of Selected OxM].OXMString,...}]
1245 res = PutOwnerTransferModeToResource(otmCtx);
1246 if(OC_STACK_OK != res)
1248 OIC_LOG(WARNING, TAG, "Failed to select the provisioning method");
1249 SetResult(otmCtx, res);
1253 //Register DTLS event handler to catch the dtls event while handshake
1254 if(CA_STATUS_OK != CARegisterDTLSHandshakeCallback(DTLSHandshakeCB))
1256 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register DTLS handshake callback.");
1259 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
1265 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
1267 OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
1271 OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
1272 return OC_STACK_INVALID_PARAM;
1274 if(oxmType >= OIC_OXM_COUNT)
1276 OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
1277 return OC_STACK_INVALID_PARAM;
1280 g_OTMDatas[oxmType].loadSecretCB= data->loadSecretCB;
1281 g_OTMDatas[oxmType].createSecureSessionCB = data->createSecureSessionCB;
1282 g_OTMDatas[oxmType].createSelectOxmPayloadCB = data->createSelectOxmPayloadCB;
1283 g_OTMDatas[oxmType].createOwnerTransferPayloadCB = data->createOwnerTransferPayloadCB;
1285 OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
1291 * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
1293 OCStackResult OTMDoOwnershipTransfer(void* ctx,
1294 OCProvisionDev_t *selectedDevicelist,
1295 OCProvisionResultCB resultCallback)
1297 OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
1299 if (NULL == selectedDevicelist)
1301 return OC_STACK_INVALID_PARAM;
1303 if (NULL == resultCallback)
1305 return OC_STACK_INVALID_CALLBACK;
1308 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
1311 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
1312 return OC_STACK_NO_MEMORY;
1314 otmCtx->ctxResultCallback = resultCallback;
1315 otmCtx->ctxHasError = false;
1316 otmCtx->userCtx = ctx;
1317 OCProvisionDev_t* pCurDev = selectedDevicelist;
1319 //Counting number of selected devices.
1320 otmCtx->ctxResultArraySize = 0;
1321 while(NULL != pCurDev)
1323 otmCtx->ctxResultArraySize++;
1324 pCurDev = pCurDev->next;
1327 otmCtx->ctxResultArray =
1328 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
1329 if(NULL == otmCtx->ctxResultArray)
1331 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
1333 return OC_STACK_NO_MEMORY;
1335 pCurDev = selectedDevicelist;
1337 OCStackResult res = OC_STACK_OK;
1338 //Fill the device UUID for result array.
1339 for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
1341 //Checking duplication of Device ID.
1342 bool isDuplicate = true;
1343 res = PDMIsDuplicateDevice(&pCurDev->doxm->deviceID, &isDuplicate);
1344 if (OC_STACK_OK != res)
1350 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Device ID is duplicated");
1351 res = OC_STACK_INVALID_PARAM;
1354 memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
1355 pCurDev->doxm->deviceID.id,
1357 otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
1358 pCurDev = pCurDev->next;
1361 StartOwnershipTransfer(otmCtx, selectedDevicelist);
1363 OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
1367 OICFree(otmCtx->ctxResultArray);
1372 OCStackResult PutProvisioningStatus(OTMContext_t* otmCtx)
1374 OIC_LOG(INFO, TAG, "IN PutProvisioningStatus");
1378 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1379 return OC_STACK_INVALID_PARAM;
1381 if(!otmCtx->selectedDeviceInfo)
1383 OIC_LOG(ERROR, TAG, "Can't find device information in OTMContext");
1385 return OC_STACK_INVALID_PARAM;
1388 otmCtx->selectedDeviceInfo->pstat->tm = NORMAL;
1389 otmCtx->selectedDeviceInfo->pstat->cm = PROVISION_ACLS | PROVISION_CREDENTIALS |
1390 SECURITY_MANAGEMENT_SERVICES | BOOTSTRAP_SERVICE;
1391 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1394 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1395 return OC_STACK_NO_MEMORY;
1397 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1399 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1400 &secPayload->securityData1, &size))
1402 OCPayloadDestroy((OCPayload *)secPayload);
1403 SetResult(otmCtx, OC_STACK_INVALID_JSON);
1404 return OC_STACK_INVALID_JSON;
1406 OIC_LOG_V(INFO, TAG, "Created payload for commit hash: %s",secPayload->securityData1);
1408 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1409 if(!PMGenerateQuery(true,
1410 otmCtx->selectedDeviceInfo->endpoint.addr,
1411 otmCtx->selectedDeviceInfo->securePort,
1412 otmCtx->selectedDeviceInfo->connType,
1413 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1415 OIC_LOG(ERROR, TAG, "PutProvisioningStatus : Failed to generate query");
1416 return OC_STACK_ERROR;
1418 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1420 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1421 cbData.cb = &ProvisioningStatusHandler;
1422 cbData.context = (void*)otmCtx;
1424 OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1425 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1426 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1427 if (ret != OC_STACK_OK)
1429 OIC_LOG(ERROR, TAG, "OCStack resource error");
1430 SetResult(otmCtx, ret);
1433 OIC_LOG(INFO, TAG, "OUT PutProvisioningStatus");