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
444 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(ownerPSK)) + 1] = {};
445 B64Result b64Ret = b64Encode(ownerPSK, sizeof(ownerPSK), base64Buff, sizeof(base64Buff),
447 VERIFY_SUCCESS(TAG, B64_OK == b64Ret, ERROR);
449 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
450 SYMMETRIC_PAIR_WISE_KEY, NULL,
451 base64Buff, ownLen, &ptDeviceID);
452 VERIFY_NON_NULL(TAG, cred, ERROR);
454 res = AddCredential(cred);
455 if(res != OC_STACK_OK)
457 DeleteCredList(cred);
463 OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
466 OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
472 * Callback handler for OwnerShipTransferModeHandler API.
474 * @param[in] ctx ctx value passed to callback from calling function.
475 * @param[in] UNUSED handle to an invocation
476 * @param[in] clientResponse Response from queries to remote servers.
477 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
478 * and OC_STACK_KEEP_TRANSACTION to keep it.
480 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
481 OCClientResponse *clientResponse)
483 OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
485 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
486 VERIFY_NON_NULL(TAG, ctx, WARNING);
488 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
490 if(clientResponse->result == OC_STACK_OK)
492 OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
493 //Send request : GET /oic/sec/pstat
494 OCStackResult res = GetProvisioningStatusResource(otmCtx);
495 if(OC_STACK_OK != res)
497 OIC_LOG(WARNING, TAG, "Failed to get pstat information");
498 SetResult(otmCtx, res);
503 OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
504 clientResponse->result);
505 SetResult(otmCtx, clientResponse->result);
508 OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
511 return OC_STACK_DELETE_TRANSACTION;
515 * Callback handler for ProvisioningStatusResouceHandler API.
517 * @param[in] ctx ctx value passed to callback from calling function.
518 * @param[in] UNUSED handle to an invocation
519 * @param[in] clientResponse Response from queries to remote servers.
520 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
521 * and OC_STACK_KEEP_TRANSACTION to keep it.
523 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
524 OCClientResponse *clientResponse)
526 OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
528 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
529 VERIFY_NON_NULL(TAG, ctx, WARNING);
531 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
533 if (OC_STACK_OK == clientResponse->result)
535 if (NULL == clientResponse->payload)
537 OIC_LOG(INFO, TAG, "Skiping Null payload");
538 SetResult(otmCtx, OC_STACK_ERROR);
539 return OC_STACK_DELETE_TRANSACTION;
542 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
544 OIC_LOG(INFO, TAG, "Unknown payload type");
545 SetResult(otmCtx, OC_STACK_ERROR);
546 return OC_STACK_DELETE_TRANSACTION;
549 OicSecPstat_t* pstat = JSONToPstatBin(
550 ((OCSecurityPayload*)clientResponse->payload)->securityData);
553 OIC_LOG(ERROR, TAG, "Error while converting json to pstat bin");
554 SetResult(otmCtx, OC_STACK_ERROR);
555 return OC_STACK_DELETE_TRANSACTION;
557 otmCtx->selectedDeviceInfo->pstat = pstat;
559 //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
560 SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
562 //Send request : PUT /oic/sec/pstat [{"om":"bx11", .. }]
563 OCStackResult res = PutUpdateOperationMode(otmCtx);
564 if (OC_STACK_OK != res)
566 OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
567 SetResult(otmCtx, res);
572 OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
573 clientResponse->result);
574 SetResult(otmCtx, clientResponse->result);
577 OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
579 return OC_STACK_DELETE_TRANSACTION;
583 * Response handler for update owner uuid request.
585 * @param[in] ctx ctx value passed to callback from calling function.
586 * @param[in] UNUSED handle to an invocation
587 * @param[in] clientResponse Response from queries to remote servers.
588 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
589 * and OC_STACK_KEEP_TRANSACTION to keep it.
591 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
592 OCClientResponse *clientResponse)
594 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
595 VERIFY_NON_NULL(TAG, ctx, WARNING);
597 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
599 OCStackResult res = OC_STACK_OK;
600 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
602 if(OC_STACK_OK == clientResponse->result)
604 if(otmCtx && otmCtx->selectedDeviceInfo)
606 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
607 if(OC_STACK_OK != res)
609 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
610 SetResult(otmCtx, res);
611 return OC_STACK_DELETE_TRANSACTION;
614 //PUT owner credential to new device according to security spec B.
615 res = PutOwnerCredential(otmCtx);
616 if(OC_STACK_OK != res)
619 "OwnerUuidUpdateHandler:Failed to send PUT request for onwer credential");
620 SetResult(otmCtx, res);
621 return OC_STACK_DELETE_TRANSACTION;
627 res = clientResponse->result;
628 OIC_LOG_V(ERROR, TAG, "OwnerUuidHandler : Unexpected result %d", res);
629 SetResult(otmCtx, res);
632 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
635 return OC_STACK_DELETE_TRANSACTION;
639 * Response handler for update operation mode.
641 * @param[in] ctx ctx value passed to callback from calling function.
642 * @param[in] UNUSED handle to an invocation
643 * @param[in] clientResponse Response from queries to remote servers.
644 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
645 * and OC_STACK_KEEP_TRANSACTION to keep it.
647 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
648 OCClientResponse *clientResponse)
650 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
652 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
653 VERIFY_NON_NULL(TAG, ctx, WARNING);
655 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
657 if (OC_STACK_OK == clientResponse->result)
659 OCStackResult res = OC_STACK_ERROR;
660 OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
662 //Load secret for temporal secure session.
663 if(g_OTMDatas[selOxm].loadSecretCB)
665 res = g_OTMDatas[selOxm].loadSecretCB(otmCtx);
666 if(OC_STACK_OK != res)
668 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
669 SetResult(otmCtx, res);
670 return OC_STACK_DELETE_TRANSACTION;
674 //It will be used in handshake event handler
677 //Try DTLS handshake to generate secure session
678 if(g_OTMDatas[selOxm].createSecureSessionCB)
680 res = g_OTMDatas[selOxm].createSecureSessionCB(otmCtx);
681 if(OC_STACK_OK != res)
683 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
684 SetResult(otmCtx, res);
685 return OC_STACK_DELETE_TRANSACTION;
691 OIC_LOG(ERROR, TAG, "Error while update operation mode");
692 SetResult(otmCtx, clientResponse->result);
695 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
698 return OC_STACK_DELETE_TRANSACTION;
702 * Response handler for update owner crendetial request.
704 * @param[in] ctx ctx value passed to callback from calling function.
705 * @param[in] UNUSED handle to an invocation
706 * @param[in] clientResponse Response from queries to remote servers.
707 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
708 * and OC_STACK_KEEP_TRANSACTION to keep it.
710 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
711 OCClientResponse *clientResponse)
713 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
714 VERIFY_NON_NULL(TAG, ctx, WARNING);
716 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
718 OCStackResult res = OC_STACK_OK;
719 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
721 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
723 if(otmCtx && otmCtx->selectedDeviceInfo)
725 //Close the temporal secure session to verify the owner credential
726 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
727 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
728 CAResult_t caResult = CACloseDtlsSession(endpoint);
729 if(CA_STATUS_OK != caResult)
731 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
732 SetResult(otmCtx, caResult);
733 return OC_STACK_DELETE_TRANSACTION;
737 * If we select NULL cipher,
738 * client will select appropriate cipher suite according to server's cipher-suite list.
740 caResult = CASelectCipherSuite(TLS_NULL_WITH_NULL_NULL);
741 if(CA_STATUS_OK != caResult)
743 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
744 SetResult(otmCtx, caResult);
745 return OC_STACK_DELETE_TRANSACTION;
749 * in case of random PIN based OxM,
750 * revert get_psk_info callback of tinyDTLS to use owner credential.
752 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
754 OicUuid_t emptyUuid = { .id={0}};
755 SetUuidForRandomPinOxm(&emptyUuid);
757 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
759 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
760 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
761 return OC_STACK_DELETE_TRANSACTION;
765 //PUT /oic/sec/doxm [{ ..., "owned":"TRUE" }]
766 res = PutOwnershipInformation(otmCtx);
767 if(OC_STACK_OK != res)
769 OIC_LOG(ERROR, TAG, "Failed to put ownership information to new device");
770 SetResult(otmCtx, res);
771 return OC_STACK_DELETE_TRANSACTION;
777 res = clientResponse->result;
778 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
779 SetResult(otmCtx, res);
782 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
785 return OC_STACK_DELETE_TRANSACTION;
790 * Response handler for update owner information request.
792 * @param[in] ctx ctx value passed to callback from calling function.
793 * @param[in] UNUSED handle to an invocation
794 * @param[in] clientResponse Response from queries to remote servers.
795 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
796 * and OC_STACK_KEEP_TRANSACTION to keep it.
798 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
799 OCClientResponse *clientResponse)
801 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
802 VERIFY_NON_NULL(TAG, ctx, WARNING);
804 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
806 OCStackResult res = OC_STACK_OK;
807 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
809 if(OC_STACK_OK == clientResponse->result)
811 if(otmCtx && otmCtx->selectedDeviceInfo)
813 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
814 OIC_LOG(INFO, TAG, "Start defualt ACL & commit-hash provisioning.");
816 res = PutProvisioningStatus(otmCtx);
817 if(OC_STACK_OK != res)
819 OIC_LOG(ERROR, TAG, "Failed to update pstat");
820 SetResult(otmCtx, res);
826 res = clientResponse->result;
827 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
828 SetResult(otmCtx, res);
831 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
834 return OC_STACK_DELETE_TRANSACTION;
838 * Response handler of update provisioning status.
840 * @param[in] ctx ctx value passed to callback from calling function.
841 * @param[in] UNUSED handle to an invocation
842 * @param[in] clientResponse Response from queries to remote servers.
843 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
844 * and OC_STACK_KEEP_TRANSACTION to keep it.
846 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
847 OCClientResponse *clientResponse)
849 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
851 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
852 VERIFY_NON_NULL(TAG, ctx, ERROR);
854 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
857 if (OC_STACK_OK == clientResponse->result)
859 OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
860 if (OC_STACK_OK == res)
862 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
863 SetResult(otmCtx, OC_STACK_OK);
864 return OC_STACK_DELETE_TRANSACTION;
868 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
873 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
874 clientResponse->result);
875 SetResult(otmCtx, clientResponse->result);
880 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
881 return OC_STACK_DELETE_TRANSACTION;
884 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx)
886 OIC_LOG(DEBUG, TAG, "IN PutOwnerCredential");
888 if(!otmCtx || !otmCtx->selectedDeviceInfo)
890 OIC_LOG(ERROR, TAG, "Invalid parameters");
891 return OC_STACK_INVALID_PARAM;
894 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
895 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
897 if(!PMGenerateQuery(true,
898 deviceInfo->endpoint.addr, deviceInfo->securePort,
899 deviceInfo->connType,
900 query, sizeof(query), OIC_RSRC_CRED_URI))
902 OIC_LOG(ERROR, TAG, "PutOwnerCredential : Failed to generate query");
903 return OC_STACK_ERROR;
905 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
906 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
909 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
910 return OC_STACK_NO_MEMORY;
913 //Generate owner credential for new device
914 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
915 OicSecCred_t* ownerCredential =
916 GetCredResourceData(&(deviceInfo->doxm->deviceID));
919 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
920 return OC_STACK_NO_RESOURCE;
923 OicUuid_t credSubjectId = {.id={0}};
924 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
926 OicSecCred_t newCredential;
927 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
928 newCredential.next = NULL;
930 //Set subject ID as PT's ID
931 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
933 //Fill private data as empty string
934 newCredential.privateData.data = NULL;
936 //Send owner credential to new device : PUT /oic/sec/cred [ owner credential ]
937 secPayload->securityData = BinToCredJSON(&newCredential);
938 if (NULL == secPayload->securityData)
941 OIC_LOG(ERROR, TAG, "Error while converting bin to json");
942 return OC_STACK_ERROR;
944 OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData);
946 OCCallbackData cbData;
947 cbData.cb = &OwnerCredentialHandler;
948 cbData.context = (void *)otmCtx;
950 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
951 &deviceInfo->endpoint, (OCPayload*)secPayload,
952 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
953 if (res != OC_STACK_OK)
955 OIC_LOG(ERROR, TAG, "OCStack resource error");
960 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
961 return OC_STACK_NO_RESOURCE;
964 OIC_LOG(DEBUG, TAG, "OUT PutOwnerCredential");
969 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx)
971 OIC_LOG(DEBUG, TAG, "IN PutOwnerTransferModeToResource");
973 if(!otmCtx || !otmCtx->selectedDeviceInfo)
975 OIC_LOG(ERROR, TAG, "Invalid parameters");
976 return OC_STACK_INVALID_PARAM;
979 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
980 OicSecOxm_t selectedOxm = deviceInfo->doxm->oxmSel;
981 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
983 if(!PMGenerateQuery(false,
984 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
985 deviceInfo->connType,
986 query, sizeof(query), OIC_RSRC_DOXM_URI))
988 OIC_LOG(ERROR, TAG, "PutOwnerTransferModeToResource : Failed to generate query");
989 return OC_STACK_ERROR;
991 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
992 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
995 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
996 return OC_STACK_NO_MEMORY;
998 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
999 secPayload->securityData = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx);
1000 if (NULL == secPayload->securityData)
1002 OICFree(secPayload);
1003 OIC_LOG(ERROR, TAG, "Error while converting bin to json");
1004 return OC_STACK_ERROR;
1007 OCCallbackData cbData;
1008 cbData.cb = &OwnerTransferModeHandler;
1009 cbData.context = (void *)otmCtx;
1011 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
1012 &deviceInfo->endpoint, (OCPayload*)secPayload,
1013 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1014 if (res != OC_STACK_OK)
1016 OIC_LOG(ERROR, TAG, "OCStack resource error");
1019 OIC_LOG(DEBUG, TAG, "OUT PutOwnerTransferModeToResource");
1024 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1026 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1028 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1030 OIC_LOG(ERROR, TAG, "Invailed parameters");
1031 return OC_STACK_INVALID_PARAM;
1034 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1035 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1036 if(!PMGenerateQuery(false,
1037 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1038 deviceInfo->connType,
1039 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1041 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1042 return OC_STACK_ERROR;
1044 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1046 OCCallbackData cbData;
1047 cbData.cb = &ListMethodsHandler;
1048 cbData.context = (void *)otmCtx;
1050 OCStackResult res = OCDoResource(NULL, OC_REST_GET, query, NULL, NULL,
1051 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1052 if (res != OC_STACK_OK)
1054 OIC_LOG(ERROR, TAG, "OCStack resource error");
1057 OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1062 static OCStackResult PutOwnerUuid(OTMContext_t* otmCtx)
1064 OIC_LOG(DEBUG, TAG, "IN PutOwnerUuid");
1066 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1068 OIC_LOG(ERROR, TAG, "Invailed parameters");
1069 return OC_STACK_INVALID_PARAM;
1072 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1073 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1074 if(!PMGenerateQuery(true,
1075 deviceInfo->endpoint.addr, deviceInfo->securePort,
1076 deviceInfo->connType,
1077 query, sizeof(query), OIC_RSRC_DOXM_URI))
1079 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1080 return OC_STACK_ERROR;
1082 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1084 //PUT PT's uuid to new device
1085 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1088 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1089 return OC_STACK_NO_MEMORY;
1091 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1092 secPayload->securityData =
1093 g_OTMDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(otmCtx);
1094 if (NULL == secPayload->securityData)
1096 OICFree(secPayload);
1097 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1098 return OC_STACK_INVALID_PARAM;
1100 OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData);
1102 OCCallbackData cbData;
1103 cbData.cb = &OwnerUuidUpdateHandler;
1104 cbData.context = (void *)otmCtx;
1107 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1108 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1109 if (res != OC_STACK_OK)
1111 OIC_LOG(ERROR, TAG, "OCStack resource error");
1114 OIC_LOG(DEBUG, TAG, "OUT PutOwnerUuid");
1119 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx)
1121 OIC_LOG(DEBUG, TAG, "IN PutOwnershipInformation");
1123 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1125 OIC_LOG(ERROR, TAG, "Invailed parameters");
1126 return OC_STACK_INVALID_PARAM;
1129 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1130 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1131 if(!PMGenerateQuery(true,
1132 deviceInfo->endpoint.addr, deviceInfo->securePort,
1133 deviceInfo->connType,
1134 query, sizeof(query), OIC_RSRC_DOXM_URI))
1136 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1137 return OC_STACK_ERROR;
1139 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1141 //OwnershipInformationHandler
1142 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1145 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1146 return OC_STACK_NO_MEMORY;
1149 otmCtx->selectedDeviceInfo->doxm->owned = true;
1150 secPayload->securityData = BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
1151 if (NULL == secPayload->securityData)
1153 OICFree(secPayload);
1154 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1155 return OC_STACK_INVALID_PARAM;
1157 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1159 OCCallbackData cbData;
1160 cbData.cb = &OwnershipInformationHandler;
1161 cbData.context = (void *)otmCtx;
1164 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1165 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1166 if (res != OC_STACK_OK)
1168 OIC_LOG(ERROR, TAG, "OCStack resource error");
1171 OIC_LOG(DEBUG, TAG, "OUT PutOwnershipInformation");
1176 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx)
1178 OIC_LOG(DEBUG, TAG, "IN PutUpdateOperationMode");
1180 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1182 return OC_STACK_INVALID_PARAM;
1185 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1186 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1187 if(!PMGenerateQuery(false,
1188 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1189 deviceInfo->connType,
1190 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1192 OIC_LOG(ERROR, TAG, "PutUpdateOperationMode : Failed to generate query");
1193 return OC_STACK_ERROR;
1195 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1197 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1200 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1201 return OC_STACK_NO_MEMORY;
1203 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1204 secPayload->securityData = BinToPstatJSON(deviceInfo->pstat);
1205 if (NULL == secPayload->securityData)
1207 OICFree(secPayload);
1208 OIC_LOG(ERROR, TAG, "Error while converting pstat bin to json");
1209 return OC_STACK_INVALID_PARAM;
1212 OCCallbackData cbData;
1213 cbData.cb = &OperationModeUpdateHandler;
1214 cbData.context = (void *)otmCtx;
1216 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1217 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1218 if (res != OC_STACK_OK)
1220 OIC_LOG(ERROR, TAG, "OCStack resource error");
1223 OIC_LOG(DEBUG, TAG, "OUT PutUpdateOperationMode");
1228 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
1230 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
1231 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1232 otmCtx->selectedDeviceInfo = selectedDevice;
1234 //Set to the lowest level OxM, and then find more higher level OxM.
1235 OCStackResult res = SelectProvisioningMethod(selectedDevice->doxm->oxm,
1236 selectedDevice->doxm->oxmLen,
1237 &selectedDevice->doxm->oxmSel);
1238 if(OC_STACK_OK != res)
1240 OIC_LOG(ERROR, TAG, "Failed to select the provisioning method");
1241 SetResult(otmCtx, res);
1244 OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
1246 //Send Req: PUT /oic/sec/doxm [{..."OxmSel" :g_OTMDatas[Index of Selected OxM].OXMString,...}]
1247 res = PutOwnerTransferModeToResource(otmCtx);
1248 if(OC_STACK_OK != res)
1250 OIC_LOG(WARNING, TAG, "Failed to select the provisioning method");
1251 SetResult(otmCtx, res);
1255 //Register DTLS event handler to catch the dtls event while handshake
1256 if(CA_STATUS_OK != CARegisterDTLSHandshakeCallback(DTLSHandshakeCB))
1258 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register DTLS handshake callback.");
1261 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
1267 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
1269 OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
1273 OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
1274 return OC_STACK_INVALID_PARAM;
1276 if(oxmType >= OIC_OXM_COUNT)
1278 OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
1279 return OC_STACK_INVALID_PARAM;
1282 g_OTMDatas[oxmType].loadSecretCB= data->loadSecretCB;
1283 g_OTMDatas[oxmType].createSecureSessionCB = data->createSecureSessionCB;
1284 g_OTMDatas[oxmType].createSelectOxmPayloadCB = data->createSelectOxmPayloadCB;
1285 g_OTMDatas[oxmType].createOwnerTransferPayloadCB = data->createOwnerTransferPayloadCB;
1287 OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
1293 * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
1295 OCStackResult OTMDoOwnershipTransfer(void* ctx,
1296 OCProvisionDev_t *selectedDevicelist,
1297 OCProvisionResultCB resultCallback)
1299 OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
1301 if (NULL == selectedDevicelist)
1303 return OC_STACK_INVALID_PARAM;
1305 if (NULL == resultCallback)
1307 return OC_STACK_INVALID_CALLBACK;
1310 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
1313 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
1314 return OC_STACK_NO_MEMORY;
1316 otmCtx->ctxResultCallback = resultCallback;
1317 otmCtx->ctxHasError = false;
1318 otmCtx->userCtx = ctx;
1319 OCProvisionDev_t* pCurDev = selectedDevicelist;
1321 //Counting number of selected devices.
1322 otmCtx->ctxResultArraySize = 0;
1323 while(NULL != pCurDev)
1325 otmCtx->ctxResultArraySize++;
1326 pCurDev = pCurDev->next;
1329 otmCtx->ctxResultArray =
1330 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
1331 if(NULL == otmCtx->ctxResultArray)
1333 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
1335 return OC_STACK_NO_MEMORY;
1337 pCurDev = selectedDevicelist;
1339 OCStackResult res = OC_STACK_OK;
1340 //Fill the device UUID for result array.
1341 for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
1343 //Checking duplication of Device ID.
1344 bool isDuplicate = true;
1345 res = PDMIsDuplicateDevice(&pCurDev->doxm->deviceID, &isDuplicate);
1346 if (OC_STACK_OK != res)
1352 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Device ID is duplicated");
1353 res = OC_STACK_INVALID_PARAM;
1356 memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
1357 pCurDev->doxm->deviceID.id,
1359 otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
1360 pCurDev = pCurDev->next;
1363 StartOwnershipTransfer(otmCtx, selectedDevicelist);
1365 OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
1369 OICFree(otmCtx->ctxResultArray);
1375 OCStackResult PutProvisioningStatus(OTMContext_t* otmCtx)
1377 OIC_LOG(INFO, TAG, "IN PutProvisioningStatus");
1381 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1382 return OC_STACK_INVALID_PARAM;
1384 if(!otmCtx->selectedDeviceInfo)
1386 OIC_LOG(ERROR, TAG, "Can't find device information in OTMContext");
1388 return OC_STACK_INVALID_PARAM;
1391 otmCtx->selectedDeviceInfo->pstat->tm = NORMAL;
1392 otmCtx->selectedDeviceInfo->pstat->cm = PROVISION_ACLS | PROVISION_CREDENTIALS |
1393 SECURITY_MANAGEMENT_SERVICES | BOOTSTRAP_SERVICE;
1394 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1397 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1398 return OC_STACK_NO_MEMORY;
1400 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1401 secPayload->securityData = BinToPstatJSON(otmCtx->selectedDeviceInfo->pstat);
1402 if (NULL == secPayload->securityData)
1404 OICFree(secPayload);
1405 SetResult(otmCtx, OC_STACK_INVALID_JSON);
1406 return OC_STACK_INVALID_JSON;
1408 OIC_LOG_V(INFO, TAG, "Created payload for commit hash: %s",secPayload->securityData);
1410 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1411 if(!PMGenerateQuery(true,
1412 otmCtx->selectedDeviceInfo->endpoint.addr,
1413 otmCtx->selectedDeviceInfo->securePort,
1414 otmCtx->selectedDeviceInfo->connType,
1415 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1417 OIC_LOG(ERROR, TAG, "PutProvisioningStatus : Failed to generate query");
1418 return OC_STACK_ERROR;
1420 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1422 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1423 cbData.cb = &ProvisioningStatusHandler;
1424 cbData.context = (void*)otmCtx;
1426 OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1427 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1428 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1429 if (ret != OC_STACK_OK)
1431 OIC_LOG(ERROR, TAG, "OCStack resource error");
1432 SetResult(otmCtx, ret);
1435 OIC_LOG(INFO, TAG, "OUT PutProvisioningStatus");