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
425 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
426 SYMMETRIC_PAIR_WISE_KEY, NULL,
427 &ownerKey, ownLen, &ptDeviceID);
428 VERIFY_NON_NULL(TAG, cred, ERROR);
430 res = AddCredential(cred);
431 if(res != OC_STACK_OK)
433 DeleteCredList(cred);
439 OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
442 OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
448 * Callback handler for OwnerShipTransferModeHandler API.
450 * @param[in] ctx ctx value passed to callback from calling function.
451 * @param[in] UNUSED handle to an invocation
452 * @param[in] clientResponse Response from queries to remote servers.
453 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
454 * and OC_STACK_KEEP_TRANSACTION to keep it.
456 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
457 OCClientResponse *clientResponse)
459 OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
461 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
462 VERIFY_NON_NULL(TAG, ctx, WARNING);
464 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
466 if(clientResponse->result == OC_STACK_OK)
468 OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
469 //Send request : GET /oic/sec/pstat
470 OCStackResult res = GetProvisioningStatusResource(otmCtx);
471 if(OC_STACK_OK != res)
473 OIC_LOG(WARNING, TAG, "Failed to get pstat information");
474 SetResult(otmCtx, res);
479 OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
480 clientResponse->result);
481 SetResult(otmCtx, clientResponse->result);
484 OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
487 return OC_STACK_DELETE_TRANSACTION;
491 * Callback handler for ProvisioningStatusResouceHandler API.
493 * @param[in] ctx ctx value passed to callback from calling function.
494 * @param[in] UNUSED handle to an invocation
495 * @param[in] clientResponse Response from queries to remote servers.
496 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
497 * and OC_STACK_KEEP_TRANSACTION to keep it.
499 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
500 OCClientResponse *clientResponse)
502 OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
504 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
505 VERIFY_NON_NULL(TAG, ctx, WARNING);
507 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
509 if (OC_STACK_OK == clientResponse->result)
511 if (NULL == clientResponse->payload)
513 OIC_LOG(INFO, TAG, "Skiping Null payload");
514 SetResult(otmCtx, OC_STACK_ERROR);
515 return OC_STACK_DELETE_TRANSACTION;
518 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
520 OIC_LOG(INFO, TAG, "Unknown payload type");
521 SetResult(otmCtx, OC_STACK_ERROR);
522 return OC_STACK_DELETE_TRANSACTION;
524 OicSecPstat_t* pstat = NULL;
525 OCStackResult result = CBORPayloadToPstat(
526 ((OCSecurityPayload*)clientResponse->payload)->securityData1,
527 ((OCSecurityPayload*)clientResponse->payload)->payloadSize,
529 if(NULL == pstat && result != OC_STACK_OK)
531 OIC_LOG(ERROR, TAG, "Error while converting cbor to pstat.");
532 SetResult(otmCtx, OC_STACK_ERROR);
533 return OC_STACK_DELETE_TRANSACTION;
535 if(false == (TAKE_OWNER & pstat->cm))
537 OIC_LOG(ERROR, TAG, "Device pairing mode enabling owner transfer operations is disabled");
538 SetResult(otmCtx, OC_STACK_ERROR);
539 return OC_STACK_DELETE_TRANSACTION;
541 otmCtx->selectedDeviceInfo->pstat = pstat;
543 //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
544 SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
546 //Send request : PUT /oic/sec/pstat [{"om":"bx11", .. }]
547 OCStackResult res = PutUpdateOperationMode(otmCtx);
548 if (OC_STACK_OK != res)
550 OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
551 SetResult(otmCtx, res);
556 OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
557 clientResponse->result);
558 SetResult(otmCtx, clientResponse->result);
561 OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
563 return OC_STACK_DELETE_TRANSACTION;
567 * Response handler for update owner uuid request.
569 * @param[in] ctx ctx value passed to callback from calling function.
570 * @param[in] UNUSED handle to an invocation
571 * @param[in] clientResponse Response from queries to remote servers.
572 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
573 * and OC_STACK_KEEP_TRANSACTION to keep it.
575 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
576 OCClientResponse *clientResponse)
578 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
579 VERIFY_NON_NULL(TAG, ctx, WARNING);
581 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
583 OCStackResult res = OC_STACK_OK;
584 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
586 if(OC_STACK_OK == clientResponse->result)
588 if(otmCtx && otmCtx->selectedDeviceInfo)
590 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
591 if(OC_STACK_OK != res)
593 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
594 SetResult(otmCtx, res);
595 return OC_STACK_DELETE_TRANSACTION;
598 //PUT owner credential to new device according to security spec B.
599 res = PutOwnerCredential(otmCtx);
600 if(OC_STACK_OK != res)
603 "OwnerUuidUpdateHandler:Failed to send PUT request for onwer credential");
604 SetResult(otmCtx, res);
605 return OC_STACK_DELETE_TRANSACTION;
611 res = clientResponse->result;
612 OIC_LOG_V(ERROR, TAG, "OwnerUuidHandler : Unexpected result %d", res);
613 SetResult(otmCtx, res);
616 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
619 return OC_STACK_DELETE_TRANSACTION;
623 * Response handler for update operation mode.
625 * @param[in] ctx ctx value passed to callback from calling function.
626 * @param[in] UNUSED handle to an invocation
627 * @param[in] clientResponse Response from queries to remote servers.
628 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
629 * and OC_STACK_KEEP_TRANSACTION to keep it.
631 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
632 OCClientResponse *clientResponse)
634 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
636 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
637 VERIFY_NON_NULL(TAG, ctx, WARNING);
639 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
641 if (OC_STACK_OK == clientResponse->result)
643 OCStackResult res = OC_STACK_ERROR;
644 OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
646 //Load secret for temporal secure session.
647 if(g_OTMDatas[selOxm].loadSecretCB)
649 res = g_OTMDatas[selOxm].loadSecretCB(otmCtx);
650 if(OC_STACK_OK != res)
652 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
653 SetResult(otmCtx, res);
654 return OC_STACK_DELETE_TRANSACTION;
658 //It will be used in handshake event handler
661 //Try DTLS handshake to generate secure session
662 if(g_OTMDatas[selOxm].createSecureSessionCB)
664 res = g_OTMDatas[selOxm].createSecureSessionCB(otmCtx);
665 if(OC_STACK_OK != res)
667 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
668 SetResult(otmCtx, res);
669 return OC_STACK_DELETE_TRANSACTION;
675 OIC_LOG(ERROR, TAG, "Error while update operation mode");
676 SetResult(otmCtx, clientResponse->result);
679 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
682 return OC_STACK_DELETE_TRANSACTION;
686 * Response handler for update owner crendetial request.
688 * @param[in] ctx ctx value passed to callback from calling function.
689 * @param[in] UNUSED handle to an invocation
690 * @param[in] clientResponse Response from queries to remote servers.
691 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
692 * and OC_STACK_KEEP_TRANSACTION to keep it.
694 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
695 OCClientResponse *clientResponse)
697 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
698 VERIFY_NON_NULL(TAG, ctx, WARNING);
700 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
702 OCStackResult res = OC_STACK_OK;
703 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
705 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
707 if(otmCtx && otmCtx->selectedDeviceInfo)
709 //Close the temporal secure session to verify the owner credential
710 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
711 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
712 CAResult_t caResult = CACloseDtlsSession(endpoint);
713 if(CA_STATUS_OK != caResult)
715 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
716 SetResult(otmCtx, caResult);
717 return OC_STACK_DELETE_TRANSACTION;
721 * If we select NULL cipher,
722 * client will select appropriate cipher suite according to server's cipher-suite list.
724 caResult = CASelectCipherSuite(TLS_NULL_WITH_NULL_NULL);
725 if(CA_STATUS_OK != caResult)
727 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
728 SetResult(otmCtx, caResult);
729 return OC_STACK_DELETE_TRANSACTION;
733 * in case of random PIN based OxM,
734 * revert get_psk_info callback of tinyDTLS to use owner credential.
736 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
738 OicUuid_t emptyUuid = { .id={0}};
739 SetUuidForRandomPinOxm(&emptyUuid);
741 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
743 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
744 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
745 return OC_STACK_DELETE_TRANSACTION;
749 //PUT /oic/sec/doxm [{ ..., "owned":"TRUE" }]
750 res = PutOwnershipInformation(otmCtx);
751 if(OC_STACK_OK != res)
753 OIC_LOG(ERROR, TAG, "Failed to put ownership information to new device");
754 SetResult(otmCtx, res);
755 return OC_STACK_DELETE_TRANSACTION;
761 res = clientResponse->result;
762 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
763 SetResult(otmCtx, res);
766 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
769 return OC_STACK_DELETE_TRANSACTION;
774 * Response handler for update owner information request.
776 * @param[in] ctx ctx value passed to callback from calling function.
777 * @param[in] UNUSED handle to an invocation
778 * @param[in] clientResponse Response from queries to remote servers.
779 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
780 * and OC_STACK_KEEP_TRANSACTION to keep it.
782 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
783 OCClientResponse *clientResponse)
785 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
786 VERIFY_NON_NULL(TAG, ctx, WARNING);
788 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
790 OCStackResult res = OC_STACK_OK;
791 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
793 if(OC_STACK_OK == clientResponse->result)
795 if(otmCtx && otmCtx->selectedDeviceInfo)
797 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
798 OIC_LOG(INFO, TAG, "Set Ready for provisioning state .");
800 res = PutProvisioningStatus(otmCtx);
801 if(OC_STACK_OK != res)
803 OIC_LOG(ERROR, TAG, "Failed to update pstat");
804 SetResult(otmCtx, res);
810 res = clientResponse->result;
811 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
812 SetResult(otmCtx, res);
815 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
818 return OC_STACK_DELETE_TRANSACTION;
822 * Response handler of update provisioning status.
824 * @param[in] ctx ctx value passed to callback from calling function.
825 * @param[in] UNUSED handle to an invocation
826 * @param[in] clientResponse Response from queries to remote servers.
827 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
828 * and OC_STACK_KEEP_TRANSACTION to keep it.
830 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
831 OCClientResponse *clientResponse)
833 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
835 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
836 VERIFY_NON_NULL(TAG, ctx, ERROR);
838 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
840 OCStackResult res = OC_STACK_OK;
842 if(OC_STACK_OK == clientResponse->result)
844 if(otmCtx && otmCtx->selectedDeviceInfo)
846 OIC_LOG(INFO, TAG, "Device state is in Ready for Provisionig.");
848 res = PutNormalOperationStatus(otmCtx);
849 if(OC_STACK_OK != res)
851 OIC_LOG(ERROR, TAG, "Failed to update pstat");
852 SetResult(otmCtx, res);
858 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
859 clientResponse->result);
860 SetResult(otmCtx, clientResponse->result);
864 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
865 return OC_STACK_DELETE_TRANSACTION;
869 * Response handler of update provisioning status to Ready for Normal..
871 * @param[in] ctx ctx value passed to callback from calling function.
872 * @param[in] UNUSED handle to an invocation
873 * @param[in] clientResponse Response from queries to remote servers.
874 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
875 * and OC_STACK_KEEP_TRANSACTION to keep it.
877 static OCStackApplicationResult ReadyForNomalStatusHandler(void *ctx, OCDoHandle UNUSED,
878 OCClientResponse *clientResponse)
880 OIC_LOG_V(INFO, TAG, "IN ReadyForNomalStatusHandler.");
882 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
883 VERIFY_NON_NULL(TAG, ctx, ERROR);
885 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
888 if (OC_STACK_OK == clientResponse->result)
890 OIC_LOG(INFO, TAG, "Device state is in Ready for Normal Operation.");
891 OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
892 if (OC_STACK_OK == res)
894 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
895 SetResult(otmCtx, OC_STACK_OK);
896 return OC_STACK_DELETE_TRANSACTION;
900 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
905 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
906 clientResponse->result);
907 SetResult(otmCtx, clientResponse->result);
911 OIC_LOG_V(INFO, TAG, "OUT ReadyForNomalStatusHandler.");
912 return OC_STACK_DELETE_TRANSACTION;
915 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx)
917 OIC_LOG(DEBUG, TAG, "IN PutOwnerCredential");
919 if(!otmCtx || !otmCtx->selectedDeviceInfo)
921 OIC_LOG(ERROR, TAG, "Invalid parameters");
922 return OC_STACK_INVALID_PARAM;
925 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
926 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
928 if(!PMGenerateQuery(true,
929 deviceInfo->endpoint.addr, deviceInfo->securePort,
930 deviceInfo->connType,
931 query, sizeof(query), OIC_RSRC_CRED_URI))
933 OIC_LOG(ERROR, TAG, "PutOwnerCredential : Failed to generate query");
934 return OC_STACK_ERROR;
936 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
937 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
940 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
941 return OC_STACK_NO_MEMORY;
944 //Generate owner credential for new device
945 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
946 OicSecCred_t* ownerCredential = GetCredResourceData(&(deviceInfo->doxm->deviceID));
949 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
950 return OC_STACK_NO_RESOURCE;
953 OicUuid_t credSubjectId = {.id={0}};
954 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
956 OicSecCred_t newCredential;
957 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
958 newCredential.next = NULL;
960 //Set subject ID as PT's ID
961 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
963 //Fill private data as empty string
964 newCredential.privateData.data = NULL;
965 newCredential.privateData.len = 0;
967 newCredential.publicData.data = NULL;
968 newCredential.publicData.len = 0;
971 //Send owner credential to new device : PUT /oic/sec/cred [ owner credential ]
972 if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData1, &secPayload->payloadSize))
975 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
976 return OC_STACK_ERROR;
978 OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData1);
980 OCCallbackData cbData;
981 cbData.cb = &OwnerCredentialHandler;
982 cbData.context = (void *)otmCtx;
984 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
985 &deviceInfo->endpoint, (OCPayload*)secPayload,
986 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
987 if (res != OC_STACK_OK)
989 OIC_LOG(ERROR, TAG, "OCStack resource error");
994 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
995 return OC_STACK_NO_RESOURCE;
998 OIC_LOG(DEBUG, TAG, "OUT PutOwnerCredential");
1003 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx)
1005 OIC_LOG(DEBUG, TAG, "IN PutOwnerTransferModeToResource");
1007 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1009 OIC_LOG(ERROR, TAG, "Invalid parameters");
1010 return OC_STACK_INVALID_PARAM;
1013 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1014 OicSecOxm_t selectedOxm = deviceInfo->doxm->oxmSel;
1015 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1017 if(!PMGenerateQuery(false,
1018 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1019 deviceInfo->connType,
1020 query, sizeof(query), OIC_RSRC_DOXM_URI))
1022 OIC_LOG(ERROR, TAG, "PutOwnerTransferModeToResource : Failed to generate query");
1023 return OC_STACK_ERROR;
1025 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1026 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1029 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1030 return OC_STACK_NO_MEMORY;
1032 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1033 OCStackResult res = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx,
1034 &secPayload->securityData1, &secPayload->payloadSize);
1035 if (OC_STACK_OK != res && NULL == secPayload->securityData1)
1037 OCPayloadDestroy((OCPayload *)secPayload);
1038 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor");
1039 return OC_STACK_ERROR;
1042 OCCallbackData cbData;
1043 cbData.cb = &OwnerTransferModeHandler;
1044 cbData.context = (void *)otmCtx;
1046 res = OCDoResource(NULL, OC_REST_PUT, query,
1047 &deviceInfo->endpoint, (OCPayload *)secPayload,
1048 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1049 if (res != OC_STACK_OK)
1051 OIC_LOG(ERROR, TAG, "OCStack resource error");
1054 OIC_LOG(DEBUG, TAG, "OUT PutOwnerTransferModeToResource");
1059 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1061 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1063 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1065 OIC_LOG(ERROR, TAG, "Invailed parameters");
1066 return OC_STACK_INVALID_PARAM;
1069 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1070 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1071 if(!PMGenerateQuery(false,
1072 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1073 deviceInfo->connType,
1074 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1076 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1077 return OC_STACK_ERROR;
1079 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1081 OCCallbackData cbData;
1082 cbData.cb = &ListMethodsHandler;
1083 cbData.context = (void *)otmCtx;
1085 OCStackResult res = OCDoResource(NULL, OC_REST_GET, query, NULL, NULL,
1086 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1087 if (res != OC_STACK_OK)
1089 OIC_LOG(ERROR, TAG, "OCStack resource error");
1092 OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1097 static OCStackResult PutOwnerUuid(OTMContext_t* otmCtx)
1099 OIC_LOG(DEBUG, TAG, "IN PutOwnerUuid");
1101 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1103 OIC_LOG(ERROR, TAG, "Invailed parameters");
1104 return OC_STACK_INVALID_PARAM;
1107 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1108 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1109 if(!PMGenerateQuery(true,
1110 deviceInfo->endpoint.addr, deviceInfo->securePort,
1111 deviceInfo->connType,
1112 query, sizeof(query), OIC_RSRC_DOXM_URI))
1114 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1115 return OC_STACK_ERROR;
1117 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1119 //PUT PT's uuid to new device
1120 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1123 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1124 return OC_STACK_NO_MEMORY;
1126 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1127 OCStackResult res = g_OTMDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(
1128 otmCtx, &secPayload->securityData1, &secPayload->payloadSize);
1129 if (NULL == secPayload->securityData1)
1131 OCPayloadDestroy((OCPayload *)secPayload);
1132 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to cbor.");
1133 return OC_STACK_INVALID_PARAM;
1135 OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData1);
1137 OCCallbackData cbData;
1138 cbData.cb = &OwnerUuidUpdateHandler;
1139 cbData.context = (void *)otmCtx;
1142 res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload *)secPayload,
1143 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1144 if (res != OC_STACK_OK)
1146 OIC_LOG(ERROR, TAG, "OCStack resource error");
1149 OIC_LOG(DEBUG, TAG, "OUT PutOwnerUuid");
1154 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx)
1156 OIC_LOG(DEBUG, TAG, "IN PutOwnershipInformation");
1158 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1160 OIC_LOG(ERROR, TAG, "Invailed parameters");
1161 return OC_STACK_INVALID_PARAM;
1164 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1165 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1166 if(!PMGenerateQuery(true,
1167 deviceInfo->endpoint.addr, deviceInfo->securePort,
1168 deviceInfo->connType,
1169 query, sizeof(query), OIC_RSRC_DOXM_URI))
1171 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1172 return OC_STACK_ERROR;
1174 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1176 //OwnershipInformationHandler
1177 OCSecurityPayload *secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1180 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1181 return OC_STACK_NO_MEMORY;
1184 otmCtx->selectedDeviceInfo->doxm->owned = true;
1186 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1187 OCStackResult res = DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm,
1188 &secPayload->securityData1, &secPayload->payloadSize);
1189 if (OC_STACK_OK != res && NULL == secPayload->securityData1)
1191 OCPayloadDestroy((OCPayload *)secPayload);
1192 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1193 return OC_STACK_INVALID_PARAM;
1196 OCCallbackData cbData;
1197 cbData.cb = &OwnershipInformationHandler;
1198 cbData.context = (void *)otmCtx;
1201 res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1202 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1203 if (res != OC_STACK_OK)
1205 OIC_LOG(ERROR, TAG, "OCStack resource error");
1208 OIC_LOG(DEBUG, TAG, "OUT PutOwnershipInformation");
1213 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx)
1215 OIC_LOG(DEBUG, TAG, "IN PutUpdateOperationMode");
1217 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1219 return OC_STACK_INVALID_PARAM;
1222 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1223 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1224 if(!PMGenerateQuery(false,
1225 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1226 deviceInfo->connType,
1227 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1229 OIC_LOG(ERROR, TAG, "PutUpdateOperationMode : Failed to generate query");
1230 return OC_STACK_ERROR;
1232 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1234 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1237 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1238 return OC_STACK_NO_MEMORY;
1240 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1241 OCStackResult res = PstatToCBORPayload(deviceInfo->pstat, &secPayload->securityData1,
1242 &secPayload->payloadSize);
1243 if (OC_STACK_OK != res)
1245 OCPayloadDestroy((OCPayload *)secPayload);
1246 OIC_LOG(ERROR, TAG, "Error while converting pstat to cbor.");
1247 return OC_STACK_INVALID_PARAM;
1250 OCCallbackData cbData;
1251 cbData.cb = &OperationModeUpdateHandler;
1252 cbData.context = (void *)otmCtx;
1254 res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload *)secPayload,
1255 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1256 if (res != OC_STACK_OK)
1258 OIC_LOG(ERROR, TAG, "OCStack resource error");
1261 OIC_LOG(DEBUG, TAG, "OUT PutUpdateOperationMode");
1266 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
1268 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
1269 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1270 otmCtx->selectedDeviceInfo = selectedDevice;
1272 //Set to the lowest level OxM, and then find more higher level OxM.
1273 OCStackResult res = SelectProvisioningMethod(selectedDevice->doxm->oxm,
1274 selectedDevice->doxm->oxmLen,
1275 &selectedDevice->doxm->oxmSel);
1276 if(OC_STACK_OK != res)
1278 OIC_LOG(ERROR, TAG, "Failed to select the provisioning method");
1279 SetResult(otmCtx, res);
1282 OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
1284 //Send Req: PUT /oic/sec/doxm [{..."OxmSel" :g_OTMDatas[Index of Selected OxM].OXMString,...}]
1285 res = PutOwnerTransferModeToResource(otmCtx);
1286 if(OC_STACK_OK != res)
1288 OIC_LOG(WARNING, TAG, "Failed to select the provisioning method");
1289 SetResult(otmCtx, res);
1293 //Register DTLS event handler to catch the dtls event while handshake
1294 if(CA_STATUS_OK != CARegisterDTLSHandshakeCallback(DTLSHandshakeCB))
1296 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register DTLS handshake callback.");
1299 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
1305 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
1307 OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
1311 OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
1312 return OC_STACK_INVALID_PARAM;
1314 if(oxmType >= OIC_OXM_COUNT)
1316 OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
1317 return OC_STACK_INVALID_PARAM;
1320 g_OTMDatas[oxmType].loadSecretCB= data->loadSecretCB;
1321 g_OTMDatas[oxmType].createSecureSessionCB = data->createSecureSessionCB;
1322 g_OTMDatas[oxmType].createSelectOxmPayloadCB = data->createSelectOxmPayloadCB;
1323 g_OTMDatas[oxmType].createOwnerTransferPayloadCB = data->createOwnerTransferPayloadCB;
1325 OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
1331 * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
1333 OCStackResult OTMDoOwnershipTransfer(void* ctx,
1334 OCProvisionDev_t *selectedDevicelist,
1335 OCProvisionResultCB resultCallback)
1337 OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
1339 if (NULL == selectedDevicelist)
1341 return OC_STACK_INVALID_PARAM;
1343 if (NULL == resultCallback)
1345 return OC_STACK_INVALID_CALLBACK;
1348 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
1351 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
1352 return OC_STACK_NO_MEMORY;
1354 otmCtx->ctxResultCallback = resultCallback;
1355 otmCtx->ctxHasError = false;
1356 otmCtx->userCtx = ctx;
1357 OCProvisionDev_t* pCurDev = selectedDevicelist;
1359 //Counting number of selected devices.
1360 otmCtx->ctxResultArraySize = 0;
1361 while(NULL != pCurDev)
1363 otmCtx->ctxResultArraySize++;
1364 pCurDev = pCurDev->next;
1367 otmCtx->ctxResultArray =
1368 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
1369 if(NULL == otmCtx->ctxResultArray)
1371 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
1373 return OC_STACK_NO_MEMORY;
1375 pCurDev = selectedDevicelist;
1377 OCStackResult res = OC_STACK_OK;
1378 //Fill the device UUID for result array.
1379 for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
1381 //Checking duplication of Device ID.
1382 bool isDuplicate = true;
1383 res = PDMIsDuplicateDevice(&pCurDev->doxm->deviceID, &isDuplicate);
1384 if (OC_STACK_OK != res)
1390 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Device ID is duplicated");
1391 res = OC_STACK_INVALID_PARAM;
1394 memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
1395 pCurDev->doxm->deviceID.id,
1397 otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
1398 pCurDev = pCurDev->next;
1401 StartOwnershipTransfer(otmCtx, selectedDevicelist);
1403 OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
1407 OICFree(otmCtx->ctxResultArray);
1413 * Callback handler of SRPFinalizeProvisioning.
1415 * @param[in] ctx ctx value passed to callback from calling function.
1416 * @param[in] UNUSED handle to an invocation
1417 * @param[in] clientResponse Response from queries to remote servers.
1418 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1419 * and OC_STACK_KEEP_TRANSACTION to keep it.
1421 static OCStackApplicationResult FinalizeProvisioningCB(void *ctx, OCDoHandle UNUSED,
1422 OCClientResponse *clientResponse)
1424 OIC_LOG_V(INFO, TAG, "IN FinalizeProvisioningCB.");
1426 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1427 VERIFY_NON_NULL(TAG, ctx, ERROR);
1429 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1431 if(OC_STACK_OK == clientResponse->result)
1433 OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
1435 if (OC_STACK_OK == res)
1437 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
1438 SetResult(otmCtx, OC_STACK_OK);
1439 return OC_STACK_DELETE_TRANSACTION;
1443 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
1447 return OC_STACK_DELETE_TRANSACTION;
1451 * Callback handler of default ACL provisioning.
1453 * @param[in] ctx ctx value passed to callback from calling function.
1454 * @param[in] UNUSED handle to an invocation
1455 * @param[in] clientResponse Response from queries to remote servers.
1456 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1457 * and OC_STACK_KEEP_TRANSACTION to keep it.
1459 static OCStackApplicationResult ProvisionDefaultACLCB(void *ctx, OCDoHandle UNUSED,
1460 OCClientResponse *clientResponse)
1462 OIC_LOG_V(INFO, TAG, "IN ProvisionDefaultACLCB.");
1464 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1465 VERIFY_NON_NULL(TAG, ctx, ERROR);
1467 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
1470 if (OC_STACK_RESOURCE_CREATED == clientResponse->result)
1472 OIC_LOG_V(INFO, TAG, "Staring commit hash task.");
1473 // TODO hash currently have fixed value 0.
1474 uint16_t aclHash = 0;
1475 otmCtx->selectedDeviceInfo->pstat->commitHash = aclHash;
1476 otmCtx->selectedDeviceInfo->pstat->tm = NORMAL;
1477 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1480 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1481 return OC_STACK_NO_MEMORY;
1483 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1484 OCStackResult res = PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1485 &secPayload->securityData1, &secPayload->payloadSize);
1486 if (OC_STACK_OK != res || NULL == secPayload->securityData1)
1488 OICFree(secPayload);
1489 SetResult(otmCtx, OC_STACK_INVALID_JSON);
1490 return OC_STACK_DELETE_TRANSACTION;
1493 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1494 if(!PMGenerateQuery(true,
1495 otmCtx->selectedDeviceInfo->endpoint.addr,
1496 otmCtx->selectedDeviceInfo->securePort,
1497 otmCtx->selectedDeviceInfo->connType,
1498 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1500 OIC_LOG(ERROR, TAG, "ProvisionDefaultACLCB : Failed to generate query");
1501 return OC_STACK_ERROR;
1503 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1505 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1506 cbData.cb = &FinalizeProvisioningCB;
1507 cbData.context = (void*)otmCtx;
1509 OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1510 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1511 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1512 if (ret != OC_STACK_OK)
1514 OIC_LOG(ERROR, TAG, "OCStack resource error");
1515 SetResult(otmCtx, ret);
1520 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1521 clientResponse->result);
1522 SetResult(otmCtx, clientResponse->result);
1525 return OC_STACK_DELETE_TRANSACTION;
1528 OCStackResult PutProvisioningStatus(OTMContext_t* otmCtx)
1530 OIC_LOG(INFO, TAG, "IN PutProvisioningStatus");
1532 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1534 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1535 return OC_STACK_INVALID_PARAM;
1538 //Change the TAKE_OWNER bit of CM to 0.
1539 otmCtx->selectedDeviceInfo->pstat->cm &= (~TAKE_OWNER);
1541 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1544 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1545 return OC_STACK_NO_MEMORY;
1547 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1548 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1549 &secPayload->securityData1, &secPayload->payloadSize))
1551 OCPayloadDestroy((OCPayload *)secPayload);
1552 return OC_STACK_INVALID_JSON;
1554 OIC_LOG_V(INFO, TAG, "Created payload for chage to Provisiong state : %s",secPayload->securityData1);
1556 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1557 if(!PMGenerateQuery(true,
1558 otmCtx->selectedDeviceInfo->endpoint.addr,
1559 otmCtx->selectedDeviceInfo->securePort,
1560 otmCtx->selectedDeviceInfo->connType,
1561 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1563 OIC_LOG(ERROR, TAG, "PutProvisioningStatus : Failed to generate query");
1564 return OC_STACK_ERROR;
1566 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1568 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1569 cbData.cb = &ProvisioningStatusHandler;
1570 cbData.context = (void*)otmCtx;
1572 OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1573 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1574 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1575 if (ret != OC_STACK_OK)
1577 OIC_LOG(ERROR, TAG, "OCStack resource error");
1580 OIC_LOG(INFO, TAG, "OUT PutProvisioningStatus");
1585 OCStackResult PutNormalOperationStatus(OTMContext_t* otmCtx)
1587 OIC_LOG(INFO, TAG, "IN PutNormalOperationStatus");
1589 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1591 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1592 return OC_STACK_INVALID_PARAM;
1596 otmCtx->selectedDeviceInfo->pstat->isOp = true;
1598 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1601 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1602 return OC_STACK_NO_MEMORY;
1604 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1605 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1606 &secPayload->securityData1, &secPayload->payloadSize))
1608 OCPayloadDestroy((OCPayload *)secPayload);
1609 return OC_STACK_INVALID_JSON;
1611 OIC_LOG_V(INFO, TAG, "Created payload for chage to Provisiong state: %s",secPayload->securityData1);
1613 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1614 if(!PMGenerateQuery(true,
1615 otmCtx->selectedDeviceInfo->endpoint.addr,
1616 otmCtx->selectedDeviceInfo->securePort,
1617 otmCtx->selectedDeviceInfo->connType,
1618 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1620 OIC_LOG(ERROR, TAG, "PutNormalOperationStatus : Failed to generate query");
1621 return OC_STACK_ERROR;
1623 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1625 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1626 cbData.cb = &ReadyForNomalStatusHandler;
1627 cbData.context = (void*)otmCtx;
1629 OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1630 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1631 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1632 if (ret != OC_STACK_OK)
1634 OIC_LOG(ERROR, TAG, "OCStack resource error");
1637 OIC_LOG(INFO, TAG, "OUT PutNormalOperationStatus");