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 * Variables for pointing the OTMContext to be used in the DTLS handshake result callback.
74 static OTMContext_t* g_otmCtx = NULL;
77 * Function to select appropriate provisioning method.
79 * @param[in] supportedMethods Array of supported methods
80 * @param[in] numberOfMethods number of supported methods
81 * @param[out] selectedMethod Selected methods
82 * @return OC_STACK_OK on success
84 static OCStackResult SelectProvisioningMethod(const OicSecOxm_t *supportedMethods,
85 size_t numberOfMethods, OicSecOxm_t *selectedMethod)
87 OIC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
89 if(numberOfMethods == 0 || !supportedMethods)
91 OIC_LOG(WARNING, TAG, "Could not find a supported OxM.");
92 return OC_STACK_ERROR;
95 *selectedMethod = supportedMethods[0];
96 for(size_t i = 0; i < numberOfMethods; i++)
98 if(*selectedMethod < supportedMethods[i])
100 *selectedMethod = supportedMethods[i];
108 * Function to select operation mode.This function will return most secure common operation mode.
110 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
111 * @param[out] selectedMode selected operation mode
112 * @return OC_STACK_OK on success
114 static void SelectOperationMode(const OCProvisionDev_t *selectedDeviceInfo,
115 OicSecDpom_t *selectedMode)
117 OIC_LOG(DEBUG, TAG, "IN SelectOperationMode");
118 *selectedMode = selectedDeviceInfo->pstat->sm[0];
119 OIC_LOG_V(DEBUG, TAG, "Selected Operation Mode = %d", *selectedMode);
123 * Function to start ownership transfer.
124 * This function will send the first request for provisioning,
125 * The next request message is sent from the response handler for this request.
127 * @param[in] ctx context value passed to callback from calling function.
128 * @param[in] selectedDevice selected device information to performing provisioning.
129 * @return OC_STACK_OK on success
131 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
134 * Function to update owner transfer mode
136 * @param[in] otmCtx Context value of ownership transfer.
137 * @return OC_STACK_OK on success
139 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx);
142 * Function to send request to resource to get its pstat resource information.
144 * @param[in] otmCtx Context value of ownership transfer.
145 * @return OC_STACK_OK on success
147 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx);
151 * Function to send uuid of owner device to new device.
152 * This function would update 'owner of doxm' as UUID for provisioning tool.
154 * @param[in] otmCtx Context value of ownership transfer.
155 * @return OC_STACK_OK on success
157 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx);
160 * Function to update the operation mode. As per the spec. Operation mode in client driven
161 * single service provisioning it will be updated to 0x3
163 * @param[in] otmCtx Context value of ownership transfer.
164 * @return OC_STACK_OK on success
166 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx);
169 * Function to update the owner credential to new device
171 * @param[in] otmCtx Context value of ownership transfer.
172 * @param[in] selectedOperationMode selected operation mode
173 * @return OC_STACK_OK on success
175 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx);
178 * Function to send ownerShip info.
179 * This function would update 'owned of doxm' as true.
181 * @param[in] otmCtx Context value of ownership transfer.
182 * @return OC_STACK_OK on success
184 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx);
187 * Function to update pstat as Ready for provisioning.
188 * This function would update 'cm' from bx0000,0010 to bx0000,0000.
190 * @param[in] ctx context value passed to callback from calling function.
191 * @param[in] selectedDevice selected device information to performing provisioning.
192 * @return OC_STACK_OK on success
194 static OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx);
197 * Function to update pstat as Ready for Normal Operation.
198 * This function would update 'isop' from false to true.
200 * @param[in] ctx context value passed to callback from calling function.
201 * @param[in] selectedDevice selected device information to performing provisioning.
202 * @return OC_STACK_OK on success
204 static OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx);
206 static bool IsComplete(OTMContext_t* otmCtx)
208 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
210 if(OC_STACK_CONTINUE == otmCtx->ctxResultArray[i].res)
220 * Function to save the result of provisioning.
222 * @param[in,out] otmCtx Context value of ownership transfer.
223 * @param[in] res result of provisioning
225 static void SetResult(OTMContext_t* otmCtx, const OCStackResult res)
227 OIC_LOG_V(DEBUG, TAG, "IN SetResult : %d ", res);
231 OIC_LOG(WARNING, TAG, "OTMContext is NULL");
235 if(otmCtx->selectedDeviceInfo)
237 //Revert psk_info callback and new deivce uuid in case of random PIN OxM
238 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
240 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
242 OIC_LOG(WARNING, TAG, "Failed to revert is DTLS credential handler.");
244 OicUuid_t emptyUuid = { .id={0}};
245 SetUuidForRandomPinOxm(&emptyUuid);
248 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
250 if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
251 otmCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
253 otmCtx->ctxResultArray[i].res = res;
254 if(OC_STACK_OK != res)
256 otmCtx->ctxHasError = true;
263 //If all request is completed, invoke the user callback.
264 if(IsComplete(otmCtx))
266 otmCtx->ctxResultCallback(otmCtx->userCtx, otmCtx->ctxResultArraySize,
267 otmCtx->ctxResultArray, otmCtx->ctxHasError);
268 OICFree(otmCtx->ctxResultArray);
273 if(OC_STACK_OK != StartOwnershipTransfer(otmCtx,
274 otmCtx->selectedDeviceInfo->next))
276 OIC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
281 OIC_LOG(DEBUG, TAG, "OUT SetResult");
285 * Function to handle the handshake result in OTM.
286 * This function will be invoked after DTLS handshake
287 * @param endPoint [IN] The remote endpoint.
288 * @param errorInfo [IN] Error information from the endpoint.
291 void DTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
293 if(NULL != g_otmCtx && NULL != g_otmCtx->selectedDeviceInfo &&
294 NULL != endpoint && NULL != info)
296 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
297 endpoint->addr, endpoint->port, info->result);
299 OicSecDoxm_t* newDevDoxm = g_otmCtx->selectedDeviceInfo->doxm;
301 if(NULL != newDevDoxm)
303 OicUuid_t emptyUuid = {.id={0}};
305 //Make sure the address matches.
306 if(strncmp(g_otmCtx->selectedDeviceInfo->endpoint.addr,
308 sizeof(endpoint->addr)) == 0 &&
309 g_otmCtx->selectedDeviceInfo->securePort == endpoint->port)
311 OCStackResult res = OC_STACK_ERROR;
313 //If temporal secure sesstion established successfully
314 if(CA_STATUS_OK == info->result &&
315 false == newDevDoxm->owned &&
316 memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) == 0)
318 //Send request : POST /oic/sec/doxm [{... , "devowner":"PT's UUID"}]
319 res = PostOwnerUuid(g_otmCtx);
320 if(OC_STACK_OK != res)
322 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
323 SetResult(g_otmCtx, res);
326 //In case of authentication failure
327 else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
329 //in case of error from owner credential
330 if(memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) != 0 &&
331 true == newDevDoxm->owned)
333 OIC_LOG(ERROR, TAG, "The owner credential may incorrect.");
335 if(OC_STACK_OK != RemoveCredential(&(newDevDoxm->deviceID)))
337 OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
339 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
341 //in case of error from wrong PIN, re-start the ownership transfer
342 else if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
344 OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
346 memcpy(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
347 newDevDoxm->owned = false;
348 g_otmCtx->attemptCnt++;
350 if(WRONG_PIN_MAX_ATTEMP > g_otmCtx->attemptCnt)
352 res = StartOwnershipTransfer(g_otmCtx, g_otmCtx->selectedDeviceInfo);
353 if(OC_STACK_OK != res)
355 SetResult(g_otmCtx, res);
356 OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
361 OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
362 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
367 OIC_LOG(ERROR, TAG, "Failed to establish secure session.");
368 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
377 * Function to save ownerPSK at provisioning tool end.
379 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
380 * @return OC_STACK_OK on success
382 static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
384 OIC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
386 OCStackResult res = OC_STACK_ERROR;
388 CAEndpoint_t endpoint;
389 memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
390 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
391 endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
392 endpoint.port = selectedDeviceInfo->securePort;
394 OicUuid_t ptDeviceID = {.id={0}};
395 if (OC_STACK_OK != GetDoxmDeviceID(&ptDeviceID))
397 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
401 uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
402 OicSecKey_t ownerKey = {ownerPSK, OWNER_PSK_LENGTH_128};
404 //Generating OwnerPSK
405 CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
406 (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
407 strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
408 ptDeviceID.id, sizeof(ptDeviceID.id),
409 selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
410 ownerPSK, OWNER_PSK_LENGTH_128);
412 if (CA_STATUS_OK == pskRet)
414 OIC_LOG(INFO, TAG,"ownerPSK dump:\n");
415 OIC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
416 //Generating new credential for provisioning tool
417 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
418 SYMMETRIC_PAIR_WISE_KEY, NULL,
419 &ownerKey, &ptDeviceID);
420 VERIFY_NON_NULL(TAG, cred, ERROR);
422 // TODO: Added as workaround. Will be replaced soon.
423 cred->privateData.encoding = OIC_ENCODING_RAW;
426 // NOTE: Test codes to use BASE64 encoded owner PSK.
427 uint32_t outSize = 0;
428 size_t b64BufSize = B64ENCODE_OUT_SAFESIZE((OWNER_PSK_LENGTH_128 + 1));
429 char* b64Buf = (uint8_t *)OICCalloc(1, b64BufSize);
430 VERIFY_NON_NULL(TAG, b64Buf, ERROR);
431 b64Encode(cred->privateData.data, cred->privateData.len, b64Buf, b64BufSize, &outSize);
433 OICFree( cred->privateData.data );
434 cred->privateData.data = (uint8_t *)OICCalloc(1, outSize + 1);
435 VERIFY_NON_NULL(TAG, cred->privateData.data, ERROR);
437 strncpy(cred->privateData.data, b64Buf, outSize);
438 cred->privateData.data[outSize] = '\0';
439 cred->privateData.encoding = OIC_ENCODING_BASE64;
440 cred->privateData.len = outSize;
442 #endif //End of Test codes
444 res = AddCredential(cred);
445 if(res != OC_STACK_OK)
447 DeleteCredList(cred);
453 OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
456 OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
462 * Callback handler for OwnerShipTransferModeHandler API.
464 * @param[in] ctx ctx value passed to callback from calling function.
465 * @param[in] UNUSED handle to an invocation
466 * @param[in] clientResponse Response from queries to remote servers.
467 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
468 * and OC_STACK_KEEP_TRANSACTION to keep it.
470 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
471 OCClientResponse *clientResponse)
473 OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
475 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
476 VERIFY_NON_NULL(TAG, ctx, WARNING);
478 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
480 if(clientResponse->result == OC_STACK_OK)
482 OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
483 //Send request : GET /oic/sec/pstat
484 OCStackResult res = GetProvisioningStatusResource(otmCtx);
485 if(OC_STACK_OK != res)
487 OIC_LOG(WARNING, TAG, "Failed to get pstat information");
488 SetResult(otmCtx, res);
493 OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
494 clientResponse->result);
495 SetResult(otmCtx, clientResponse->result);
498 OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
501 return OC_STACK_DELETE_TRANSACTION;
505 * Callback handler for ProvisioningStatusResouceHandler API.
507 * @param[in] ctx ctx value passed to callback from calling function.
508 * @param[in] UNUSED handle to an invocation
509 * @param[in] clientResponse Response from queries to remote servers.
510 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
511 * and OC_STACK_KEEP_TRANSACTION to keep it.
513 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
514 OCClientResponse *clientResponse)
516 OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
518 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
519 VERIFY_NON_NULL(TAG, ctx, WARNING);
521 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
523 if (OC_STACK_OK == clientResponse->result)
525 if (NULL == clientResponse->payload)
527 OIC_LOG(INFO, TAG, "Skiping Null payload");
528 SetResult(otmCtx, OC_STACK_ERROR);
529 return OC_STACK_DELETE_TRANSACTION;
532 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
534 OIC_LOG(INFO, TAG, "Unknown payload type");
535 SetResult(otmCtx, OC_STACK_ERROR);
536 return OC_STACK_DELETE_TRANSACTION;
538 OicSecPstat_t* pstat = NULL;
539 OCStackResult result = CBORPayloadToPstat(
540 ((OCSecurityPayload*)clientResponse->payload)->securityData,
541 ((OCSecurityPayload*)clientResponse->payload)->payloadSize,
543 if(NULL == pstat || result != OC_STACK_OK)
545 OIC_LOG(ERROR, TAG, "Error while converting cbor to pstat.");
546 SetResult(otmCtx, OC_STACK_ERROR);
547 return OC_STACK_DELETE_TRANSACTION;
549 if(false == (TAKE_OWNER & pstat->cm))
551 OIC_LOG(ERROR, TAG, "Device pairing mode enabling owner transfer operations is disabled");
552 SetResult(otmCtx, OC_STACK_ERROR);
553 return OC_STACK_DELETE_TRANSACTION;
555 otmCtx->selectedDeviceInfo->pstat = pstat;
557 //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
558 SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
560 //Send request : POST /oic/sec/pstat [{"om":"bx11", .. }]
561 OCStackResult res = PostUpdateOperationMode(otmCtx);
562 if (OC_STACK_OK != res)
564 OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
565 SetResult(otmCtx, res);
570 OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
571 clientResponse->result);
572 SetResult(otmCtx, clientResponse->result);
575 OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
577 return OC_STACK_DELETE_TRANSACTION;
581 * Response handler for update owner uuid request.
583 * @param[in] ctx ctx value passed to callback from calling function.
584 * @param[in] UNUSED handle to an invocation
585 * @param[in] clientResponse Response from queries to remote servers.
586 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
587 * and OC_STACK_KEEP_TRANSACTION to keep it.
589 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
590 OCClientResponse *clientResponse)
592 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
593 VERIFY_NON_NULL(TAG, ctx, WARNING);
595 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
597 OCStackResult res = OC_STACK_OK;
598 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
600 if(OC_STACK_OK == clientResponse->result)
602 if(otmCtx && otmCtx->selectedDeviceInfo)
604 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
605 if(OC_STACK_OK != res)
607 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
608 SetResult(otmCtx, res);
609 return OC_STACK_DELETE_TRANSACTION;
612 //PUT owner credential to new device according to security spec B.
613 res = PutOwnerCredential(otmCtx);
614 if(OC_STACK_OK != res)
617 "OwnerUuidUpdateHandler:Failed to send PUT request for onwer credential");
618 SetResult(otmCtx, res);
619 return OC_STACK_DELETE_TRANSACTION;
625 res = clientResponse->result;
626 OIC_LOG_V(ERROR, TAG, "OwnerUuidHandler : Unexpected result %d", res);
627 SetResult(otmCtx, res);
630 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
633 return OC_STACK_DELETE_TRANSACTION;
637 * Response handler for update operation mode.
639 * @param[in] ctx ctx value passed to callback from calling function.
640 * @param[in] UNUSED handle to an invocation
641 * @param[in] clientResponse Response from queries to remote servers.
642 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
643 * and OC_STACK_KEEP_TRANSACTION to keep it.
645 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
646 OCClientResponse *clientResponse)
648 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
650 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
651 VERIFY_NON_NULL(TAG, ctx, WARNING);
653 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
655 if (OC_STACK_OK == clientResponse->result)
657 OCStackResult res = OC_STACK_ERROR;
658 OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
660 //Load secret for temporal secure session.
661 if(g_OTMDatas[selOxm].loadSecretCB)
663 res = g_OTMDatas[selOxm].loadSecretCB(otmCtx);
664 if(OC_STACK_OK != res)
666 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
667 SetResult(otmCtx, res);
668 return OC_STACK_DELETE_TRANSACTION;
672 //It will be used in handshake event handler
675 //Try DTLS handshake to generate secure session
676 if(g_OTMDatas[selOxm].createSecureSessionCB)
678 res = g_OTMDatas[selOxm].createSecureSessionCB(otmCtx);
679 if(OC_STACK_OK != res)
681 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
682 SetResult(otmCtx, res);
683 return OC_STACK_DELETE_TRANSACTION;
689 OIC_LOG(ERROR, TAG, "Error while update operation mode");
690 SetResult(otmCtx, clientResponse->result);
693 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
696 return OC_STACK_DELETE_TRANSACTION;
700 * Response handler for update owner crendetial request.
702 * @param[in] ctx ctx value passed to callback from calling function.
703 * @param[in] UNUSED handle to an invocation
704 * @param[in] clientResponse Response from queries to remote servers.
705 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
706 * and OC_STACK_KEEP_TRANSACTION to keep it.
708 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
709 OCClientResponse *clientResponse)
711 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
712 VERIFY_NON_NULL(TAG, ctx, WARNING);
714 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
716 OCStackResult res = OC_STACK_OK;
717 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
719 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
721 if(otmCtx && otmCtx->selectedDeviceInfo)
723 //Close the temporal secure session to verify the owner credential
724 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
725 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
726 CAResult_t caResult = CACloseDtlsSession(endpoint);
727 if(CA_STATUS_OK != caResult)
729 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
730 SetResult(otmCtx, caResult);
731 return OC_STACK_DELETE_TRANSACTION;
735 * If we select NULL cipher,
736 * client will select appropriate cipher suite according to server's cipher-suite list.
738 caResult = CASelectCipherSuite(TLS_NULL_WITH_NULL_NULL);
739 if(CA_STATUS_OK != caResult)
741 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
742 SetResult(otmCtx, caResult);
743 return OC_STACK_DELETE_TRANSACTION;
747 * in case of random PIN based OxM,
748 * revert get_psk_info callback of tinyDTLS to use owner credential.
750 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
752 OicUuid_t emptyUuid = { .id={0}};
753 SetUuidForRandomPinOxm(&emptyUuid);
755 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
757 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
758 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
759 return OC_STACK_DELETE_TRANSACTION;
763 //POST /oic/sec/doxm [{ ..., "owned":"TRUE" }]
764 res = PostOwnershipInformation(otmCtx);
765 if(OC_STACK_OK != res)
767 OIC_LOG(ERROR, TAG, "Failed to post ownership information to new device");
768 SetResult(otmCtx, res);
769 return OC_STACK_DELETE_TRANSACTION;
775 res = clientResponse->result;
776 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
777 SetResult(otmCtx, res);
780 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
783 return OC_STACK_DELETE_TRANSACTION;
788 * Response handler for update owner information request.
790 * @param[in] ctx ctx value passed to callback from calling function.
791 * @param[in] UNUSED handle to an invocation
792 * @param[in] clientResponse Response from queries to remote servers.
793 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
794 * and OC_STACK_KEEP_TRANSACTION to keep it.
796 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
797 OCClientResponse *clientResponse)
799 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
800 VERIFY_NON_NULL(TAG, ctx, WARNING);
802 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
804 OCStackResult res = OC_STACK_OK;
805 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
807 if(OC_STACK_OK == clientResponse->result)
809 if(otmCtx && otmCtx->selectedDeviceInfo)
811 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
812 OIC_LOG(INFO, TAG, "Set Ready for provisioning state .");
814 res = PostProvisioningStatus(otmCtx);
815 if(OC_STACK_OK != res)
817 OIC_LOG(ERROR, TAG, "Failed to update pstat");
818 SetResult(otmCtx, res);
824 res = clientResponse->result;
825 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
826 SetResult(otmCtx, res);
829 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
832 return OC_STACK_DELETE_TRANSACTION;
836 * Response handler of update provisioning status.
838 * @param[in] ctx ctx value passed to callback from calling function.
839 * @param[in] UNUSED handle to an invocation
840 * @param[in] clientResponse Response from queries to remote servers.
841 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
842 * and OC_STACK_KEEP_TRANSACTION to keep it.
844 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
845 OCClientResponse *clientResponse)
847 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
849 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
850 VERIFY_NON_NULL(TAG, ctx, ERROR);
852 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
854 OCStackResult res = OC_STACK_OK;
856 if(OC_STACK_OK == clientResponse->result)
858 if(otmCtx && otmCtx->selectedDeviceInfo)
860 OIC_LOG(INFO, TAG, "Device state is in Ready for Provisionig.");
862 res = PostNormalOperationStatus(otmCtx);
863 if(OC_STACK_OK != res)
865 OIC_LOG(ERROR, TAG, "Failed to update pstat");
866 SetResult(otmCtx, res);
872 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
873 clientResponse->result);
874 SetResult(otmCtx, clientResponse->result);
878 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
879 return OC_STACK_DELETE_TRANSACTION;
883 * Response handler of update provisioning status to Ready for Normal..
885 * @param[in] ctx ctx value passed to callback from calling function.
886 * @param[in] UNUSED handle to an invocation
887 * @param[in] clientResponse Response from queries to remote servers.
888 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
889 * and OC_STACK_KEEP_TRANSACTION to keep it.
891 static OCStackApplicationResult ReadyForNomalStatusHandler(void *ctx, OCDoHandle UNUSED,
892 OCClientResponse *clientResponse)
894 OIC_LOG_V(INFO, TAG, "IN ReadyForNomalStatusHandler.");
896 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
897 VERIFY_NON_NULL(TAG, ctx, ERROR);
899 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
902 if (OC_STACK_OK == clientResponse->result)
904 OIC_LOG(INFO, TAG, "Device state is in Ready for Normal Operation.");
905 OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
906 if (OC_STACK_OK == res)
908 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
909 SetResult(otmCtx, OC_STACK_OK);
910 return OC_STACK_DELETE_TRANSACTION;
914 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
919 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
920 clientResponse->result);
921 SetResult(otmCtx, clientResponse->result);
925 OIC_LOG_V(INFO, TAG, "OUT ReadyForNomalStatusHandler.");
926 return OC_STACK_DELETE_TRANSACTION;
929 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx)
931 OIC_LOG(DEBUG, TAG, "IN PutOwnerCredential");
933 if(!otmCtx || !otmCtx->selectedDeviceInfo)
935 OIC_LOG(ERROR, TAG, "Invalid parameters");
936 return OC_STACK_INVALID_PARAM;
939 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
940 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
942 if(!PMGenerateQuery(true,
943 deviceInfo->endpoint.addr, deviceInfo->securePort,
944 deviceInfo->connType,
945 query, sizeof(query), OIC_RSRC_CRED_URI))
947 OIC_LOG(ERROR, TAG, "PutOwnerCredential : Failed to generate query");
948 return OC_STACK_ERROR;
950 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
951 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
954 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
955 return OC_STACK_NO_MEMORY;
958 //Generate owner credential for new device
959 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
960 const OicSecCred_t* ownerCredential = GetCredResourceData(&(deviceInfo->doxm->deviceID));
963 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
964 return OC_STACK_NO_RESOURCE;
967 OicUuid_t credSubjectId = {.id={0}};
968 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
970 OicSecCred_t newCredential;
971 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
972 newCredential.next = NULL;
974 //Set subject ID as PT's ID
975 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
977 //Fill private data as empty string
978 newCredential.privateData.data = "";
979 newCredential.privateData.len = 0;
980 newCredential.privateData.encoding = ownerCredential->privateData.encoding;
982 newCredential.publicData.data = NULL;
983 newCredential.publicData.len = 0;
986 //Send owner credential to new device : PUT /oic/sec/cred [ owner credential ]
987 if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData,
988 &secPayload->payloadSize, secureFlag))
991 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
992 return OC_STACK_ERROR;
994 OIC_LOG(DEBUG, TAG, "Cred Payload:");
995 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
997 OCCallbackData cbData;
998 cbData.cb = &OwnerCredentialHandler;
999 cbData.context = (void *)otmCtx;
1001 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
1002 &deviceInfo->endpoint, (OCPayload*)secPayload,
1003 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1004 if (res != OC_STACK_OK)
1006 OIC_LOG(ERROR, TAG, "OCStack resource error");
1011 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
1012 return OC_STACK_NO_RESOURCE;
1015 OIC_LOG(DEBUG, TAG, "OUT PutOwnerCredential");
1020 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx)
1022 OIC_LOG(DEBUG, TAG, "IN PostOwnerTransferModeToResource");
1024 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1026 OIC_LOG(ERROR, TAG, "Invalid parameters");
1027 return OC_STACK_INVALID_PARAM;
1030 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1031 OicSecOxm_t selectedOxm = deviceInfo->doxm->oxmSel;
1032 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1034 if(!PMGenerateQuery(false,
1035 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1036 deviceInfo->connType,
1037 query, sizeof(query), OIC_RSRC_DOXM_URI))
1039 OIC_LOG(ERROR, TAG, "PostOwnerTransferModeToResource : Failed to generate query");
1040 return OC_STACK_ERROR;
1042 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1043 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1046 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1047 return OC_STACK_NO_MEMORY;
1049 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1050 OCStackResult res = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx,
1051 &secPayload->securityData, &secPayload->payloadSize);
1052 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1054 OCPayloadDestroy((OCPayload *)secPayload);
1055 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor");
1056 return OC_STACK_ERROR;
1059 OCCallbackData cbData;
1060 cbData.cb = &OwnerTransferModeHandler;
1061 cbData.context = (void *)otmCtx;
1063 res = OCDoResource(NULL, OC_REST_POST, query,
1064 &deviceInfo->endpoint, (OCPayload *)secPayload,
1065 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1066 if (res != OC_STACK_OK)
1068 OIC_LOG(ERROR, TAG, "OCStack resource error");
1071 OIC_LOG(DEBUG, TAG, "OUT PostOwnerTransferModeToResource");
1076 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1078 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1080 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1082 OIC_LOG(ERROR, TAG, "Invailed parameters");
1083 return OC_STACK_INVALID_PARAM;
1086 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1087 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1088 if(!PMGenerateQuery(false,
1089 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1090 deviceInfo->connType,
1091 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1093 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1094 return OC_STACK_ERROR;
1096 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1098 OCCallbackData cbData;
1099 cbData.cb = &ListMethodsHandler;
1100 cbData.context = (void *)otmCtx;
1102 OCStackResult res = OCDoResource(NULL, OC_REST_GET, query, NULL, NULL,
1103 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1104 if (res != OC_STACK_OK)
1106 OIC_LOG(ERROR, TAG, "OCStack resource error");
1109 OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1114 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx)
1116 OIC_LOG(DEBUG, TAG, "IN PostOwnerUuid");
1118 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1120 OIC_LOG(ERROR, TAG, "Invailed parameters");
1121 return OC_STACK_INVALID_PARAM;
1124 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1125 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1126 if(!PMGenerateQuery(true,
1127 deviceInfo->endpoint.addr, deviceInfo->securePort,
1128 deviceInfo->connType,
1129 query, sizeof(query), OIC_RSRC_DOXM_URI))
1131 OIC_LOG(ERROR, TAG, "PostOwnerUuid : Failed to generate query");
1132 return OC_STACK_ERROR;
1134 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1136 //Post PT's uuid to new device
1137 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1140 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1141 return OC_STACK_NO_MEMORY;
1143 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1144 OCStackResult res = g_OTMDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(
1145 otmCtx, &secPayload->securityData, &secPayload->payloadSize);
1146 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1148 OCPayloadDestroy((OCPayload *)secPayload);
1149 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to cbor.");
1150 return OC_STACK_INVALID_PARAM;
1152 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1154 OCCallbackData cbData;
1155 cbData.cb = &OwnerUuidUpdateHandler;
1156 cbData.context = (void *)otmCtx;
1159 res = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1160 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1161 if (res != OC_STACK_OK)
1163 OIC_LOG(ERROR, TAG, "OCStack resource error");
1166 OIC_LOG(DEBUG, TAG, "OUT PostOwnerUuid");
1171 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx)
1173 OIC_LOG(DEBUG, TAG, "IN PostOwnershipInformation");
1175 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1177 OIC_LOG(ERROR, TAG, "Invailed parameters");
1178 return OC_STACK_INVALID_PARAM;
1181 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1182 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1183 if(!PMGenerateQuery(true,
1184 deviceInfo->endpoint.addr, deviceInfo->securePort,
1185 deviceInfo->connType,
1186 query, sizeof(query), OIC_RSRC_DOXM_URI))
1188 OIC_LOG(ERROR, TAG, "PostOwnershipInformation : Failed to generate query");
1189 return OC_STACK_ERROR;
1191 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1193 //OwnershipInformationHandler
1194 OCSecurityPayload *secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1197 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1198 return OC_STACK_NO_MEMORY;
1201 otmCtx->selectedDeviceInfo->doxm->owned = true;
1203 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1204 OCStackResult res = DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm,
1205 &secPayload->securityData, &secPayload->payloadSize);
1206 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1208 OCPayloadDestroy((OCPayload *)secPayload);
1209 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1210 return OC_STACK_INVALID_PARAM;
1213 OCCallbackData cbData;
1214 cbData.cb = &OwnershipInformationHandler;
1215 cbData.context = (void *)otmCtx;
1218 res = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1219 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1220 if (res != OC_STACK_OK)
1222 OIC_LOG(ERROR, TAG, "OCStack resource error");
1225 OIC_LOG(DEBUG, TAG, "OUT PostOwnershipInformation");
1230 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx)
1232 OIC_LOG(DEBUG, TAG, "IN PostUpdateOperationMode");
1234 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1236 return OC_STACK_INVALID_PARAM;
1239 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1240 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1241 if(!PMGenerateQuery(false,
1242 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1243 deviceInfo->connType,
1244 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1246 OIC_LOG(ERROR, TAG, "PostUpdateOperationMode : Failed to generate query");
1247 return OC_STACK_ERROR;
1249 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1251 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1254 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1255 return OC_STACK_NO_MEMORY;
1257 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1258 OCStackResult res = PstatToCBORPayload(deviceInfo->pstat, &secPayload->securityData,
1259 &secPayload->payloadSize);
1260 if (OC_STACK_OK != res)
1262 OCPayloadDestroy((OCPayload *)secPayload);
1263 OIC_LOG(ERROR, TAG, "Error while converting pstat to cbor.");
1264 return OC_STACK_INVALID_PARAM;
1267 OCCallbackData cbData;
1268 cbData.cb = &OperationModeUpdateHandler;
1269 cbData.context = (void *)otmCtx;
1271 res = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1272 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1273 if (res != OC_STACK_OK)
1275 OIC_LOG(ERROR, TAG, "OCStack resource error");
1278 OIC_LOG(DEBUG, TAG, "OUT PostUpdateOperationMode");
1283 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
1285 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
1286 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1287 otmCtx->selectedDeviceInfo = selectedDevice;
1289 //Set to the lowest level OxM, and then find more higher level OxM.
1290 OCStackResult res = SelectProvisioningMethod(selectedDevice->doxm->oxm,
1291 selectedDevice->doxm->oxmLen,
1292 &selectedDevice->doxm->oxmSel);
1293 if(OC_STACK_OK != res)
1295 OIC_LOG(ERROR, TAG, "Failed to select the provisioning method");
1296 SetResult(otmCtx, res);
1299 OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
1301 //Send Req: POST /oic/sec/doxm [{..."OxmSel" :g_OTMDatas[Index of Selected OxM].OXMString,...}]
1302 res = PostOwnerTransferModeToResource(otmCtx);
1303 if(OC_STACK_OK != res)
1305 OIC_LOG(WARNING, TAG, "Failed to select the provisioning method");
1306 SetResult(otmCtx, res);
1310 //Register DTLS event handler to catch the dtls event while handshake
1311 if(CA_STATUS_OK != CARegisterDTLSHandshakeCallback(DTLSHandshakeCB))
1313 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register DTLS handshake callback.");
1316 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
1322 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
1324 OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
1328 OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
1329 return OC_STACK_INVALID_PARAM;
1331 if(oxmType >= OIC_OXM_COUNT)
1333 OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
1334 return OC_STACK_INVALID_PARAM;
1337 g_OTMDatas[oxmType].loadSecretCB= data->loadSecretCB;
1338 g_OTMDatas[oxmType].createSecureSessionCB = data->createSecureSessionCB;
1339 g_OTMDatas[oxmType].createSelectOxmPayloadCB = data->createSelectOxmPayloadCB;
1340 g_OTMDatas[oxmType].createOwnerTransferPayloadCB = data->createOwnerTransferPayloadCB;
1342 OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
1348 * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
1350 OCStackResult OTMDoOwnershipTransfer(void* ctx,
1351 OCProvisionDev_t *selectedDevicelist,
1352 OCProvisionResultCB resultCallback)
1354 OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
1356 if (NULL == selectedDevicelist)
1358 return OC_STACK_INVALID_PARAM;
1360 if (NULL == resultCallback)
1362 return OC_STACK_INVALID_CALLBACK;
1365 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
1368 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
1369 return OC_STACK_NO_MEMORY;
1371 otmCtx->ctxResultCallback = resultCallback;
1372 otmCtx->ctxHasError = false;
1373 otmCtx->userCtx = ctx;
1374 OCProvisionDev_t* pCurDev = selectedDevicelist;
1376 //Counting number of selected devices.
1377 otmCtx->ctxResultArraySize = 0;
1378 while(NULL != pCurDev)
1380 otmCtx->ctxResultArraySize++;
1381 pCurDev = pCurDev->next;
1384 otmCtx->ctxResultArray =
1385 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
1386 if(NULL == otmCtx->ctxResultArray)
1388 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
1390 return OC_STACK_NO_MEMORY;
1392 pCurDev = selectedDevicelist;
1394 OCStackResult res = OC_STACK_OK;
1395 //Fill the device UUID for result array.
1396 for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
1398 //Checking duplication of Device ID.
1399 bool isDuplicate = true;
1400 res = PDMIsDuplicateDevice(&pCurDev->doxm->deviceID, &isDuplicate);
1401 if (OC_STACK_OK != res)
1407 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Device ID is duplicated");
1408 res = OC_STACK_INVALID_PARAM;
1411 memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
1412 pCurDev->doxm->deviceID.id,
1414 otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
1415 pCurDev = pCurDev->next;
1418 StartOwnershipTransfer(otmCtx, selectedDevicelist);
1420 OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
1424 OICFree(otmCtx->ctxResultArray);
1429 OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx)
1431 OIC_LOG(INFO, TAG, "IN PostProvisioningStatus");
1433 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1435 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1436 return OC_STACK_INVALID_PARAM;
1439 //Change the TAKE_OWNER bit of CM to 0.
1440 otmCtx->selectedDeviceInfo->pstat->cm &= (~TAKE_OWNER);
1442 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1445 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1446 return OC_STACK_NO_MEMORY;
1448 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1449 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1450 &secPayload->securityData, &secPayload->payloadSize))
1452 OCPayloadDestroy((OCPayload *)secPayload);
1453 return OC_STACK_INVALID_JSON;
1455 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
1456 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1458 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1459 if(!PMGenerateQuery(true,
1460 otmCtx->selectedDeviceInfo->endpoint.addr,
1461 otmCtx->selectedDeviceInfo->securePort,
1462 otmCtx->selectedDeviceInfo->connType,
1463 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1465 OIC_LOG(ERROR, TAG, "PostProvisioningStatus : Failed to generate query");
1466 return OC_STACK_ERROR;
1468 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1470 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1471 cbData.cb = &ProvisioningStatusHandler;
1472 cbData.context = (void*)otmCtx;
1474 OCStackResult ret = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1475 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1476 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1477 if (ret != OC_STACK_OK)
1479 OIC_LOG(ERROR, TAG, "OCStack resource error");
1482 OIC_LOG(INFO, TAG, "OUT PostProvisioningStatus");
1487 OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx)
1489 OIC_LOG(INFO, TAG, "IN PostNormalOperationStatus");
1491 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1493 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1494 return OC_STACK_INVALID_PARAM;
1498 otmCtx->selectedDeviceInfo->pstat->isOp = true;
1500 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1503 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1504 return OC_STACK_NO_MEMORY;
1506 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1507 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1508 &secPayload->securityData, &secPayload->payloadSize))
1510 OCPayloadDestroy((OCPayload *)secPayload);
1511 return OC_STACK_INVALID_JSON;
1513 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
1514 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1516 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1517 if(!PMGenerateQuery(true,
1518 otmCtx->selectedDeviceInfo->endpoint.addr,
1519 otmCtx->selectedDeviceInfo->securePort,
1520 otmCtx->selectedDeviceInfo->connType,
1521 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1523 OIC_LOG(ERROR, TAG, "PostNormalOperationStatus : Failed to generate query");
1524 return OC_STACK_ERROR;
1526 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1528 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1529 cbData.cb = &ReadyForNomalStatusHandler;
1530 cbData.context = (void*)otmCtx;
1532 OCStackResult ret = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1533 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1534 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1535 if (ret != OC_STACK_OK)
1537 OIC_LOG(ERROR, TAG, "OCStack resource error");
1540 OIC_LOG(INFO, TAG, "OUT PostNormalOperationStatus");