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"
65 * Array to store the callbacks for each owner transfer method.
67 static OTMCallbackData_t g_OTMDatas[OIC_OXM_COUNT];
70 * Variable for storing provisioning tool's provisioning capabilities
71 * Must be in decreasing order of preference. More prefered method should
72 * have lower array index.
74 static OicSecDpom_t gProvisioningToolCapability[] = { SINGLE_SERVICE_CLIENT_DRIVEN };
77 * Number of supported provisioning methods
78 * current version supports only one.
80 static size_t gNumOfProvisioningMethodsPT = 1;
83 * Variables for pointing the OTMContext to be used in the DTLS handshake result callback.
85 static OTMContext_t* g_otmCtx = NULL;
88 * Function to select appropriate provisioning method.
90 * @param[in] supportedMethods Array of supported methods
91 * @param[in] numberOfMethods number of supported methods
92 * @param[out] selectedMethod Selected methods
93 * @return OC_STACK_OK on success
95 static OCStackResult SelectProvisioningMethod(const OicSecOxm_t *supportedMethods,
96 size_t numberOfMethods,
97 OicSecOxm_t *selectedMethod)
99 OIC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
101 if(numberOfMethods == 0 || !supportedMethods)
103 OIC_LOG(WARNING, TAG, "Could not find a supported OxM.");
104 return OC_STACK_ERROR;
107 *selectedMethod = supportedMethods[0];
108 for(size_t i = 0; i < numberOfMethods; i++)
110 if(*selectedMethod < supportedMethods[i])
112 *selectedMethod = supportedMethods[i];
120 * Function to select operation mode.This function will return most secure common operation mode.
122 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
123 * @param[out] selectedMode selected operation mode
124 * @return OC_STACK_OK on success
126 static void SelectOperationMode(const OCProvisionDev_t *selectedDeviceInfo,
127 OicSecDpom_t *selectedMode)
129 OIC_LOG(DEBUG, TAG, "IN SelectOperationMode");
134 while (i < gNumOfProvisioningMethodsPT && j < selectedDeviceInfo->pstat->smLen)
136 if (gProvisioningToolCapability[i] < selectedDeviceInfo->pstat->sm[j])
140 else if (selectedDeviceInfo->pstat->sm[j] < gProvisioningToolCapability[i])
144 else /* if gProvisioningToolCapability[i] == deviceSupportedMethods[j] */
146 *selectedMode = gProvisioningToolCapability[j];
150 OIC_LOG_V(DEBUG, TAG, "Selected Operation Mode = %d", *selectedMode);
152 OIC_LOG(DEBUG, TAG, "OUT SelectOperationMode");
156 * Function to start ownership transfer.
157 * This function will send the first request for provisioning,
158 * The next request message is sent from the response handler for this request.
160 * @param[in] ctx context value passed to callback from calling function.
161 * @param[in] selectedDevice selected device information to performing provisioning.
162 * @return OC_STACK_OK on success
164 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
167 * Function to update owner transfer mode
169 * @param[in] otmCtx Context value of ownership transfer.
170 * @return OC_STACK_OK on success
172 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx);
175 * Function to send request to resource to get its pstat resource information.
177 * @param[in] otmCtx Context value of ownership transfer.
178 * @return OC_STACK_OK on success
180 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx);
184 * Function to send uuid of owner device to new device.
185 * This function would update 'owner of doxm' as UUID for provisioning tool.
187 * @param[in] otmCtx Context value of ownership transfer.
188 * @return OC_STACK_OK on success
190 static OCStackResult PutOwnerUuid(OTMContext_t* otmCtx);
193 * Function to update the operation mode. As per the spec. Operation mode in client driven
194 * single service provisioning it will be updated to 0x3
196 * @param[in] otmCtx Context value of ownership transfer.
197 * @return OC_STACK_OK on success
199 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx);
202 * Function to update the owner credential to new device
204 * @param[in] otmCtx Context value of ownership transfer.
205 * @param[in] selectedOperationMode selected operation mode
206 * @return OC_STACK_OK on success
208 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx);
211 * Function to send ownerShip info.
212 * This function would update 'owned of doxm' as true.
214 * @param[in] otmCtx Context value of ownership transfer.
215 * @return OC_STACK_OK on success
217 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx);
220 * Function to update pstat when finalize provisioning.
221 * This function would update 'cm' as bx0011,1100 and 'tm' as bx0000,0000.
223 * @param[in] ctx context value passed to callback from calling function.
224 * @param[in] selectedDevice selected device information to performing provisioning.
225 * @return OC_STACK_OK on success
227 static OCStackResult PutProvisioningStatus(OTMContext_t* otmCtx);
229 static bool IsComplete(OTMContext_t* otmCtx)
231 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
233 if(OC_STACK_CONTINUE == otmCtx->ctxResultArray[i].res)
243 * Function to save the result of provisioning.
245 * @param[in,out] otmCtx Context value of ownership transfer.
246 * @param[in] res result of provisioning
248 static void SetResult(OTMContext_t* otmCtx, const OCStackResult res)
250 OIC_LOG_V(DEBUG, TAG, "IN SetResult : %d ", res);
254 OIC_LOG(WARNING, TAG, "OTMContext is NULL");
258 if(otmCtx->selectedDeviceInfo)
260 //Revert psk_info callback and new deivce uuid in case of random PIN OxM
261 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
263 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
265 OIC_LOG(WARNING, TAG, "Failed to revert is DTLS credential handler.");
267 OicUuid_t emptyUuid = { .id={0}};
268 SetUuidForRandomPinOxm(&emptyUuid);
271 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
273 if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
274 otmCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
276 otmCtx->ctxResultArray[i].res = res;
277 if(OC_STACK_OK != res)
279 otmCtx->ctxHasError = true;
286 //If all request is completed, invoke the user callback.
287 if(IsComplete(otmCtx))
289 otmCtx->ctxResultCallback(otmCtx->userCtx, otmCtx->ctxResultArraySize,
290 otmCtx->ctxResultArray, otmCtx->ctxHasError);
291 OICFree(otmCtx->ctxResultArray);
296 if(OC_STACK_OK != StartOwnershipTransfer(otmCtx,
297 otmCtx->selectedDeviceInfo->next))
299 OIC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
304 OIC_LOG(DEBUG, TAG, "OUT SetResult");
308 * Function to handle the handshake result in OTM.
309 * This function will be invoked after DTLS handshake
310 * @param endPoint [IN] The remote endpoint.
311 * @param errorInfo [IN] Error information from the endpoint.
314 void DTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
316 if(NULL != g_otmCtx && NULL != g_otmCtx->selectedDeviceInfo &&
317 NULL != endpoint && NULL != info)
319 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
320 endpoint->addr, endpoint->port, info->result);
322 OicSecDoxm_t* newDevDoxm = g_otmCtx->selectedDeviceInfo->doxm;
324 if(NULL != newDevDoxm)
326 OicUuid_t emptyUuid = {.id={0}};
328 //Make sure the address matches.
329 if(strncmp(g_otmCtx->selectedDeviceInfo->endpoint.addr,
331 sizeof(endpoint->addr)) == 0 &&
332 g_otmCtx->selectedDeviceInfo->securePort == endpoint->port)
334 OCStackResult res = OC_STACK_ERROR;
336 //If temporal secure sesstion established successfully
337 if(CA_STATUS_OK == info->result &&
338 false == newDevDoxm->owned &&
339 memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) == 0)
341 //Send request : PUT /oic/sec/doxm [{... , "devowner":"PT's UUID"}]
342 res = PutOwnerUuid(g_otmCtx);
343 if(OC_STACK_OK != res)
345 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
346 SetResult(g_otmCtx, res);
349 //In case of authentication failure
350 else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
352 //in case of error from owner credential
353 if(memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) != 0 &&
354 true == newDevDoxm->owned)
356 OIC_LOG(ERROR, TAG, "The owner credential may incorrect.");
358 if(OC_STACK_OK != RemoveCredential(&(newDevDoxm->deviceID)))
360 OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
362 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
364 //in case of error from wrong PIN, re-start the ownership transfer
365 else if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
367 OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
369 memcpy(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
370 newDevDoxm->owned = false;
371 g_otmCtx->attemptCnt++;
373 if(WRONG_PIN_MAX_ATTEMP > g_otmCtx->attemptCnt)
375 res = StartOwnershipTransfer(g_otmCtx, g_otmCtx->selectedDeviceInfo);
376 if(OC_STACK_OK != res)
378 SetResult(g_otmCtx, res);
379 OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
384 OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
385 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
390 OIC_LOG(ERROR, TAG, "Failed to establish secure session.");
391 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
400 * Function to save ownerPSK at provisioning tool end.
402 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
403 * @return OC_STACK_OK on success
405 static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
407 OIC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
409 OCStackResult res = OC_STACK_ERROR;
411 CAEndpoint_t endpoint;
412 memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
413 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
414 endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
415 endpoint.port = selectedDeviceInfo->securePort;
417 OicUuid_t ptDeviceID = {.id={0}};
418 if (OC_STACK_OK != GetDoxmDeviceID(&ptDeviceID))
420 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
424 uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
426 //Generating OwnerPSK
427 CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
428 (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
429 strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
430 ptDeviceID.id, sizeof(ptDeviceID.id),
431 selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
432 ownerPSK, OWNER_PSK_LENGTH_128);
434 if (CA_STATUS_OK == pskRet)
436 OIC_LOG(INFO, TAG,"ownerPSK dump:\n");
437 OIC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
438 //Generating new credential for provisioning tool
442 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(ownerPSK)) + 1] = {};
443 B64Result b64Ret = b64Encode(ownerPSK, sizeof(ownerPSK), base64Buff, sizeof(base64Buff),
445 VERIFY_SUCCESS(TAG, B64_OK == b64Ret, ERROR);
447 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
448 SYMMETRIC_PAIR_WISE_KEY, NULL,
449 base64Buff, ownLen, &ptDeviceID);
450 VERIFY_NON_NULL(TAG, cred, ERROR);
452 res = AddCredential(cred);
453 if(res != OC_STACK_OK)
455 DeleteCredList(cred);
461 OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
464 OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
470 * Callback handler for OwnerShipTransferModeHandler API.
472 * @param[in] ctx ctx value passed to callback from calling function.
473 * @param[in] UNUSED handle to an invocation
474 * @param[in] clientResponse Response from queries to remote servers.
475 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
476 * and OC_STACK_KEEP_TRANSACTION to keep it.
478 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
479 OCClientResponse *clientResponse)
481 OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
483 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
484 VERIFY_NON_NULL(TAG, ctx, WARNING);
486 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
488 if(clientResponse->result == OC_STACK_OK)
490 OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
491 //Send request : GET /oic/sec/pstat
492 OCStackResult res = GetProvisioningStatusResource(otmCtx);
493 if(OC_STACK_OK != res)
495 OIC_LOG(WARNING, TAG, "Failed to get pstat information");
496 SetResult(otmCtx, res);
501 OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
502 clientResponse->result);
503 SetResult(otmCtx, clientResponse->result);
506 OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
509 return OC_STACK_DELETE_TRANSACTION;
513 * Callback handler for ProvisioningStatusResouceHandler API.
515 * @param[in] ctx ctx value passed to callback from calling function.
516 * @param[in] UNUSED handle to an invocation
517 * @param[in] clientResponse Response from queries to remote servers.
518 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
519 * and OC_STACK_KEEP_TRANSACTION to keep it.
521 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
522 OCClientResponse *clientResponse)
524 OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
526 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
527 VERIFY_NON_NULL(TAG, ctx, WARNING);
529 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
531 if (OC_STACK_OK == clientResponse->result)
533 if (NULL == clientResponse->payload)
535 OIC_LOG(INFO, TAG, "Skiping Null payload");
536 SetResult(otmCtx, OC_STACK_ERROR);
537 return OC_STACK_DELETE_TRANSACTION;
540 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
542 OIC_LOG(INFO, TAG, "Unknown payload type");
543 SetResult(otmCtx, OC_STACK_ERROR);
544 return OC_STACK_DELETE_TRANSACTION;
547 OicSecPstat_t* pstat = JSONToPstatBin(
548 ((OCSecurityPayload*)clientResponse->payload)->securityData);
551 OIC_LOG(ERROR, TAG, "Error while converting json to pstat bin");
552 SetResult(otmCtx, OC_STACK_ERROR);
553 return OC_STACK_DELETE_TRANSACTION;
555 otmCtx->selectedDeviceInfo->pstat = pstat;
557 //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
558 SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
560 //Send request : PUT /oic/sec/pstat [{"om":"bx11", .. }]
561 OCStackResult res = PutUpdateOperationMode(otmCtx);
562 if (OC_STACK_OK != res)
564 OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
565 SetResult(otmCtx, res);
570 OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
571 clientResponse->result);
572 SetResult(otmCtx, clientResponse->result);
575 OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
577 return OC_STACK_DELETE_TRANSACTION;
581 * Response handler for update owner uuid request.
583 * @param[in] ctx ctx value passed to callback from calling function.
584 * @param[in] UNUSED handle to an invocation
585 * @param[in] clientResponse Response from queries to remote servers.
586 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
587 * and OC_STACK_KEEP_TRANSACTION to keep it.
589 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
590 OCClientResponse *clientResponse)
592 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
593 VERIFY_NON_NULL(TAG, ctx, WARNING);
595 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
597 OCStackResult res = OC_STACK_OK;
598 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
600 if(OC_STACK_OK == clientResponse->result)
602 if(otmCtx && otmCtx->selectedDeviceInfo)
604 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
605 if(OC_STACK_OK != res)
607 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
608 SetResult(otmCtx, res);
609 return OC_STACK_DELETE_TRANSACTION;
612 //PUT owner credential to new device according to security spec B.
613 res = PutOwnerCredential(otmCtx);
614 if(OC_STACK_OK != res)
617 "OwnerUuidUpdateHandler:Failed to send PUT request for onwer credential");
618 SetResult(otmCtx, res);
619 return OC_STACK_DELETE_TRANSACTION;
625 res = clientResponse->result;
626 OIC_LOG_V(ERROR, TAG, "OwnerUuidHandler : Unexpected result %d", res);
627 SetResult(otmCtx, res);
630 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
633 return OC_STACK_DELETE_TRANSACTION;
637 * Response handler for update operation mode.
639 * @param[in] ctx ctx value passed to callback from calling function.
640 * @param[in] UNUSED handle to an invocation
641 * @param[in] clientResponse Response from queries to remote servers.
642 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
643 * and OC_STACK_KEEP_TRANSACTION to keep it.
645 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
646 OCClientResponse *clientResponse)
648 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
650 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
651 VERIFY_NON_NULL(TAG, ctx, WARNING);
653 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
655 if (OC_STACK_OK == clientResponse->result)
657 OCStackResult res = OC_STACK_ERROR;
658 OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
660 //Load secret for temporal secure session.
661 if(g_OTMDatas[selOxm].loadSecretCB)
663 res = g_OTMDatas[selOxm].loadSecretCB(otmCtx);
664 if(OC_STACK_OK != res)
666 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
667 SetResult(otmCtx, res);
668 return OC_STACK_DELETE_TRANSACTION;
672 //It will be used in handshake event handler
675 //Try DTLS handshake to generate secure session
676 if(g_OTMDatas[selOxm].createSecureSessionCB)
678 res = g_OTMDatas[selOxm].createSecureSessionCB(otmCtx);
679 if(OC_STACK_OK != res)
681 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
682 SetResult(otmCtx, res);
683 return OC_STACK_DELETE_TRANSACTION;
689 OIC_LOG(ERROR, TAG, "Error while update operation mode");
690 SetResult(otmCtx, clientResponse->result);
693 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
696 return OC_STACK_DELETE_TRANSACTION;
700 * Response handler for update owner crendetial request.
702 * @param[in] ctx ctx value passed to callback from calling function.
703 * @param[in] UNUSED handle to an invocation
704 * @param[in] clientResponse Response from queries to remote servers.
705 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
706 * and OC_STACK_KEEP_TRANSACTION to keep it.
708 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
709 OCClientResponse *clientResponse)
711 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
712 VERIFY_NON_NULL(TAG, ctx, WARNING);
714 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
716 OCStackResult res = OC_STACK_OK;
717 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
719 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
721 if(otmCtx && otmCtx->selectedDeviceInfo)
723 //Close the temporal secure session to verify the owner credential
724 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
725 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
726 CAResult_t caResult = CACloseDtlsSession(endpoint);
727 if(CA_STATUS_OK != caResult)
729 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
730 SetResult(otmCtx, caResult);
731 return OC_STACK_DELETE_TRANSACTION;
735 * If we select NULL cipher,
736 * client will select appropriate cipher suite according to server's cipher-suite list.
738 caResult = CASelectCipherSuite(TLS_NULL_WITH_NULL_NULL);
739 if(CA_STATUS_OK != caResult)
741 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
742 SetResult(otmCtx, caResult);
743 return OC_STACK_DELETE_TRANSACTION;
747 * in case of random PIN based OxM,
748 * revert get_psk_info callback of tinyDTLS to use owner credential.
750 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
752 OicUuid_t emptyUuid = { .id={0}};
753 SetUuidForRandomPinOxm(&emptyUuid);
755 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
757 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
758 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
759 return OC_STACK_DELETE_TRANSACTION;
763 //PUT /oic/sec/doxm [{ ..., "owned":"TRUE" }]
764 res = PutOwnershipInformation(otmCtx);
765 if(OC_STACK_OK != res)
767 OIC_LOG(ERROR, TAG, "Failed to put ownership information to new device");
768 SetResult(otmCtx, res);
769 return OC_STACK_DELETE_TRANSACTION;
775 res = clientResponse->result;
776 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
777 SetResult(otmCtx, res);
780 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
783 return OC_STACK_DELETE_TRANSACTION;
788 * Response handler for update owner information request.
790 * @param[in] ctx ctx value passed to callback from calling function.
791 * @param[in] UNUSED handle to an invocation
792 * @param[in] clientResponse Response from queries to remote servers.
793 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
794 * and OC_STACK_KEEP_TRANSACTION to keep it.
796 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
797 OCClientResponse *clientResponse)
799 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
800 VERIFY_NON_NULL(TAG, ctx, WARNING);
802 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
804 OCStackResult res = OC_STACK_OK;
805 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
807 if(OC_STACK_OK == clientResponse->result)
809 if(otmCtx && otmCtx->selectedDeviceInfo)
811 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
812 OIC_LOG(INFO, TAG, "Start defualt ACL & commit-hash provisioning.");
814 res = PutProvisioningStatus(otmCtx);
815 if(OC_STACK_OK != res)
817 OIC_LOG(ERROR, TAG, "Failed to update pstat");
818 SetResult(otmCtx, res);
824 res = clientResponse->result;
825 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
826 SetResult(otmCtx, res);
829 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
832 return OC_STACK_DELETE_TRANSACTION;
836 * Response handler of update provisioning status.
838 * @param[in] ctx ctx value passed to callback from calling function.
839 * @param[in] UNUSED handle to an invocation
840 * @param[in] clientResponse Response from queries to remote servers.
841 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
842 * and OC_STACK_KEEP_TRANSACTION to keep it.
844 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
845 OCClientResponse *clientResponse)
847 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
849 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
850 VERIFY_NON_NULL(TAG, ctx, ERROR);
852 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
855 if (OC_STACK_OK == clientResponse->result)
857 OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
858 if (OC_STACK_OK == res)
860 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
861 SetResult(otmCtx, OC_STACK_OK);
862 return OC_STACK_DELETE_TRANSACTION;
866 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
871 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
872 clientResponse->result);
873 SetResult(otmCtx, clientResponse->result);
878 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
879 return OC_STACK_DELETE_TRANSACTION;
882 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx)
884 OIC_LOG(DEBUG, TAG, "IN PutOwnerCredential");
886 if(!otmCtx || !otmCtx->selectedDeviceInfo)
888 OIC_LOG(ERROR, TAG, "Invalid parameters");
889 return OC_STACK_INVALID_PARAM;
892 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
893 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
895 if(!PMGenerateQuery(true,
896 deviceInfo->endpoint.addr, deviceInfo->securePort,
897 deviceInfo->connType,
898 query, sizeof(query), OIC_RSRC_CRED_URI))
900 OIC_LOG(ERROR, TAG, "PutOwnerCredential : Failed to generate query");
901 return OC_STACK_ERROR;
903 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
904 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
907 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
908 return OC_STACK_NO_MEMORY;
911 //Generate owner credential for new device
912 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
913 OicSecCred_t* ownerCredential =
914 GetCredResourceData(&(deviceInfo->doxm->deviceID));
917 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
918 return OC_STACK_NO_RESOURCE;
921 OicUuid_t credSubjectId = {.id={0}};
922 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
924 OicSecCred_t newCredential;
925 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
926 newCredential.next = NULL;
928 //Set subject ID as PT's ID
929 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
931 //Fill private data as empty string
932 newCredential.privateData.data = NULL;
934 //Send owner credential to new device : PUT /oic/sec/cred [ owner credential ]
935 secPayload->securityData = BinToCredJSON(&newCredential);
936 if (NULL == secPayload->securityData)
939 OIC_LOG(ERROR, TAG, "Error while converting bin to json");
940 return OC_STACK_ERROR;
942 OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData);
944 OCCallbackData cbData;
945 cbData.cb = &OwnerCredentialHandler;
946 cbData.context = (void *)otmCtx;
948 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
949 &deviceInfo->endpoint, (OCPayload*)secPayload,
950 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
951 if (res != OC_STACK_OK)
953 OIC_LOG(ERROR, TAG, "OCStack resource error");
958 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
959 return OC_STACK_NO_RESOURCE;
962 OIC_LOG(DEBUG, TAG, "OUT PutOwnerCredential");
967 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx)
969 OIC_LOG(DEBUG, TAG, "IN PutOwnerTransferModeToResource");
971 if(!otmCtx || !otmCtx->selectedDeviceInfo)
973 OIC_LOG(ERROR, TAG, "Invalid parameters");
974 return OC_STACK_INVALID_PARAM;
977 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
978 OicSecOxm_t selectedOxm = deviceInfo->doxm->oxmSel;
979 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
981 if(!PMGenerateQuery(false,
982 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
983 deviceInfo->connType,
984 query, sizeof(query), OIC_RSRC_DOXM_URI))
986 OIC_LOG(ERROR, TAG, "PutOwnerTransferModeToResource : Failed to generate query");
987 return OC_STACK_ERROR;
989 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
990 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
993 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
994 return OC_STACK_NO_MEMORY;
996 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
997 secPayload->securityData = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx);
998 if (NULL == secPayload->securityData)
1000 OICFree(secPayload);
1001 OIC_LOG(ERROR, TAG, "Error while converting bin to json");
1002 return OC_STACK_ERROR;
1005 OCCallbackData cbData;
1006 cbData.cb = &OwnerTransferModeHandler;
1007 cbData.context = (void *)otmCtx;
1009 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
1010 &deviceInfo->endpoint, (OCPayload*)secPayload,
1011 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1012 if (res != OC_STACK_OK)
1014 OIC_LOG(ERROR, TAG, "OCStack resource error");
1017 OIC_LOG(DEBUG, TAG, "OUT PutOwnerTransferModeToResource");
1022 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1024 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1026 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1028 OIC_LOG(ERROR, TAG, "Invailed parameters");
1029 return OC_STACK_INVALID_PARAM;
1032 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1033 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1034 if(!PMGenerateQuery(false,
1035 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1036 deviceInfo->connType,
1037 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1039 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1040 return OC_STACK_ERROR;
1042 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1044 OCCallbackData cbData;
1045 cbData.cb = &ListMethodsHandler;
1046 cbData.context = (void *)otmCtx;
1048 OCStackResult res = OCDoResource(NULL, OC_REST_GET, query, NULL, NULL,
1049 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1050 if (res != OC_STACK_OK)
1052 OIC_LOG(ERROR, TAG, "OCStack resource error");
1055 OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1060 static OCStackResult PutOwnerUuid(OTMContext_t* otmCtx)
1062 OIC_LOG(DEBUG, TAG, "IN PutOwnerUuid");
1064 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1066 OIC_LOG(ERROR, TAG, "Invailed parameters");
1067 return OC_STACK_INVALID_PARAM;
1070 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1071 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1072 if(!PMGenerateQuery(true,
1073 deviceInfo->endpoint.addr, deviceInfo->securePort,
1074 deviceInfo->connType,
1075 query, sizeof(query), OIC_RSRC_DOXM_URI))
1077 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1078 return OC_STACK_ERROR;
1080 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1082 //PUT PT's uuid to new device
1083 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1086 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1087 return OC_STACK_NO_MEMORY;
1089 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1090 secPayload->securityData =
1091 g_OTMDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(otmCtx);
1092 if (NULL == secPayload->securityData)
1094 OICFree(secPayload);
1095 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1096 return OC_STACK_INVALID_PARAM;
1098 OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData);
1100 OCCallbackData cbData;
1101 cbData.cb = &OwnerUuidUpdateHandler;
1102 cbData.context = (void *)otmCtx;
1105 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1106 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1107 if (res != OC_STACK_OK)
1109 OIC_LOG(ERROR, TAG, "OCStack resource error");
1112 OIC_LOG(DEBUG, TAG, "OUT PutOwnerUuid");
1117 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx)
1119 OIC_LOG(DEBUG, TAG, "IN PutOwnershipInformation");
1121 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1123 OIC_LOG(ERROR, TAG, "Invailed parameters");
1124 return OC_STACK_INVALID_PARAM;
1127 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1128 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1129 if(!PMGenerateQuery(true,
1130 deviceInfo->endpoint.addr, deviceInfo->securePort,
1131 deviceInfo->connType,
1132 query, sizeof(query), OIC_RSRC_DOXM_URI))
1134 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1135 return OC_STACK_ERROR;
1137 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1139 //OwnershipInformationHandler
1140 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1143 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1144 return OC_STACK_NO_MEMORY;
1147 otmCtx->selectedDeviceInfo->doxm->owned = true;
1148 secPayload->securityData = BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
1149 if (NULL == secPayload->securityData)
1151 OICFree(secPayload);
1152 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1153 return OC_STACK_INVALID_PARAM;
1155 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1157 OCCallbackData cbData;
1158 cbData.cb = &OwnershipInformationHandler;
1159 cbData.context = (void *)otmCtx;
1162 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1163 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1164 if (res != OC_STACK_OK)
1166 OIC_LOG(ERROR, TAG, "OCStack resource error");
1169 OIC_LOG(DEBUG, TAG, "OUT PutOwnershipInformation");
1174 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx)
1176 OIC_LOG(DEBUG, TAG, "IN PutUpdateOperationMode");
1178 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1180 return OC_STACK_INVALID_PARAM;
1183 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1184 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1185 if(!PMGenerateQuery(false,
1186 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1187 deviceInfo->connType,
1188 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1190 OIC_LOG(ERROR, TAG, "PutUpdateOperationMode : Failed to generate query");
1191 return OC_STACK_ERROR;
1193 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1195 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1198 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1199 return OC_STACK_NO_MEMORY;
1201 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1202 secPayload->securityData = BinToPstatJSON(deviceInfo->pstat);
1203 if (NULL == secPayload->securityData)
1205 OICFree(secPayload);
1206 OIC_LOG(ERROR, TAG, "Error while converting pstat bin to json");
1207 return OC_STACK_INVALID_PARAM;
1210 OCCallbackData cbData;
1211 cbData.cb = &OperationModeUpdateHandler;
1212 cbData.context = (void *)otmCtx;
1214 OCStackResult 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);
1373 OCStackResult PutProvisioningStatus(OTMContext_t* otmCtx)
1375 OIC_LOG(INFO, TAG, "IN PutProvisioningStatus");
1379 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1380 return OC_STACK_INVALID_PARAM;
1382 if(!otmCtx->selectedDeviceInfo)
1384 OIC_LOG(ERROR, TAG, "Can't find device information in OTMContext");
1386 return OC_STACK_INVALID_PARAM;
1389 otmCtx->selectedDeviceInfo->pstat->tm = NORMAL;
1390 otmCtx->selectedDeviceInfo->pstat->cm = PROVISION_ACLS | PROVISION_CREDENTIALS |
1391 SECURITY_MANAGEMENT_SERVICES | BOOTSTRAP_SERVICE;
1392 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1395 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1396 return OC_STACK_NO_MEMORY;
1398 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1399 secPayload->securityData = BinToPstatJSON(otmCtx->selectedDeviceInfo->pstat);
1400 if (NULL == secPayload->securityData)
1402 OICFree(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->securityData);
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");