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 * Number of supported provisioning methods
73 * current version supports only one.
75 static size_t gNumOfProvisioningMethodsPT = 1;
78 * Variables for pointing the OTMContext to be used in the DTLS handshake result callback.
80 static OTMContext_t* g_otmCtx = NULL;
83 * Function to select appropriate provisioning method.
85 * @param[in] supportedMethods Array of supported methods
86 * @param[in] numberOfMethods number of supported methods
87 * @param[out] selectedMethod Selected methods
88 * @return OC_STACK_OK on success
90 static OCStackResult SelectProvisioningMethod(const OicSecOxm_t *supportedMethods,
91 size_t numberOfMethods, OicSecOxm_t *selectedMethod)
93 OIC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
95 if(numberOfMethods == 0 || !supportedMethods)
97 OIC_LOG(WARNING, TAG, "Could not find a supported OxM.");
98 return OC_STACK_ERROR;
101 *selectedMethod = supportedMethods[0];
102 for(size_t i = 0; i < numberOfMethods; i++)
104 if(*selectedMethod < supportedMethods[i])
106 *selectedMethod = supportedMethods[i];
114 * Function to select operation mode.This function will return most secure common operation mode.
116 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
117 * @param[out] selectedMode selected operation mode
118 * @return OC_STACK_OK on success
120 static void SelectOperationMode(const OCProvisionDev_t *selectedDeviceInfo,
121 OicSecDpom_t *selectedMode)
123 OIC_LOG(DEBUG, TAG, "IN SelectOperationMode");
124 *selectedMode = selectedDeviceInfo->pstat->sm[0];
125 OIC_LOG_V(DEBUG, TAG, "Selected Operation Mode = %d", *selectedMode);
129 * Function to start ownership transfer.
130 * This function will send the first request for provisioning,
131 * The next request message is sent from the response handler for this request.
133 * @param[in] ctx context value passed to callback from calling function.
134 * @param[in] selectedDevice selected device information to performing provisioning.
135 * @return OC_STACK_OK on success
137 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
140 * Function to update owner transfer mode
142 * @param[in] otmCtx Context value of ownership transfer.
143 * @return OC_STACK_OK on success
145 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx);
148 * Function to send request to resource to get its pstat resource information.
150 * @param[in] otmCtx Context value of ownership transfer.
151 * @return OC_STACK_OK on success
153 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx);
157 * Function to send uuid of owner device to new device.
158 * This function would update 'owner of doxm' as UUID for provisioning tool.
160 * @param[in] otmCtx Context value of ownership transfer.
161 * @return OC_STACK_OK on success
163 static OCStackResult PutOwnerUuid(OTMContext_t* otmCtx);
166 * Function to update the operation mode. As per the spec. Operation mode in client driven
167 * single service provisioning it will be updated to 0x3
169 * @param[in] otmCtx Context value of ownership transfer.
170 * @return OC_STACK_OK on success
172 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx);
175 * Function to update the owner credential to new device
177 * @param[in] otmCtx Context value of ownership transfer.
178 * @param[in] selectedOperationMode selected operation mode
179 * @return OC_STACK_OK on success
181 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx);
184 * Function to send ownerShip info.
185 * This function would update 'owned of doxm' as true.
187 * @param[in] otmCtx Context value of ownership transfer.
188 * @return OC_STACK_OK on success
190 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx);
193 * Function to update pstat as Ready for provisioning.
194 * This function would update 'cm' from bx0000,0010 to bx0000,0000.
196 * @param[in] ctx context value passed to callback from calling function.
197 * @param[in] selectedDevice selected device information to performing provisioning.
198 * @return OC_STACK_OK on success
200 static OCStackResult PutProvisioningStatus(OTMContext_t* otmCtx);
203 * Function to update pstat as Ready for Normal Operation.
204 * This function would update 'isop' from false to true.
206 * @param[in] ctx context value passed to callback from calling function.
207 * @param[in] selectedDevice selected device information to performing provisioning.
208 * @return OC_STACK_OK on success
210 static OCStackResult PutNormalOperationStatus(OTMContext_t* otmCtx);
212 static bool IsComplete(OTMContext_t* otmCtx)
214 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
216 if(OC_STACK_CONTINUE == otmCtx->ctxResultArray[i].res)
226 * Function to save the result of provisioning.
228 * @param[in,out] otmCtx Context value of ownership transfer.
229 * @param[in] res result of provisioning
231 static void SetResult(OTMContext_t* otmCtx, const OCStackResult res)
233 OIC_LOG_V(DEBUG, TAG, "IN SetResult : %d ", res);
237 OIC_LOG(WARNING, TAG, "OTMContext is NULL");
241 if(otmCtx->selectedDeviceInfo)
243 //Revert psk_info callback and new deivce uuid in case of random PIN OxM
244 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
246 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
248 OIC_LOG(WARNING, TAG, "Failed to revert is DTLS credential handler.");
250 OicUuid_t emptyUuid = { .id={0}};
251 SetUuidForRandomPinOxm(&emptyUuid);
254 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
256 if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
257 otmCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
259 otmCtx->ctxResultArray[i].res = res;
260 if(OC_STACK_OK != res)
262 otmCtx->ctxHasError = true;
269 //If all request is completed, invoke the user callback.
270 if(IsComplete(otmCtx))
272 otmCtx->ctxResultCallback(otmCtx->userCtx, otmCtx->ctxResultArraySize,
273 otmCtx->ctxResultArray, otmCtx->ctxHasError);
274 OICFree(otmCtx->ctxResultArray);
279 if(OC_STACK_OK != StartOwnershipTransfer(otmCtx,
280 otmCtx->selectedDeviceInfo->next))
282 OIC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
287 OIC_LOG(DEBUG, TAG, "OUT SetResult");
291 * Function to handle the handshake result in OTM.
292 * This function will be invoked after DTLS handshake
293 * @param endPoint [IN] The remote endpoint.
294 * @param errorInfo [IN] Error information from the endpoint.
297 void DTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
299 if(NULL != g_otmCtx && NULL != g_otmCtx->selectedDeviceInfo &&
300 NULL != endpoint && NULL != info)
302 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
303 endpoint->addr, endpoint->port, info->result);
305 OicSecDoxm_t* newDevDoxm = g_otmCtx->selectedDeviceInfo->doxm;
307 if(NULL != newDevDoxm)
309 OicUuid_t emptyUuid = {.id={0}};
311 //Make sure the address matches.
312 if(strncmp(g_otmCtx->selectedDeviceInfo->endpoint.addr,
314 sizeof(endpoint->addr)) == 0 &&
315 g_otmCtx->selectedDeviceInfo->securePort == endpoint->port)
317 OCStackResult res = OC_STACK_ERROR;
319 //If temporal secure sesstion established successfully
320 if(CA_STATUS_OK == info->result &&
321 false == newDevDoxm->owned &&
322 memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) == 0)
324 //Send request : PUT /oic/sec/doxm [{... , "devowner":"PT's UUID"}]
325 res = PutOwnerUuid(g_otmCtx);
326 if(OC_STACK_OK != res)
328 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
329 SetResult(g_otmCtx, res);
332 //In case of authentication failure
333 else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
335 //in case of error from owner credential
336 if(memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) != 0 &&
337 true == newDevDoxm->owned)
339 OIC_LOG(ERROR, TAG, "The owner credential may incorrect.");
341 if(OC_STACK_OK != RemoveCredential(&(newDevDoxm->deviceID)))
343 OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
345 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
347 //in case of error from wrong PIN, re-start the ownership transfer
348 else if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
350 OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
352 memcpy(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
353 newDevDoxm->owned = false;
354 g_otmCtx->attemptCnt++;
356 if(WRONG_PIN_MAX_ATTEMP > g_otmCtx->attemptCnt)
358 res = StartOwnershipTransfer(g_otmCtx, g_otmCtx->selectedDeviceInfo);
359 if(OC_STACK_OK != res)
361 SetResult(g_otmCtx, res);
362 OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
367 OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
368 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
373 OIC_LOG(ERROR, TAG, "Failed to establish secure session.");
374 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
383 * Function to save ownerPSK at provisioning tool end.
385 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
386 * @return OC_STACK_OK on success
388 static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
390 OIC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
392 OCStackResult res = OC_STACK_ERROR;
394 CAEndpoint_t endpoint;
395 memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
396 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
397 endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
398 endpoint.port = selectedDeviceInfo->securePort;
400 OicUuid_t ptDeviceID = {.id={0}};
401 if (OC_STACK_OK != GetDoxmDeviceID(&ptDeviceID))
403 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
407 uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
408 OicSecKey_t ownerKey = {ownerPSK, OWNER_PSK_LENGTH_128};
410 //Generating OwnerPSK
411 CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
412 (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
413 strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
414 ptDeviceID.id, sizeof(ptDeviceID.id),
415 selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
416 ownerPSK, OWNER_PSK_LENGTH_128);
418 if (CA_STATUS_OK == pskRet)
420 OIC_LOG(INFO, TAG,"ownerPSK dump:\n");
421 OIC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
422 //Generating new credential for provisioning tool
423 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
424 SYMMETRIC_PAIR_WISE_KEY, NULL,
425 &ownerKey, &ptDeviceID);
426 VERIFY_NON_NULL(TAG, cred, ERROR);
428 res = AddCredential(cred);
429 if(res != OC_STACK_OK)
431 DeleteCredList(cred);
437 OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
440 OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
446 * Callback handler for OwnerShipTransferModeHandler API.
448 * @param[in] ctx ctx value passed to callback from calling function.
449 * @param[in] UNUSED handle to an invocation
450 * @param[in] clientResponse Response from queries to remote servers.
451 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
452 * and OC_STACK_KEEP_TRANSACTION to keep it.
454 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
455 OCClientResponse *clientResponse)
457 OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
459 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
460 VERIFY_NON_NULL(TAG, ctx, WARNING);
462 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
464 if(clientResponse->result == OC_STACK_OK)
466 OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
467 //Send request : GET /oic/sec/pstat
468 OCStackResult res = GetProvisioningStatusResource(otmCtx);
469 if(OC_STACK_OK != res)
471 OIC_LOG(WARNING, TAG, "Failed to get pstat information");
472 SetResult(otmCtx, res);
477 OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
478 clientResponse->result);
479 SetResult(otmCtx, clientResponse->result);
482 OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
485 return OC_STACK_DELETE_TRANSACTION;
489 * Callback handler for ProvisioningStatusResouceHandler API.
491 * @param[in] ctx ctx value passed to callback from calling function.
492 * @param[in] UNUSED handle to an invocation
493 * @param[in] clientResponse Response from queries to remote servers.
494 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
495 * and OC_STACK_KEEP_TRANSACTION to keep it.
497 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
498 OCClientResponse *clientResponse)
500 OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
502 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
503 VERIFY_NON_NULL(TAG, ctx, WARNING);
505 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
507 if (OC_STACK_OK == clientResponse->result)
509 if (NULL == clientResponse->payload)
511 OIC_LOG(INFO, TAG, "Skiping Null payload");
512 SetResult(otmCtx, OC_STACK_ERROR);
513 return OC_STACK_DELETE_TRANSACTION;
516 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
518 OIC_LOG(INFO, TAG, "Unknown payload type");
519 SetResult(otmCtx, OC_STACK_ERROR);
520 return OC_STACK_DELETE_TRANSACTION;
522 OicSecPstat_t* pstat = NULL;
523 OCStackResult result = CBORPayloadToPstat(
524 ((OCSecurityPayload*)clientResponse->payload)->securityData1,
525 ((OCSecurityPayload*)clientResponse->payload)->payloadSize,
527 if(NULL == pstat && result != OC_STACK_OK)
529 OIC_LOG(ERROR, TAG, "Error while converting cbor to pstat.");
530 SetResult(otmCtx, OC_STACK_ERROR);
531 return OC_STACK_DELETE_TRANSACTION;
533 if(false == (TAKE_OWNER & pstat->cm))
535 OIC_LOG(ERROR, TAG, "Device pairing mode enabling owner transfer operations is disabled");
536 SetResult(otmCtx, OC_STACK_ERROR);
537 return OC_STACK_DELETE_TRANSACTION;
539 otmCtx->selectedDeviceInfo->pstat = pstat;
541 //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
542 SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
544 //Send request : PUT /oic/sec/pstat [{"om":"bx11", .. }]
545 OCStackResult res = PutUpdateOperationMode(otmCtx);
546 if (OC_STACK_OK != res)
548 OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
549 SetResult(otmCtx, res);
554 OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
555 clientResponse->result);
556 SetResult(otmCtx, clientResponse->result);
559 OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
561 return OC_STACK_DELETE_TRANSACTION;
565 * Response handler for update owner uuid request.
567 * @param[in] ctx ctx value passed to callback from calling function.
568 * @param[in] UNUSED handle to an invocation
569 * @param[in] clientResponse Response from queries to remote servers.
570 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
571 * and OC_STACK_KEEP_TRANSACTION to keep it.
573 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
574 OCClientResponse *clientResponse)
576 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
577 VERIFY_NON_NULL(TAG, ctx, WARNING);
579 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
581 OCStackResult res = OC_STACK_OK;
582 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
584 if(OC_STACK_OK == clientResponse->result)
586 if(otmCtx && otmCtx->selectedDeviceInfo)
588 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
589 if(OC_STACK_OK != res)
591 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
592 SetResult(otmCtx, res);
593 return OC_STACK_DELETE_TRANSACTION;
596 //PUT owner credential to new device according to security spec B.
597 res = PutOwnerCredential(otmCtx);
598 if(OC_STACK_OK != res)
601 "OwnerUuidUpdateHandler:Failed to send PUT request for onwer credential");
602 SetResult(otmCtx, res);
603 return OC_STACK_DELETE_TRANSACTION;
609 res = clientResponse->result;
610 OIC_LOG_V(ERROR, TAG, "OwnerUuidHandler : Unexpected result %d", res);
611 SetResult(otmCtx, res);
614 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
617 return OC_STACK_DELETE_TRANSACTION;
621 * Response handler for update operation mode.
623 * @param[in] ctx ctx value passed to callback from calling function.
624 * @param[in] UNUSED handle to an invocation
625 * @param[in] clientResponse Response from queries to remote servers.
626 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
627 * and OC_STACK_KEEP_TRANSACTION to keep it.
629 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
630 OCClientResponse *clientResponse)
632 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
634 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
635 VERIFY_NON_NULL(TAG, ctx, WARNING);
637 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
639 if (OC_STACK_OK == clientResponse->result)
641 OCStackResult res = OC_STACK_ERROR;
642 OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
644 //Load secret for temporal secure session.
645 if(g_OTMDatas[selOxm].loadSecretCB)
647 res = g_OTMDatas[selOxm].loadSecretCB(otmCtx);
648 if(OC_STACK_OK != res)
650 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
651 SetResult(otmCtx, res);
652 return OC_STACK_DELETE_TRANSACTION;
656 //It will be used in handshake event handler
659 //Try DTLS handshake to generate secure session
660 if(g_OTMDatas[selOxm].createSecureSessionCB)
662 res = g_OTMDatas[selOxm].createSecureSessionCB(otmCtx);
663 if(OC_STACK_OK != res)
665 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
666 SetResult(otmCtx, res);
667 return OC_STACK_DELETE_TRANSACTION;
673 OIC_LOG(ERROR, TAG, "Error while update operation mode");
674 SetResult(otmCtx, clientResponse->result);
677 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
680 return OC_STACK_DELETE_TRANSACTION;
684 * Response handler for update owner crendetial request.
686 * @param[in] ctx ctx value passed to callback from calling function.
687 * @param[in] UNUSED handle to an invocation
688 * @param[in] clientResponse Response from queries to remote servers.
689 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
690 * and OC_STACK_KEEP_TRANSACTION to keep it.
692 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
693 OCClientResponse *clientResponse)
695 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
696 VERIFY_NON_NULL(TAG, ctx, WARNING);
698 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
700 OCStackResult res = OC_STACK_OK;
701 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
703 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
705 if(otmCtx && otmCtx->selectedDeviceInfo)
707 //Close the temporal secure session to verify the owner credential
708 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
709 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
710 CAResult_t caResult = CACloseDtlsSession(endpoint);
711 if(CA_STATUS_OK != caResult)
713 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
714 SetResult(otmCtx, caResult);
715 return OC_STACK_DELETE_TRANSACTION;
719 * If we select NULL cipher,
720 * client will select appropriate cipher suite according to server's cipher-suite list.
722 caResult = CASelectCipherSuite(TLS_NULL_WITH_NULL_NULL);
723 if(CA_STATUS_OK != caResult)
725 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
726 SetResult(otmCtx, caResult);
727 return OC_STACK_DELETE_TRANSACTION;
731 * in case of random PIN based OxM,
732 * revert get_psk_info callback of tinyDTLS to use owner credential.
734 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
736 OicUuid_t emptyUuid = { .id={0}};
737 SetUuidForRandomPinOxm(&emptyUuid);
739 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
741 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
742 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
743 return OC_STACK_DELETE_TRANSACTION;
747 //PUT /oic/sec/doxm [{ ..., "owned":"TRUE" }]
748 res = PutOwnershipInformation(otmCtx);
749 if(OC_STACK_OK != res)
751 OIC_LOG(ERROR, TAG, "Failed to put ownership information to new device");
752 SetResult(otmCtx, res);
753 return OC_STACK_DELETE_TRANSACTION;
759 res = clientResponse->result;
760 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
761 SetResult(otmCtx, res);
764 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
767 return OC_STACK_DELETE_TRANSACTION;
772 * Response handler for update owner information request.
774 * @param[in] ctx ctx value passed to callback from calling function.
775 * @param[in] UNUSED handle to an invocation
776 * @param[in] clientResponse Response from queries to remote servers.
777 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
778 * and OC_STACK_KEEP_TRANSACTION to keep it.
780 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
781 OCClientResponse *clientResponse)
783 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
784 VERIFY_NON_NULL(TAG, ctx, WARNING);
786 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
788 OCStackResult res = OC_STACK_OK;
789 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
791 if(OC_STACK_OK == clientResponse->result)
793 if(otmCtx && otmCtx->selectedDeviceInfo)
795 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
796 OIC_LOG(INFO, TAG, "Set Ready for provisioning state .");
798 res = PutProvisioningStatus(otmCtx);
799 if(OC_STACK_OK != res)
801 OIC_LOG(ERROR, TAG, "Failed to update pstat");
802 SetResult(otmCtx, res);
808 res = clientResponse->result;
809 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
810 SetResult(otmCtx, res);
813 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
816 return OC_STACK_DELETE_TRANSACTION;
820 * Response handler of update provisioning status.
822 * @param[in] ctx ctx value passed to callback from calling function.
823 * @param[in] UNUSED handle to an invocation
824 * @param[in] clientResponse Response from queries to remote servers.
825 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
826 * and OC_STACK_KEEP_TRANSACTION to keep it.
828 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
829 OCClientResponse *clientResponse)
831 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
833 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
834 VERIFY_NON_NULL(TAG, ctx, ERROR);
836 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
838 OCStackResult res = OC_STACK_OK;
840 if(OC_STACK_OK == clientResponse->result)
842 if(otmCtx && otmCtx->selectedDeviceInfo)
844 OIC_LOG(INFO, TAG, "Device state is in Ready for Provisionig.");
846 res = PutNormalOperationStatus(otmCtx);
847 if(OC_STACK_OK != res)
849 OIC_LOG(ERROR, TAG, "Failed to update pstat");
850 SetResult(otmCtx, res);
856 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
857 clientResponse->result);
858 SetResult(otmCtx, clientResponse->result);
862 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
863 return OC_STACK_DELETE_TRANSACTION;
867 * Response handler of update provisioning status to Ready for Normal..
869 * @param[in] ctx ctx value passed to callback from calling function.
870 * @param[in] UNUSED handle to an invocation
871 * @param[in] clientResponse Response from queries to remote servers.
872 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
873 * and OC_STACK_KEEP_TRANSACTION to keep it.
875 static OCStackApplicationResult ReadyForNomalStatusHandler(void *ctx, OCDoHandle UNUSED,
876 OCClientResponse *clientResponse)
878 OIC_LOG_V(INFO, TAG, "IN ReadyForNomalStatusHandler.");
880 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
881 VERIFY_NON_NULL(TAG, ctx, ERROR);
883 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
886 if (OC_STACK_OK == clientResponse->result)
888 OIC_LOG(INFO, TAG, "Device state is in Ready for Normal Operation.");
889 OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
890 if (OC_STACK_OK == res)
892 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
893 SetResult(otmCtx, OC_STACK_OK);
894 return OC_STACK_DELETE_TRANSACTION;
898 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
903 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
904 clientResponse->result);
905 SetResult(otmCtx, clientResponse->result);
909 OIC_LOG_V(INFO, TAG, "OUT ReadyForNomalStatusHandler.");
910 return OC_STACK_DELETE_TRANSACTION;
913 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx)
915 OIC_LOG(DEBUG, TAG, "IN PutOwnerCredential");
917 if(!otmCtx || !otmCtx->selectedDeviceInfo)
919 OIC_LOG(ERROR, TAG, "Invalid parameters");
920 return OC_STACK_INVALID_PARAM;
923 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
924 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
926 if(!PMGenerateQuery(true,
927 deviceInfo->endpoint.addr, deviceInfo->securePort,
928 deviceInfo->connType,
929 query, sizeof(query), OIC_RSRC_CRED_URI))
931 OIC_LOG(ERROR, TAG, "PutOwnerCredential : Failed to generate query");
932 return OC_STACK_ERROR;
934 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
935 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
938 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
939 return OC_STACK_NO_MEMORY;
942 //Generate owner credential for new device
943 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
944 OicSecCred_t* ownerCredential = GetCredResourceData(&(deviceInfo->doxm->deviceID));
947 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
948 return OC_STACK_NO_RESOURCE;
951 OicUuid_t credSubjectId = {.id={0}};
952 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
954 OicSecCred_t newCredential;
955 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
956 newCredential.next = NULL;
958 //Set subject ID as PT's ID
959 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
961 //Fill private data as empty string
962 newCredential.privateData.data = NULL;
963 newCredential.privateData.len = 0;
965 newCredential.publicData.data = NULL;
966 newCredential.publicData.len = 0;
969 //Send owner credential to new device : PUT /oic/sec/cred [ owner credential ]
970 if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData1, &secPayload->payloadSize))
973 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
974 return OC_STACK_ERROR;
976 OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData1);
978 OCCallbackData cbData;
979 cbData.cb = &OwnerCredentialHandler;
980 cbData.context = (void *)otmCtx;
982 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
983 &deviceInfo->endpoint, (OCPayload*)secPayload,
984 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
985 if (res != OC_STACK_OK)
987 OIC_LOG(ERROR, TAG, "OCStack resource error");
992 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
993 return OC_STACK_NO_RESOURCE;
996 OIC_LOG(DEBUG, TAG, "OUT PutOwnerCredential");
1001 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx)
1003 OIC_LOG(DEBUG, TAG, "IN PutOwnerTransferModeToResource");
1005 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1007 OIC_LOG(ERROR, TAG, "Invalid parameters");
1008 return OC_STACK_INVALID_PARAM;
1011 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1012 OicSecOxm_t selectedOxm = deviceInfo->doxm->oxmSel;
1013 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1015 if(!PMGenerateQuery(false,
1016 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1017 deviceInfo->connType,
1018 query, sizeof(query), OIC_RSRC_DOXM_URI))
1020 OIC_LOG(ERROR, TAG, "PutOwnerTransferModeToResource : Failed to generate query");
1021 return OC_STACK_ERROR;
1023 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1024 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1027 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1028 return OC_STACK_NO_MEMORY;
1030 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1031 OCStackResult res = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx,
1032 &secPayload->securityData1, &secPayload->payloadSize);
1033 if (OC_STACK_OK != res && NULL == secPayload->securityData1)
1035 OCPayloadDestroy((OCPayload *)secPayload);
1036 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor");
1037 return OC_STACK_ERROR;
1040 OCCallbackData cbData;
1041 cbData.cb = &OwnerTransferModeHandler;
1042 cbData.context = (void *)otmCtx;
1044 res = OCDoResource(NULL, OC_REST_PUT, query,
1045 &deviceInfo->endpoint, (OCPayload *)secPayload,
1046 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1047 if (res != OC_STACK_OK)
1049 OIC_LOG(ERROR, TAG, "OCStack resource error");
1052 OIC_LOG(DEBUG, TAG, "OUT PutOwnerTransferModeToResource");
1057 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1059 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1061 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1063 OIC_LOG(ERROR, TAG, "Invailed parameters");
1064 return OC_STACK_INVALID_PARAM;
1067 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1068 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1069 if(!PMGenerateQuery(false,
1070 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1071 deviceInfo->connType,
1072 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1074 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1075 return OC_STACK_ERROR;
1077 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1079 OCCallbackData cbData;
1080 cbData.cb = &ListMethodsHandler;
1081 cbData.context = (void *)otmCtx;
1083 OCStackResult res = OCDoResource(NULL, OC_REST_GET, query, NULL, NULL,
1084 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1085 if (res != OC_STACK_OK)
1087 OIC_LOG(ERROR, TAG, "OCStack resource error");
1090 OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1095 static OCStackResult PutOwnerUuid(OTMContext_t* otmCtx)
1097 OIC_LOG(DEBUG, TAG, "IN PutOwnerUuid");
1099 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1101 OIC_LOG(ERROR, TAG, "Invailed parameters");
1102 return OC_STACK_INVALID_PARAM;
1105 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1106 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1107 if(!PMGenerateQuery(true,
1108 deviceInfo->endpoint.addr, deviceInfo->securePort,
1109 deviceInfo->connType,
1110 query, sizeof(query), OIC_RSRC_DOXM_URI))
1112 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1113 return OC_STACK_ERROR;
1115 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1117 //PUT PT's uuid to new device
1118 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1121 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1122 return OC_STACK_NO_MEMORY;
1124 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1125 OCStackResult res = g_OTMDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(
1126 otmCtx, &secPayload->securityData1, &secPayload->payloadSize);
1127 if (NULL == secPayload->securityData1)
1129 OCPayloadDestroy((OCPayload *)secPayload);
1130 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to cbor.");
1131 return OC_STACK_INVALID_PARAM;
1133 OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData1);
1135 OCCallbackData cbData;
1136 cbData.cb = &OwnerUuidUpdateHandler;
1137 cbData.context = (void *)otmCtx;
1140 res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload *)secPayload,
1141 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1142 if (res != OC_STACK_OK)
1144 OIC_LOG(ERROR, TAG, "OCStack resource error");
1147 OIC_LOG(DEBUG, TAG, "OUT PutOwnerUuid");
1152 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx)
1154 OIC_LOG(DEBUG, TAG, "IN PutOwnershipInformation");
1156 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1158 OIC_LOG(ERROR, TAG, "Invailed parameters");
1159 return OC_STACK_INVALID_PARAM;
1162 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1163 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1164 if(!PMGenerateQuery(true,
1165 deviceInfo->endpoint.addr, deviceInfo->securePort,
1166 deviceInfo->connType,
1167 query, sizeof(query), OIC_RSRC_DOXM_URI))
1169 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1170 return OC_STACK_ERROR;
1172 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1174 //OwnershipInformationHandler
1175 OCSecurityPayload *secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1178 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1179 return OC_STACK_NO_MEMORY;
1182 otmCtx->selectedDeviceInfo->doxm->owned = true;
1184 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1185 OCStackResult res = DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm,
1186 &secPayload->securityData1, &secPayload->payloadSize);
1187 if (OC_STACK_OK != res && NULL == secPayload->securityData1)
1189 OCPayloadDestroy((OCPayload *)secPayload);
1190 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1191 return OC_STACK_INVALID_PARAM;
1194 OCCallbackData cbData;
1195 cbData.cb = &OwnershipInformationHandler;
1196 cbData.context = (void *)otmCtx;
1199 res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1200 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1201 if (res != OC_STACK_OK)
1203 OIC_LOG(ERROR, TAG, "OCStack resource error");
1206 OIC_LOG(DEBUG, TAG, "OUT PutOwnershipInformation");
1211 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx)
1213 OIC_LOG(DEBUG, TAG, "IN PutUpdateOperationMode");
1215 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1217 return OC_STACK_INVALID_PARAM;
1220 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1221 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1222 if(!PMGenerateQuery(false,
1223 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1224 deviceInfo->connType,
1225 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1227 OIC_LOG(ERROR, TAG, "PutUpdateOperationMode : Failed to generate query");
1228 return OC_STACK_ERROR;
1230 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1232 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1235 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1236 return OC_STACK_NO_MEMORY;
1238 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1239 OCStackResult res = PstatToCBORPayload(deviceInfo->pstat, &secPayload->securityData1,
1240 &secPayload->payloadSize);
1241 if (OC_STACK_OK != res)
1243 OCPayloadDestroy((OCPayload *)secPayload);
1244 OIC_LOG(ERROR, TAG, "Error while converting pstat to cbor.");
1245 return OC_STACK_INVALID_PARAM;
1248 OCCallbackData cbData;
1249 cbData.cb = &OperationModeUpdateHandler;
1250 cbData.context = (void *)otmCtx;
1252 res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload *)secPayload,
1253 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1254 if (res != OC_STACK_OK)
1256 OIC_LOG(ERROR, TAG, "OCStack resource error");
1259 OIC_LOG(DEBUG, TAG, "OUT PutUpdateOperationMode");
1264 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
1266 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
1267 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1268 otmCtx->selectedDeviceInfo = selectedDevice;
1270 //Set to the lowest level OxM, and then find more higher level OxM.
1271 OCStackResult res = SelectProvisioningMethod(selectedDevice->doxm->oxm,
1272 selectedDevice->doxm->oxmLen,
1273 &selectedDevice->doxm->oxmSel);
1274 if(OC_STACK_OK != res)
1276 OIC_LOG(ERROR, TAG, "Failed to select the provisioning method");
1277 SetResult(otmCtx, res);
1280 OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
1282 //Send Req: PUT /oic/sec/doxm [{..."OxmSel" :g_OTMDatas[Index of Selected OxM].OXMString,...}]
1283 res = PutOwnerTransferModeToResource(otmCtx);
1284 if(OC_STACK_OK != res)
1286 OIC_LOG(WARNING, TAG, "Failed to select the provisioning method");
1287 SetResult(otmCtx, res);
1291 //Register DTLS event handler to catch the dtls event while handshake
1292 if(CA_STATUS_OK != CARegisterDTLSHandshakeCallback(DTLSHandshakeCB))
1294 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register DTLS handshake callback.");
1297 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
1303 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
1305 OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
1309 OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
1310 return OC_STACK_INVALID_PARAM;
1312 if(oxmType >= OIC_OXM_COUNT)
1314 OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
1315 return OC_STACK_INVALID_PARAM;
1318 g_OTMDatas[oxmType].loadSecretCB= data->loadSecretCB;
1319 g_OTMDatas[oxmType].createSecureSessionCB = data->createSecureSessionCB;
1320 g_OTMDatas[oxmType].createSelectOxmPayloadCB = data->createSelectOxmPayloadCB;
1321 g_OTMDatas[oxmType].createOwnerTransferPayloadCB = data->createOwnerTransferPayloadCB;
1323 OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
1329 * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
1331 OCStackResult OTMDoOwnershipTransfer(void* ctx,
1332 OCProvisionDev_t *selectedDevicelist,
1333 OCProvisionResultCB resultCallback)
1335 OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
1337 if (NULL == selectedDevicelist)
1339 return OC_STACK_INVALID_PARAM;
1341 if (NULL == resultCallback)
1343 return OC_STACK_INVALID_CALLBACK;
1346 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
1349 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
1350 return OC_STACK_NO_MEMORY;
1352 otmCtx->ctxResultCallback = resultCallback;
1353 otmCtx->ctxHasError = false;
1354 otmCtx->userCtx = ctx;
1355 OCProvisionDev_t* pCurDev = selectedDevicelist;
1357 //Counting number of selected devices.
1358 otmCtx->ctxResultArraySize = 0;
1359 while(NULL != pCurDev)
1361 otmCtx->ctxResultArraySize++;
1362 pCurDev = pCurDev->next;
1365 otmCtx->ctxResultArray =
1366 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
1367 if(NULL == otmCtx->ctxResultArray)
1369 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
1371 return OC_STACK_NO_MEMORY;
1373 pCurDev = selectedDevicelist;
1375 OCStackResult res = OC_STACK_OK;
1376 //Fill the device UUID for result array.
1377 for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
1379 //Checking duplication of Device ID.
1380 bool isDuplicate = true;
1381 res = PDMIsDuplicateDevice(&pCurDev->doxm->deviceID, &isDuplicate);
1382 if (OC_STACK_OK != res)
1388 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Device ID is duplicated");
1389 res = OC_STACK_INVALID_PARAM;
1392 memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
1393 pCurDev->doxm->deviceID.id,
1395 otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
1396 pCurDev = pCurDev->next;
1399 StartOwnershipTransfer(otmCtx, selectedDevicelist);
1401 OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
1405 OICFree(otmCtx->ctxResultArray);
1411 * Callback handler of SRPFinalizeProvisioning.
1413 * @param[in] ctx ctx value passed to callback from calling function.
1414 * @param[in] UNUSED handle to an invocation
1415 * @param[in] clientResponse Response from queries to remote servers.
1416 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1417 * and OC_STACK_KEEP_TRANSACTION to keep it.
1419 static OCStackApplicationResult FinalizeProvisioningCB(void *ctx, OCDoHandle UNUSED,
1420 OCClientResponse *clientResponse)
1422 OIC_LOG_V(INFO, TAG, "IN FinalizeProvisioningCB.");
1424 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1425 VERIFY_NON_NULL(TAG, ctx, ERROR);
1427 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1429 if(OC_STACK_OK == clientResponse->result)
1431 OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
1433 if (OC_STACK_OK == res)
1435 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
1436 SetResult(otmCtx, OC_STACK_OK);
1437 return OC_STACK_DELETE_TRANSACTION;
1441 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
1445 return OC_STACK_DELETE_TRANSACTION;
1449 * Callback handler of default ACL provisioning.
1451 * @param[in] ctx ctx value passed to callback from calling function.
1452 * @param[in] UNUSED handle to an invocation
1453 * @param[in] clientResponse Response from queries to remote servers.
1454 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1455 * and OC_STACK_KEEP_TRANSACTION to keep it.
1457 static OCStackApplicationResult ProvisionDefaultACLCB(void *ctx, OCDoHandle UNUSED,
1458 OCClientResponse *clientResponse)
1460 OIC_LOG_V(INFO, TAG, "IN ProvisionDefaultACLCB.");
1462 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1463 VERIFY_NON_NULL(TAG, ctx, ERROR);
1465 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
1468 if (OC_STACK_RESOURCE_CREATED == clientResponse->result)
1470 OIC_LOG_V(INFO, TAG, "Staring commit hash task.");
1471 // TODO hash currently have fixed value 0.
1472 uint16_t aclHash = 0;
1473 otmCtx->selectedDeviceInfo->pstat->commitHash = aclHash;
1474 otmCtx->selectedDeviceInfo->pstat->tm = NORMAL;
1475 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1478 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1479 return OC_STACK_NO_MEMORY;
1481 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1482 OCStackResult res = PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1483 &secPayload->securityData1, &secPayload->payloadSize);
1484 if (OC_STACK_OK != res || NULL == secPayload->securityData1)
1486 OICFree(secPayload);
1487 SetResult(otmCtx, OC_STACK_INVALID_JSON);
1488 return OC_STACK_DELETE_TRANSACTION;
1491 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1492 if(!PMGenerateQuery(true,
1493 otmCtx->selectedDeviceInfo->endpoint.addr,
1494 otmCtx->selectedDeviceInfo->securePort,
1495 otmCtx->selectedDeviceInfo->connType,
1496 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1498 OIC_LOG(ERROR, TAG, "ProvisionDefaultACLCB : Failed to generate query");
1499 return OC_STACK_ERROR;
1501 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1503 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1504 cbData.cb = &FinalizeProvisioningCB;
1505 cbData.context = (void*)otmCtx;
1507 OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1508 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1509 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1510 if (ret != OC_STACK_OK)
1512 OIC_LOG(ERROR, TAG, "OCStack resource error");
1513 SetResult(otmCtx, ret);
1518 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1519 clientResponse->result);
1520 SetResult(otmCtx, clientResponse->result);
1523 return OC_STACK_DELETE_TRANSACTION;
1526 OCStackResult PutProvisioningStatus(OTMContext_t* otmCtx)
1528 OIC_LOG(INFO, TAG, "IN PutProvisioningStatus");
1530 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1532 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1533 return OC_STACK_INVALID_PARAM;
1536 //Change the TAKE_OWNER bit of CM to 0.
1537 otmCtx->selectedDeviceInfo->pstat->cm &= (~TAKE_OWNER);
1539 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1542 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1543 return OC_STACK_NO_MEMORY;
1545 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1546 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1547 &secPayload->securityData1, &secPayload->payloadSize))
1549 OCPayloadDestroy((OCPayload *)secPayload);
1550 return OC_STACK_INVALID_JSON;
1552 OIC_LOG_V(INFO, TAG, "Created payload for chage to Provisiong state : %s",secPayload->securityData1);
1554 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1555 if(!PMGenerateQuery(true,
1556 otmCtx->selectedDeviceInfo->endpoint.addr,
1557 otmCtx->selectedDeviceInfo->securePort,
1558 otmCtx->selectedDeviceInfo->connType,
1559 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1561 OIC_LOG(ERROR, TAG, "PutProvisioningStatus : Failed to generate query");
1562 return OC_STACK_ERROR;
1564 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1566 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1567 cbData.cb = &ProvisioningStatusHandler;
1568 cbData.context = (void*)otmCtx;
1570 OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1571 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1572 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1573 if (ret != OC_STACK_OK)
1575 OIC_LOG(ERROR, TAG, "OCStack resource error");
1578 OIC_LOG(INFO, TAG, "OUT PutProvisioningStatus");
1583 OCStackResult PutNormalOperationStatus(OTMContext_t* otmCtx)
1585 OIC_LOG(INFO, TAG, "IN PutNormalOperationStatus");
1587 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1589 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1590 return OC_STACK_INVALID_PARAM;
1594 otmCtx->selectedDeviceInfo->pstat->isOp = true;
1596 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1599 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1600 return OC_STACK_NO_MEMORY;
1602 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1603 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1604 &secPayload->securityData1, &secPayload->payloadSize))
1606 OCPayloadDestroy((OCPayload *)secPayload);
1607 return OC_STACK_INVALID_JSON;
1609 OIC_LOG_V(INFO, TAG, "Created payload for chage to Provisiong state: %s",secPayload->securityData1);
1611 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1612 if(!PMGenerateQuery(true,
1613 otmCtx->selectedDeviceInfo->endpoint.addr,
1614 otmCtx->selectedDeviceInfo->securePort,
1615 otmCtx->selectedDeviceInfo->connType,
1616 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1618 OIC_LOG(ERROR, TAG, "PutNormalOperationStatus : Failed to generate query");
1619 return OC_STACK_ERROR;
1621 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1623 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1624 cbData.cb = &ReadyForNomalStatusHandler;
1625 cbData.context = (void*)otmCtx;
1627 OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1628 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1629 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1630 if (ret != OC_STACK_OK)
1632 OIC_LOG(ERROR, TAG, "OCStack resource error");
1635 OIC_LOG(INFO, TAG, "OUT PutNormalOperationStatus");