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
33 #include "iotivity_config.h"
40 #ifdef HAVE_SYS_TIME_H
47 #include "oic_malloc.h"
48 #include "oic_string.h"
50 #include "cainterface.h"
55 #include "srmresourcestrings.h"
56 #include "doxmresource.h"
57 #include "pstatresource.h"
58 #include "credresource.h"
59 #include "aclresource.h"
60 #include "ownershiptransfermanager.h"
61 #include "securevirtualresourcetypes.h"
62 #include "oxmjustworks.h"
63 #include "oxmrandompin.h"
64 #include "oxmmanufacturercert.h"
65 #ifdef _ENABLE_MULTIPLE_OWNER_
66 #include "oxmpreconfpin.h"
67 #endif //_ENABLE_MULTIPLE_OWNER_
68 #include "otmcontextlist.h"
70 #include "pmutility.h"
71 #include "srmutility.h"
72 #include "provisioningdatabasemanager.h"
73 #include "ocpayload.h"
74 #include "payload_logging.h"
75 #include "pkix_interface.h"
81 #define NOT_ALLOWED_OXM 0
84 * List of allowed oxm list.
85 * All oxm methods are allowed as default.
87 static uint8_t g_OxmAllowStatus[OIC_OXM_COUNT] = {ALLOWED_OXM, ALLOWED_OXM, ALLOWED_OXM, NOT_ALLOWED_OXM};
90 * Variables for pointing the OTMContext to be used in the DTLS handshake result callback.
92 static OTMContext_t* g_otmCtx = NULL;
94 OCStackResult OTMSetOTCallback(OicSecOxm_t oxm, OTMCallbackData_t* callbacks)
96 OCStackResult res = OC_STACK_INVALID_PARAM;
98 OIC_LOG(INFO, TAG, "IN OTMSetOTCallback");
100 VERIFY_NON_NULL(TAG, callbacks, ERROR);
101 #ifdef _ENABLE_MULTIPLE_OWNER_
102 VERIFY_SUCCESS(TAG, (OIC_OXM_COUNT > oxm || OIC_PRECONFIG_PIN == oxm), ERROR);
104 VERIFY_SUCCESS(TAG, (OIC_OXM_COUNT > oxm), ERROR);
105 #endif //_ENABLE_MULTIPLE_OWNER_
110 callbacks->loadSecretCB = LoadSecretJustWorksCallback;
111 callbacks->createSecureSessionCB = CreateSecureSessionJustWorksCallback;
112 callbacks->createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
113 callbacks->createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
115 case OIC_RANDOM_DEVICE_PIN:
116 callbacks->loadSecretCB = InputPinCodeCallback;
117 callbacks->createSecureSessionCB = CreateSecureSessionRandomPinCallback;
118 callbacks->createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
119 callbacks->createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
121 case OIC_MANUFACTURER_CERTIFICATE:
122 callbacks->loadSecretCB = PrepareMCertificateCallback;
123 callbacks->createSecureSessionCB = CreateSecureSessionMCertificateCallback;
124 callbacks->createSelectOxmPayloadCB = CreateMCertificateBasedSelectOxmPayload;
125 callbacks->createOwnerTransferPayloadCB = CreateMCertificateBasedOwnerTransferPayload;
127 case OIC_DECENTRALIZED_PUBLIC_KEY:
128 OIC_LOG(ERROR, TAG, "OIC_DECENTRALIZED_PUBLIC_KEY not supported yet.");
129 return OC_STACK_INVALID_METHOD;
130 #ifdef _ENABLE_MULTIPLE_OWNER_
131 case OIC_PRECONFIG_PIN:
132 callbacks->loadSecretCB = LoadPreconfigPinCodeCallback;
133 callbacks->createSecureSessionCB = CreateSecureSessionPreconfigPinCallback;
134 callbacks->createSelectOxmPayloadCB = CreatePreconfigPinBasedSelectOxmPayload;
135 callbacks->createOwnerTransferPayloadCB = CreatePreconfigPinBasedOwnerTransferPayload;
137 #endif //_ENABLE_MULTIPLE_OWNER_
139 OIC_LOG_V(ERROR, TAG, "Unknown OxM : %d", (int)oxm);
140 return OC_STACK_INVALID_PARAM;
146 OIC_LOG(INFO, TAG, "OUT OTMSetOTCallback");
151 * Function to select appropriate provisioning method.
153 * @param[in] supportedMethods Array of supported methods
154 * @param[in] numberOfMethods number of supported methods
155 * @param[out] selectedMethod Selected methods
156 * @return OC_STACK_OK on success
158 static OCStackResult SelectProvisioningMethod(const OicSecOxm_t *supportedMethods,
159 size_t numberOfMethods, OicSecOxm_t *selectedMethod)
161 bool isOxmSelected = false;
163 OIC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
165 if(numberOfMethods == 0 || !supportedMethods)
167 OIC_LOG(WARNING, TAG, "Could not find a supported OxM.");
168 return OC_STACK_ERROR;
171 for(size_t i = 0; i < numberOfMethods; i++)
173 if(ALLOWED_OXM == g_OxmAllowStatus[supportedMethods[i]])
175 *selectedMethod = supportedMethods[i];
176 isOxmSelected = true;
182 OIC_LOG(ERROR, TAG, "Can not find the allowed OxM.");
183 return OC_STACK_NOT_ALLOWED_OXM;
186 for(size_t i = 0; i < numberOfMethods; i++)
188 if(*selectedMethod < supportedMethods[i] &&
189 ALLOWED_OXM == g_OxmAllowStatus[supportedMethods[i]])
191 *selectedMethod = supportedMethods[i];
195 OIC_LOG(DEBUG, TAG, "OUT SelectProvisioningMethod");
201 * Function to select operation mode.This function will return most secure common operation mode.
203 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
204 * @param[out] selectedMode selected operation mode
205 * @return OC_STACK_OK on success
207 static void SelectOperationMode(const OCProvisionDev_t *selectedDeviceInfo,
208 OicSecDpom_t *selectedMode)
210 OIC_LOG(DEBUG, TAG, "IN SelectOperationMode");
211 *selectedMode = selectedDeviceInfo->pstat->sm[0];
212 OIC_LOG_V(DEBUG, TAG, "Selected Operation Mode = %d", *selectedMode);
216 * Function to start ownership transfer.
217 * This function will send the first request for provisioning,
218 * The next request message is sent from the response handler for this request.
220 * @param[in] ctx context value passed to callback from calling function.
221 * @param[in] selectedDevice selected device information to performing provisioning.
222 * @return OC_STACK_OK on success
224 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
227 * Function to update owner transfer mode
229 * @param[in] otmCtx Context value of ownership transfer.
230 * @return OC_STACK_OK on success
232 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx);
235 * Function to send request to resource to get its pstat resource information.
237 * @param[in] otmCtx Context value of ownership transfer.
238 * @return OC_STACK_OK on success
240 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx);
244 * Function to send uuid of owner device to new device.
245 * This function would update 'owner of doxm' as UUID for provisioning tool.
247 * @param[in] otmCtx Context value of ownership transfer.
248 * @return OC_STACK_OK on success
250 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx);
253 * Function to update the operation mode. As per the spec. Operation mode in client driven
254 * single service provisioning it will be updated to 0x3
256 * @param[in] otmCtx Context value of ownership transfer.
257 * @return OC_STACK_OK on success
259 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx);
262 * Function to update the owner credential to new device
264 * @param[in] otmCtx Context value of ownership transfer.
265 * @param[in] selectedOperationMode selected operation mode
266 * @return OC_STACK_OK on success
268 static OCStackResult PostOwnerCredential(OTMContext_t* otmCtx);
271 * Function to update the owner ACL to new device.
273 * @param[in] otmCtx Context value of ownership transfer.
274 * @return OC_STACK_OK on success
276 static OCStackResult PostOwnerAcl(OTMContext_t* otmCtx);
279 * Function to send ownerShip info.
280 * This function would update 'owned of doxm' as true.
282 * @param[in] otmCtx Context value of ownership transfer.
283 * @return OC_STACK_OK on success
285 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx);
288 * Function to update pstat as Ready for provisioning.
289 * This function would update 'cm' from bx0000,0010 to bx0000,0000.
291 * @param[in] ctx context value passed to callback from calling function.
292 * @param[in] selectedDevice selected device information to performing provisioning.
293 * @return OC_STACK_OK on success
295 static OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx);
298 * Function to update pstat as Ready for Normal Operation.
299 * This function would update 'isop' from false to true.
301 * @param[in] ctx context value passed to callback from calling function.
302 * @param[in] selectedDevice selected device information to performing provisioning.
303 * @return OC_STACK_OK on success
305 static OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx);
307 static bool IsComplete(OTMContext_t* otmCtx)
309 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
311 if(OC_STACK_CONTINUE == otmCtx->ctxResultArray[i].res)
321 * Function to save the result of provisioning.
323 * @param[in,out] otmCtx Context value of ownership transfer.
324 * @param[in] res result of provisioning
326 static void SetResult(OTMContext_t* otmCtx, const OCStackResult res)
328 OIC_LOG_V(DEBUG, TAG, "IN SetResult : %d ", res);
330 if(NULL == otmCtx || NULL == otmCtx->selectedDeviceInfo)
332 OIC_LOG(WARNING, TAG, "OTMContext is NULL");
336 //If OTM Context was removed from previous response handler, just exit the current OTM process.
337 if(NULL == GetOTMContext(otmCtx->selectedDeviceInfo->endpoint.addr,
338 otmCtx->selectedDeviceInfo->securePort))
340 OIC_LOG(WARNING, TAG, "Current OTM Process has already ended.");
344 //Revert psk_info callback and new deivce uuid in case of random PIN OxM
345 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
347 if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskCredentials))
349 OIC_LOG(WARNING, TAG, "Failed to revert is DTLS credential handler.");
351 OicUuid_t emptyUuid = { .id={0}};
352 SetUuidForPinBasedOxm(&emptyUuid);
354 else if(OIC_MANUFACTURER_CERTIFICATE == otmCtx->selectedDeviceInfo->doxm->oxmSel)
356 //Revert back certificate related callbacks.
357 if(CA_STATUS_OK != CAregisterPkixInfoHandler(GetPkixInfo))
359 OIC_LOG(WARNING, TAG, "Failed to revert PkixInfoHandler.");
361 if(CA_STATUS_OK != CAregisterGetCredentialTypesHandler(InitCipherSuiteList))
363 OIC_LOG(WARNING, TAG, "Failed to revert CredentialTypesHandler.");
367 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
369 if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
370 otmCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
372 otmCtx->ctxResultArray[i].res = res;
373 if(OC_STACK_OK != res && OC_STACK_CONTINUE != res && OC_STACK_DUPLICATE_REQUEST != res)
375 otmCtx->ctxHasError = true;
376 if (OC_STACK_OK != PDMDeleteDevice(&otmCtx->ctxResultArray[i].deviceId))
378 OIC_LOG(WARNING, TAG, "Internal error in PDMDeleteDevice");
380 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
381 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
382 if (CA_STATUS_OK != CAcloseSslConnection(endpoint))
384 OIC_LOG(WARNING, TAG, "Failed to close Secure session");
390 //In case of duplicated OTM process, OTMContext and OCDoHandle should not be removed.
391 if(OC_STACK_DUPLICATE_REQUEST != res)
393 //Remove the current OTM Context from OTM queue
394 RemoveOTMContext(otmCtx->selectedDeviceInfo->endpoint.addr,
395 otmCtx->selectedDeviceInfo->securePort);
397 //If there is a request being performed, cancel it to prevent retransmission.
398 if(otmCtx->ocDoHandle)
400 OIC_LOG_V(DEBUG, TAG, "OCCancel - %s : %d",
401 otmCtx->selectedDeviceInfo->endpoint.addr,
402 otmCtx->selectedDeviceInfo->securePort);
403 if(OC_STACK_OK != OCCancel(otmCtx->ocDoHandle, OC_HIGH_QOS, NULL, 0))
405 OIC_LOG(WARNING, TAG, "Failed to remove registered callback");
409 otmCtx->ocDoHandle = NULL;
414 //If all OTM process is complete, invoke the user callback.
415 if(IsComplete(otmCtx))
417 otmCtx->ctxResultCallback(otmCtx->userCtx, otmCtx->ctxResultArraySize,
418 otmCtx->ctxResultArray, otmCtx->ctxHasError);
419 OICFree(otmCtx->ctxResultArray);
424 if(OC_STACK_OK != StartOwnershipTransfer(otmCtx,
425 otmCtx->selectedDeviceInfo->next))
427 OIC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
431 OIC_LOG(DEBUG, TAG, "OUT SetResult");
435 * Function to handle the handshake result in OTM.
436 * This function will be invoked after DTLS handshake
437 * @param endPoint [IN] The remote endpoint.
438 * @param errorInfo [IN] Error information from the endpoint.
441 void DTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
443 if(NULL != endpoint && NULL != info)
445 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
446 endpoint->addr, endpoint->port, info->result);
448 OTMContext_t* otmCtx = GetOTMContext(endpoint->addr, endpoint->port);
451 OicSecDoxm_t* newDevDoxm = otmCtx->selectedDeviceInfo->doxm;
452 if(NULL != newDevDoxm)
454 OicUuid_t emptyUuid = {.id={0}};
456 //Make sure the address matches.
457 if(strncmp(otmCtx->selectedDeviceInfo->endpoint.addr,
459 sizeof(endpoint->addr)) == 0 &&
460 otmCtx->selectedDeviceInfo->securePort == endpoint->port)
462 OCStackResult res = OC_STACK_ERROR;
464 //If temporal secure sesstion established successfully
465 if(CA_STATUS_OK == info->result &&
466 false == newDevDoxm->owned &&
467 memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) == 0)
469 //Send request : POST /oic/sec/doxm [{... , "devowner":"PT's UUID"}]
470 res = PostOwnerUuid(otmCtx);
471 if(OC_STACK_OK != res)
473 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
474 SetResult(otmCtx, res);
477 //In case of authentication failure
478 else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
480 //in case of error from owner credential
481 if(memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) != 0 &&
482 true == newDevDoxm->owned)
484 OIC_LOG(ERROR, TAG, "The owner credential may incorrect.");
486 if(OC_STACK_OK != RemoveCredential(&(newDevDoxm->deviceID)))
488 OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
490 SetResult(otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
492 //in case of error from wrong PIN, re-start the ownership transfer
493 else if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
495 OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
497 memcpy(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
498 newDevDoxm->owned = false;
499 otmCtx->attemptCnt++;
501 if(WRONG_PIN_MAX_ATTEMP > otmCtx->attemptCnt)
503 res = StartOwnershipTransfer(otmCtx, otmCtx->selectedDeviceInfo);
504 if(OC_STACK_OK != res)
506 SetResult(otmCtx, res);
507 OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
512 OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
513 SetResult(otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
518 OIC_LOG(ERROR, TAG, "Failed to establish secure session.");
519 SetResult(otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
527 OIC_LOG(ERROR, TAG, "Can not find the OTM Context.");
533 * Function to save the Owner/SubOwner PSK.
535 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
536 * @return OC_STACK_OK on success
538 static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
540 OIC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
542 OCStackResult res = OC_STACK_ERROR;
544 CAEndpoint_t endpoint;
545 memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
546 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
547 endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
548 endpoint.port = selectedDeviceInfo->securePort;
549 endpoint.adapter = selectedDeviceInfo->endpoint.adapter;
551 OicUuid_t ownerDeviceID = {.id={0}};
552 if (OC_STACK_OK != GetDoxmDeviceID(&ownerDeviceID))
554 OIC_LOG(ERROR, TAG, "Error while retrieving Owner's device ID");
558 uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
559 OicSecKey_t ownerKey = {ownerPSK, OWNER_PSK_LENGTH_128};
561 //Generating OwnerPSK
562 CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
563 (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
564 strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
565 ownerDeviceID.id, sizeof(ownerDeviceID.id),
566 selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
567 ownerPSK, OWNER_PSK_LENGTH_128);
569 if (CA_STATUS_OK == pskRet)
571 OIC_LOG(DEBUG, TAG,"Owner PSK dump:\n");
572 OIC_LOG_BUFFER(DEBUG, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
573 //Generating new credential for provisioning tool
574 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
575 SYMMETRIC_PAIR_WISE_KEY, NULL,
576 &ownerKey, &ownerDeviceID, NULL);
577 OICClearMemory(ownerPSK, sizeof(ownerPSK));
578 VERIFY_NON_NULL(TAG, cred, ERROR);
580 // TODO: Added as workaround. Will be replaced soon.
581 cred->privateData.encoding = OIC_ENCODING_RAW;
584 // NOTE: Test codes to use BASE64 encoded owner PSK.
585 uint32_t outSize = 0;
586 size_t b64BufSize = B64ENCODE_OUT_SAFESIZE((OWNER_PSK_LENGTH_128 + 1));
587 char* b64Buf = (uint8_t *)OICCalloc(1, b64BufSize);
588 VERIFY_NON_NULL(TAG, b64Buf, ERROR);
589 b64Encode(cred->privateData.data, cred->privateData.len, b64Buf, b64BufSize, &outSize);
591 OICFree( cred->privateData.data );
592 cred->privateData.data = (uint8_t *)OICCalloc(1, outSize + 1);
593 VERIFY_NON_NULL(TAG, cred->privateData.data, ERROR);
595 strncpy(cred->privateData.data, b64Buf, outSize);
596 cred->privateData.data[outSize] = '\0';
597 cred->privateData.encoding = OIC_ENCODING_BASE64;
598 cred->privateData.len = outSize;
600 #endif //End of Test codes
602 //Finding previous ownerPSK.
603 const OicSecCred_t* credList = GetCredList();
604 OicSecCred_t* prevCred = NULL;
606 LL_FOREACH(credList, prevCred)
608 //OwnerPSK's type is SYMMETRIC_PAIR_WISE_KEY
609 if (SYMMETRIC_PAIR_WISE_KEY == prevCred->credType &&
610 0 == memcmp(prevCred->subject.id, cred->subject.id, sizeof(cred->subject.id)))
612 credId = prevCred->credId;
617 //If duplicate owner PSK is exists, remove it.
620 OIC_LOG(WARNING, TAG, "Duplicate OwnerPSK was detected.");
621 OIC_LOG(WARNING, TAG, "[Subject] : ");
622 OIC_LOG_BUFFER(WARNING, TAG, prevCred->subject.id, sizeof(prevCred->subject.id));
623 OIC_LOG_V(WARNING, TAG, "[Encoding Type] : %d", prevCred->privateData.encoding);
624 OIC_LOG(DEBUG, TAG, "[Private Data] : ");
625 OIC_LOG_BUFFER(DEBUG, TAG, prevCred->privateData.data, prevCred->privateData.len);
626 OIC_LOG(WARNING, TAG, "Previous OwnerPSK will be removed.");
628 res = RemoveCredentialByCredId(credId);
629 if(OC_STACK_RESOURCE_DELETED != res)
631 OIC_LOG(ERROR, TAG, "Failed to remove the previous OwnerPSK");
632 DeleteCredList(cred);
637 res = AddCredential(cred);
638 if(res != OC_STACK_OK)
640 DeleteCredList(cred);
646 OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
649 OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
655 * Callback handler for OwnerShipTransferModeHandler API.
657 * @param[in] ctx ctx value passed to callback from calling function.
658 * @param[in] UNUSED handle to an invocation
659 * @param[in] clientResponse Response from queries to remote servers.
660 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
661 * and OC_STACK_KEEP_TRANSACTION to keep it.
663 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
664 OCClientResponse *clientResponse)
666 OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
668 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
669 VERIFY_NON_NULL(TAG, ctx, WARNING);
671 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
672 otmCtx->ocDoHandle = NULL;
674 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
676 OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
677 //Send request : GET /oic/sec/pstat
678 OCStackResult res = GetProvisioningStatusResource(otmCtx);
679 if(OC_STACK_OK != res)
681 OIC_LOG(WARNING, TAG, "Failed to get pstat information");
682 SetResult(otmCtx, res);
687 OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
688 clientResponse->result);
689 SetResult(otmCtx, clientResponse->result);
692 OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
695 return OC_STACK_DELETE_TRANSACTION;
699 * Callback handler for ProvisioningStatusResouceHandler API.
701 * @param[in] ctx ctx value passed to callback from calling function.
702 * @param[in] UNUSED handle to an invocation
703 * @param[in] clientResponse Response from queries to remote servers.
704 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
705 * and OC_STACK_KEEP_TRANSACTION to keep it.
707 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
708 OCClientResponse *clientResponse)
710 OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
712 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
713 VERIFY_NON_NULL(TAG, ctx, WARNING);
715 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
716 otmCtx->ocDoHandle = NULL;
718 if (OC_STACK_OK == clientResponse->result)
720 if (NULL == clientResponse->payload)
722 OIC_LOG(INFO, TAG, "Skiping Null payload");
723 SetResult(otmCtx, OC_STACK_ERROR);
724 return OC_STACK_DELETE_TRANSACTION;
727 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
729 OIC_LOG(INFO, TAG, "Unknown payload type");
730 SetResult(otmCtx, OC_STACK_ERROR);
731 return OC_STACK_DELETE_TRANSACTION;
733 OicSecPstat_t* pstat = NULL;
734 OCStackResult result = CBORPayloadToPstat(
735 ((OCSecurityPayload*)clientResponse->payload)->securityData,
736 ((OCSecurityPayload*)clientResponse->payload)->payloadSize,
738 if(NULL == pstat || result != OC_STACK_OK)
740 OIC_LOG(ERROR, TAG, "Error while converting cbor to pstat.");
741 SetResult(otmCtx, OC_STACK_ERROR);
742 return OC_STACK_DELETE_TRANSACTION;
744 if(false == (TAKE_OWNER & pstat->cm))
746 OIC_LOG(ERROR, TAG, "Device pairing mode enabling owner transfer operations is disabled");
747 SetResult(otmCtx, OC_STACK_ERROR);
748 return OC_STACK_DELETE_TRANSACTION;
750 otmCtx->selectedDeviceInfo->pstat = pstat;
752 //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
753 SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
755 //Send request : POST /oic/sec/pstat [{"om":"bx11", .. }]
756 OCStackResult res = PostUpdateOperationMode(otmCtx);
757 if (OC_STACK_OK != res)
759 OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
760 SetResult(otmCtx, res);
765 OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
766 clientResponse->result);
767 SetResult(otmCtx, clientResponse->result);
770 OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
772 return OC_STACK_DELETE_TRANSACTION;
776 * Response handler for update owner uuid request.
778 * @param[in] ctx ctx value passed to callback from calling function.
779 * @param[in] UNUSED handle to an invocation
780 * @param[in] clientResponse Response from queries to remote servers.
781 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
782 * and OC_STACK_KEEP_TRANSACTION to keep it.
784 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
785 OCClientResponse *clientResponse)
787 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
788 VERIFY_NON_NULL(TAG, ctx, WARNING);
790 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
792 OCStackResult res = OC_STACK_OK;
793 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
794 otmCtx->ocDoHandle = NULL;
796 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
798 if(otmCtx && otmCtx->selectedDeviceInfo)
800 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
801 if(OC_STACK_OK != res)
803 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
804 SetResult(otmCtx, res);
805 return OC_STACK_DELETE_TRANSACTION;
808 //POST owner credential to new device according to security spec B.
809 res = PostOwnerCredential(otmCtx);
810 if(OC_STACK_OK != res)
813 "OwnerUuidUpdateHandler:Failed to send PosT request for onwer credential");
814 SetResult(otmCtx, res);
815 return OC_STACK_DELETE_TRANSACTION;
821 res = clientResponse->result;
822 OIC_LOG_V(ERROR, TAG, "OwnerUuidHandler : Unexpected result %d", res);
823 SetResult(otmCtx, res);
826 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
829 return OC_STACK_DELETE_TRANSACTION;
833 * Response handler for update operation mode.
835 * @param[in] ctx ctx value passed to callback from calling function.
836 * @param[in] UNUSED handle to an invocation
837 * @param[in] clientResponse Response from queries to remote servers.
838 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
839 * and OC_STACK_KEEP_TRANSACTION to keep it.
841 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
842 OCClientResponse *clientResponse)
844 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
846 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
847 VERIFY_NON_NULL(TAG, ctx, WARNING);
849 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
850 otmCtx->ocDoHandle = NULL;
852 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
854 OCStackResult res = OC_STACK_ERROR;
857 //Load secret for temporal secure session.
858 if(otmCtx->otmCallback.loadSecretCB)
860 res = otmCtx->otmCallback.loadSecretCB(otmCtx);
861 if(OC_STACK_OK != res)
863 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
864 SetResult(otmCtx, res);
865 return OC_STACK_DELETE_TRANSACTION;
869 //Save the current context instance to use on the dtls handshake callback
870 if(OC_STACK_OK != AddOTMContext(otmCtx,
871 otmCtx->selectedDeviceInfo->endpoint.addr,
872 otmCtx->selectedDeviceInfo->securePort))
874 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to add OTM Context into OTM List.");
875 SetResult(otmCtx, res);
876 return OC_STACK_DELETE_TRANSACTION;
879 //Try DTLS handshake to generate secure session
880 if(otmCtx->otmCallback.createSecureSessionCB)
882 res = otmCtx->otmCallback.createSecureSessionCB(otmCtx);
883 if(OC_STACK_OK != res)
885 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
886 SetResult(otmCtx, res);
887 return OC_STACK_DELETE_TRANSACTION;
893 OIC_LOG(ERROR, TAG, "Error while update operation mode");
894 SetResult(otmCtx, clientResponse->result);
897 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
900 return OC_STACK_DELETE_TRANSACTION;
904 * Response handler for update owner crendetial request.
906 * @param[in] ctx ctx value passed to callback from calling function.
907 * @param[in] UNUSED handle to an invocation
908 * @param[in] clientResponse Response from queries to remote servers.
909 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
910 * and OC_STACK_KEEP_TRANSACTION to keep it.
912 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
913 OCClientResponse *clientResponse)
915 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
916 VERIFY_NON_NULL(TAG, ctx, WARNING);
918 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
920 OCStackResult res = OC_STACK_OK;
921 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
922 otmCtx->ocDoHandle = NULL;
924 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
926 if(otmCtx && otmCtx->selectedDeviceInfo)
928 //Close the temporal secure session to verify the owner credential
929 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
930 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
931 CAResult_t caResult = CA_STATUS_OK;
932 caResult = CAcloseSslConnection(endpoint);
934 if(CA_STATUS_OK != caResult)
936 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
937 SetResult(otmCtx, caResult);
938 return OC_STACK_DELETE_TRANSACTION;
942 * If we select NULL cipher,
943 * client will select appropriate cipher suite according to server's cipher-suite list.
945 // TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 = 0xC037, /**< see RFC 5489 */
946 caResult = CASelectCipherSuite(0xC037, endpoint->adapter);
948 if(CA_STATUS_OK != caResult)
950 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
951 SetResult(otmCtx, caResult);
952 return OC_STACK_DELETE_TRANSACTION;
956 * in case of random PIN based OxM,
957 * revert get_psk_info callback of tinyDTLS to use owner credential.
959 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
961 OicUuid_t emptyUuid = { .id={0}};
962 SetUuidForPinBasedOxm(&emptyUuid);
964 caResult = CAregisterPskCredentialsHandler(GetDtlsPskCredentials);
966 if(CA_STATUS_OK != caResult)
968 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
969 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
970 return OC_STACK_DELETE_TRANSACTION;
974 otmCtx->selectedDeviceInfo->connType |= CT_FLAG_SECURE;
976 res = PostOwnerAcl(otmCtx);
977 if(OC_STACK_OK != res)
979 OIC_LOG(ERROR, TAG, "Failed to update owner ACL to new device");
980 SetResult(otmCtx, res);
981 return OC_STACK_DELETE_TRANSACTION;
987 res = clientResponse->result;
988 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
989 SetResult(otmCtx, res);
992 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
995 return OC_STACK_DELETE_TRANSACTION;
999 * Response handler for update owner ACL request.
1001 * @param[in] ctx ctx value passed to callback from calling function.
1002 * @param[in] UNUSED handle to an invocation
1003 * @param[in] clientResponse Response from queries to remote servers.
1004 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1005 * and OC_STACK_KEEP_TRANSACTION to keep it.
1007 static OCStackApplicationResult OwnerAclHandler(void *ctx, OCDoHandle UNUSED,
1008 OCClientResponse *clientResponse)
1010 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1011 VERIFY_NON_NULL(TAG, ctx, WARNING);
1013 OIC_LOG(DEBUG, TAG, "IN OwnerAclHandler");
1015 OCStackResult res = OC_STACK_OK;
1016 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1017 otmCtx->ocDoHandle = NULL;
1019 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1021 if(otmCtx && otmCtx->selectedDeviceInfo)
1023 //POST /oic/sec/doxm [{ ..., "owned":"TRUE" }]
1024 res = PostOwnershipInformation(otmCtx);
1025 if(OC_STACK_OK != res)
1027 OIC_LOG(ERROR, TAG, "Failed to update ownership information to new device");
1028 SetResult(otmCtx, res);
1034 res = clientResponse->result;
1035 OIC_LOG_V(ERROR, TAG, "OwnerAclHandler : Unexpected result %d", res);
1036 SetResult(otmCtx, res);
1039 OIC_LOG(DEBUG, TAG, "OUT OwnerAclHandler");
1042 return OC_STACK_DELETE_TRANSACTION;
1047 * Response handler for update owner information request.
1049 * @param[in] ctx ctx value passed to callback from calling function.
1050 * @param[in] UNUSED handle to an invocation
1051 * @param[in] clientResponse Response from queries to remote servers.
1052 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1053 * and OC_STACK_KEEP_TRANSACTION to keep it.
1055 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
1056 OCClientResponse *clientResponse)
1058 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
1059 VERIFY_NON_NULL(TAG, ctx, WARNING);
1061 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
1063 OCStackResult res = OC_STACK_OK;
1064 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1065 otmCtx->ocDoHandle = NULL;
1067 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1069 if(otmCtx && otmCtx->selectedDeviceInfo)
1071 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
1072 OIC_LOG(INFO, TAG, "Set Ready for provisioning state .");
1074 res = PostProvisioningStatus(otmCtx);
1075 if(OC_STACK_OK != res)
1077 OIC_LOG(ERROR, TAG, "Failed to update pstat");
1078 SetResult(otmCtx, res);
1084 res = clientResponse->result;
1085 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
1086 SetResult(otmCtx, res);
1089 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
1092 return OC_STACK_DELETE_TRANSACTION;
1096 * Response handler of update provisioning status.
1098 * @param[in] ctx ctx value passed to callback from calling function.
1099 * @param[in] UNUSED handle to an invocation
1100 * @param[in] clientResponse Response from queries to remote servers.
1101 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1102 * and OC_STACK_KEEP_TRANSACTION to keep it.
1104 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
1105 OCClientResponse *clientResponse)
1107 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
1109 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1110 VERIFY_NON_NULL(TAG, ctx, ERROR);
1112 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
1113 otmCtx->ocDoHandle = NULL;
1115 OCStackResult res = OC_STACK_OK;
1117 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1119 if(otmCtx && otmCtx->selectedDeviceInfo)
1121 OIC_LOG(INFO, TAG, "Device state is in Ready for Provisionig.");
1123 res = PostNormalOperationStatus(otmCtx);
1124 if(OC_STACK_OK != res)
1126 OIC_LOG(ERROR, TAG, "Failed to update pstat");
1127 SetResult(otmCtx, res);
1133 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1134 clientResponse->result);
1135 SetResult(otmCtx, clientResponse->result);
1139 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
1140 return OC_STACK_DELETE_TRANSACTION;
1144 * Response handler of update provisioning status to Ready for Normal..
1146 * @param[in] ctx ctx value passed to callback from calling function.
1147 * @param[in] UNUSED handle to an invocation
1148 * @param[in] clientResponse Response from queries to remote servers.
1149 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1150 * and OC_STACK_KEEP_TRANSACTION to keep it.
1152 static OCStackApplicationResult ReadyForNomalStatusHandler(void *ctx, OCDoHandle UNUSED,
1153 OCClientResponse *clientResponse)
1155 OIC_LOG_V(INFO, TAG, "IN ReadyForNomalStatusHandler.");
1157 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1158 VERIFY_NON_NULL(TAG, ctx, ERROR);
1160 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
1161 otmCtx->ocDoHandle = NULL;
1164 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1166 OIC_LOG(INFO, TAG, "Device state is in Ready for Normal Operation.");
1167 OCStackResult res = PDMSetDeviceState(&otmCtx->selectedDeviceInfo->doxm->deviceID,
1169 if (OC_STACK_OK == res)
1171 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
1172 SetResult(otmCtx, OC_STACK_OK);
1173 return OC_STACK_DELETE_TRANSACTION;
1177 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
1182 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1183 clientResponse->result);
1184 SetResult(otmCtx, clientResponse->result);
1188 OIC_LOG_V(INFO, TAG, "OUT ReadyForNomalStatusHandler.");
1189 return OC_STACK_DELETE_TRANSACTION;
1192 static OCStackResult PostOwnerCredential(OTMContext_t* otmCtx)
1194 OIC_LOG(DEBUG, TAG, "IN PostOwnerCredential");
1196 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1198 OIC_LOG(ERROR, TAG, "Invalid parameters");
1199 return OC_STACK_INVALID_PARAM;
1202 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1203 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1205 if(!PMGenerateQuery(true,
1206 deviceInfo->endpoint.addr, deviceInfo->securePort,
1207 deviceInfo->connType,
1208 query, sizeof(query), OIC_RSRC_CRED_URI))
1210 OIC_LOG(ERROR, TAG, "PostOwnerCredential : Failed to generate query");
1211 return OC_STACK_ERROR;
1213 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1214 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1217 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1218 return OC_STACK_NO_MEMORY;
1221 //Generate owner credential for new device
1222 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1223 const OicSecCred_t* ownerCredential = GetCredResourceData(&(deviceInfo->doxm->deviceID));
1224 if(!ownerCredential)
1226 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
1227 return OC_STACK_NO_RESOURCE;
1230 OicUuid_t credSubjectId = {.id={0}};
1231 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
1233 OicSecCred_t newCredential;
1234 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
1235 newCredential.next = NULL;
1237 //Set subject ID as PT's ID
1238 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
1240 //Fill private data as empty string
1241 newCredential.privateData.data = "";
1242 newCredential.privateData.len = 0;
1243 newCredential.privateData.encoding = ownerCredential->privateData.encoding;
1245 newCredential.publicData.data = NULL;
1246 newCredential.publicData.len = 0;
1249 //Send owner credential to new device : POST /oic/sec/cred [ owner credential ]
1250 if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData,
1251 &secPayload->payloadSize, secureFlag))
1253 OICFree(secPayload);
1254 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
1255 return OC_STACK_ERROR;
1257 OIC_LOG(DEBUG, TAG, "Cred Payload:");
1258 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1260 OCCallbackData cbData;
1261 cbData.cb = &OwnerCredentialHandler;
1262 cbData.context = (void *)otmCtx;
1264 OCStackResult res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query,
1265 &deviceInfo->endpoint, (OCPayload*)secPayload,
1266 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1267 if (res != OC_STACK_OK)
1269 OIC_LOG(ERROR, TAG, "OCStack resource error");
1274 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
1275 return OC_STACK_NO_RESOURCE;
1278 OIC_LOG(DEBUG, TAG, "OUT PostOwnerCredential");
1283 static OicSecAcl_t* GenerateOwnerAcl(const OicUuid_t* owner)
1285 OicSecAcl_t* ownerAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
1286 OicSecAce_t* ownerAce = (OicSecAce_t*)OICCalloc(1, sizeof(OicSecAce_t));
1287 OicSecRsrc_t* wildcardRsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
1288 if(NULL == ownerAcl || NULL == ownerAce || NULL == wildcardRsrc)
1290 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1293 LL_APPEND(ownerAcl->aces, ownerAce);
1294 LL_APPEND(ownerAce->resources, wildcardRsrc);
1296 //Set resource owner as PT
1297 memcpy(ownerAcl->rownerID.id, owner->id, sizeof(owner->id));
1299 //PT has full permission.
1300 ownerAce->permission = PERMISSION_FULL_CONTROL;
1302 //Set subject as PT's UUID
1303 memcpy(ownerAce->subjectuuid.id, owner->id, sizeof(owner->id));
1305 wildcardRsrc->href = OICStrdup(WILDCARD_RESOURCE_URI);
1306 if(NULL == wildcardRsrc->href)
1311 wildcardRsrc->interfaceLen = 1;
1312 wildcardRsrc->interfaces = (char**)OICMalloc(wildcardRsrc->interfaceLen * sizeof(char*));
1313 if(NULL == wildcardRsrc->interfaces)
1317 wildcardRsrc->interfaces[0] = OICStrdup(WILDCARD_RESOURCE_URI);
1318 if(NULL == wildcardRsrc->interfaces[0])
1323 wildcardRsrc->typeLen = 1;
1324 wildcardRsrc->types = (char**)OICMalloc(wildcardRsrc->typeLen * sizeof(char*));
1325 if(NULL == wildcardRsrc->types)
1329 wildcardRsrc->types[0] = OICStrdup(WILDCARD_RESOURCE_URI);
1330 if(NULL == wildcardRsrc->types[0])
1338 //in case of memory allocation failed, each resource should be removed individually.
1339 if(NULL == ownerAcl || NULL == ownerAce || NULL == wildcardRsrc)
1343 OICFree(wildcardRsrc);
1347 DeleteACLList(ownerAcl);
1353 * Function to update the owner ACL to new device.
1355 * @param[in] otmCtx Context value of ownership transfer.
1356 * @return OC_STACK_OK on success
1358 static OCStackResult PostOwnerAcl(OTMContext_t* otmCtx)
1360 OCStackResult res = OC_STACK_ERROR;
1362 OIC_LOG(DEBUG, TAG, "IN PostOwnerAcl");
1364 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1366 OIC_LOG(ERROR, TAG, "Invalid parameters");
1367 return OC_STACK_INVALID_PARAM;
1370 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1371 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1372 OicSecAcl_t* ownerAcl = NULL;
1374 if(!PMGenerateQuery(true,
1375 deviceInfo->endpoint.addr, deviceInfo->securePort,
1376 deviceInfo->connType,
1377 query, sizeof(query), OIC_RSRC_ACL_URI))
1379 OIC_LOG(ERROR, TAG, "Failed to generate query");
1380 return OC_STACK_ERROR;
1382 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1385 res = GetDoxmDeviceID(&ownerID);
1386 if(OC_STACK_OK != res)
1388 OIC_LOG(ERROR, TAG, "Failed to generate owner ACL");
1392 //Generate owner ACL for new device
1393 ownerAcl = GenerateOwnerAcl(&ownerID);
1394 if(NULL == ownerAcl)
1396 OIC_LOG(ERROR, TAG, "Failed to generate owner ACL");
1397 return OC_STACK_NO_MEMORY;
1400 //Generate ACL payload
1401 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1404 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1405 res = OC_STACK_NO_MEMORY;
1409 res = AclToCBORPayload(ownerAcl, &secPayload->securityData, &secPayload->payloadSize);
1410 if (OC_STACK_OK != res)
1412 OICFree(secPayload);
1413 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
1416 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1418 OIC_LOG(DEBUG, TAG, "Owner ACL Payload:");
1419 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1421 //Send owner ACL to new device : POST /oic/sec/cred [ owner credential ]
1422 OCCallbackData cbData;
1423 cbData.cb = &OwnerAclHandler;
1424 cbData.context = (void *)otmCtx;
1426 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query,
1427 &deviceInfo->endpoint, (OCPayload*)secPayload,
1428 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1429 if (res != OC_STACK_OK)
1431 OIC_LOG(ERROR, TAG, "OCStack resource error");
1435 OIC_LOG(DEBUG, TAG, "OUT PostOwnerAcl");
1438 DeleteACLList(ownerAcl);
1443 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx)
1445 OIC_LOG(DEBUG, TAG, "IN PostOwnerTransferModeToResource");
1447 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1449 OIC_LOG(ERROR, TAG, "Invalid parameters");
1450 return OC_STACK_INVALID_PARAM;
1453 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1454 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1456 if(!PMGenerateQuery(false,
1457 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1458 deviceInfo->connType,
1459 query, sizeof(query), OIC_RSRC_DOXM_URI))
1461 OIC_LOG(ERROR, TAG, "PostOwnerTransferModeToResource : Failed to generate query");
1462 return OC_STACK_ERROR;
1464 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1466 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1469 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1470 return OC_STACK_NO_MEMORY;
1473 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1474 OCStackResult res = otmCtx->otmCallback.createSelectOxmPayloadCB(otmCtx,
1475 &secPayload->securityData, &secPayload->payloadSize);
1476 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1478 OCPayloadDestroy((OCPayload *)secPayload);
1479 OIC_LOG(ERROR, TAG, "Error while converting bin to cbor");
1480 return OC_STACK_ERROR;
1483 OCCallbackData cbData;
1484 cbData.cb = &OwnerTransferModeHandler;
1485 cbData.context = (void *)otmCtx;
1487 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query,
1488 &deviceInfo->endpoint, (OCPayload *)secPayload,
1489 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1490 if (res != OC_STACK_OK)
1492 OIC_LOG(ERROR, TAG, "OCStack resource error");
1495 OIC_LOG(DEBUG, TAG, "OUT PostOwnerTransferModeToResource");
1500 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1502 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1504 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1506 OIC_LOG(ERROR, TAG, "Invailed parameters");
1507 return OC_STACK_INVALID_PARAM;
1510 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1511 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1512 if(!PMGenerateQuery(false,
1513 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1514 deviceInfo->connType,
1515 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1517 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1518 return OC_STACK_ERROR;
1520 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1522 OCCallbackData cbData;
1523 cbData.cb = &ListMethodsHandler;
1524 cbData.context = (void *)otmCtx;
1526 OCStackResult res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_GET, query, NULL, NULL,
1527 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1528 if (res != OC_STACK_OK)
1530 OIC_LOG(ERROR, TAG, "OCStack resource error");
1533 OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1538 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx)
1540 OIC_LOG(DEBUG, TAG, "IN PostOwnerUuid");
1542 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1544 OIC_LOG(ERROR, TAG, "Invailed parameters");
1545 return OC_STACK_INVALID_PARAM;
1548 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1549 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1550 if(!PMGenerateQuery(true,
1551 deviceInfo->endpoint.addr, deviceInfo->securePort,
1552 deviceInfo->connType,
1553 query, sizeof(query), OIC_RSRC_DOXM_URI))
1555 OIC_LOG(ERROR, TAG, "PostOwnerUuid : Failed to generate query");
1556 return OC_STACK_ERROR;
1558 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1560 //Post PT's uuid to new device
1561 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1564 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1565 return OC_STACK_NO_MEMORY;
1567 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1568 OCStackResult res = otmCtx->otmCallback.createOwnerTransferPayloadCB(
1569 otmCtx, &secPayload->securityData, &secPayload->payloadSize);
1570 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1572 OCPayloadDestroy((OCPayload *)secPayload);
1573 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to cbor.");
1574 return OC_STACK_INVALID_PARAM;
1576 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1578 OCCallbackData cbData;
1579 cbData.cb = &OwnerUuidUpdateHandler;
1580 cbData.context = (void *)otmCtx;
1583 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1584 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1585 if (res != OC_STACK_OK)
1587 OIC_LOG(ERROR, TAG, "OCStack resource error");
1590 OIC_LOG(DEBUG, TAG, "OUT PostOwnerUuid");
1595 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx)
1597 OIC_LOG(DEBUG, TAG, "IN PostOwnershipInformation");
1599 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1601 OIC_LOG(ERROR, TAG, "Invailed parameters");
1602 return OC_STACK_INVALID_PARAM;
1605 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1606 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1607 if(!PMGenerateQuery(true,
1608 deviceInfo->endpoint.addr, deviceInfo->securePort,
1609 deviceInfo->connType,
1610 query, sizeof(query), OIC_RSRC_DOXM_URI))
1612 OIC_LOG(ERROR, TAG, "PostOwnershipInformation : Failed to generate query");
1613 return OC_STACK_ERROR;
1615 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1617 //OwnershipInformationHandler
1618 OCSecurityPayload *secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1621 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1622 return OC_STACK_NO_MEMORY;
1625 otmCtx->selectedDeviceInfo->doxm->owned = true;
1627 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1628 OCStackResult res = DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm,
1629 &secPayload->securityData, &secPayload->payloadSize, true);
1630 if (OC_STACK_OK != res && NULL == secPayload->securityData)
1632 OCPayloadDestroy((OCPayload *)secPayload);
1633 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1634 return OC_STACK_INVALID_PARAM;
1637 OCCallbackData cbData;
1638 cbData.cb = &OwnershipInformationHandler;
1639 cbData.context = (void *)otmCtx;
1642 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1643 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1644 if (res != OC_STACK_OK)
1646 OIC_LOG(ERROR, TAG, "OCStack resource error");
1649 OIC_LOG(DEBUG, TAG, "OUT PostOwnershipInformation");
1654 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx)
1656 OIC_LOG(DEBUG, TAG, "IN PostUpdateOperationMode");
1658 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1660 return OC_STACK_INVALID_PARAM;
1663 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1664 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1665 if(!PMGenerateQuery(false,
1666 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1667 deviceInfo->connType,
1668 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1670 OIC_LOG(ERROR, TAG, "PostUpdateOperationMode : Failed to generate query");
1671 return OC_STACK_ERROR;
1673 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1675 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1678 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1679 return OC_STACK_NO_MEMORY;
1681 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1682 OCStackResult res = PstatToCBORPayload(deviceInfo->pstat, &secPayload->securityData,
1683 &secPayload->payloadSize, true);
1684 if (OC_STACK_OK != res)
1686 OCPayloadDestroy((OCPayload *)secPayload);
1687 OIC_LOG(ERROR, TAG, "Error while converting pstat to cbor.");
1688 return OC_STACK_INVALID_PARAM;
1691 OCCallbackData cbData;
1692 cbData.cb = &OperationModeUpdateHandler;
1693 cbData.context = (void *)otmCtx;
1695 res = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1696 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1697 if (res != OC_STACK_OK)
1699 OIC_LOG(ERROR, TAG, "OCStack resource error");
1702 OIC_LOG(DEBUG, TAG, "OUT PostUpdateOperationMode");
1707 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
1709 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
1710 OCStackResult res = OC_STACK_INVALID_PARAM;
1712 VERIFY_NON_NULL(TAG, selectedDevice, ERROR);
1713 VERIFY_NON_NULL(TAG, selectedDevice->doxm, ERROR);
1715 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1716 otmCtx->selectedDeviceInfo = selectedDevice;
1718 //Checking duplication of Device ID.
1719 bool isDuplicate = true;
1720 res = PDMIsDuplicateDevice(&selectedDevice->doxm->deviceID, &isDuplicate);
1721 if (OC_STACK_OK != res)
1723 OIC_LOG(ERROR, TAG, "Internal error in PDMIsDuplicateDevice");
1728 PdmDeviceState_t state = PDM_DEVICE_UNKNOWN;
1729 res = PDMGetDeviceState(&selectedDevice->doxm->deviceID, &state);
1730 if(OC_STACK_OK != res)
1732 OIC_LOG(ERROR, TAG, "Internal error in PDMGetDeviceState");
1733 SetResult(otmCtx, res);
1737 char* strUuid = NULL;
1738 res = ConvertUuidToStr(&selectedDevice->doxm->deviceID, &strUuid);
1739 if(OC_STACK_OK != res)
1741 OIC_LOG(ERROR, TAG, "Failed to convert UUID to str");
1742 SetResult(otmCtx, res);
1746 if(PDM_DEVICE_STALE == state)
1748 OIC_LOG(INFO, TAG, "Detected duplicated UUID in stale status, "
1749 "device status will revert back to initial status.");
1750 res = PDMSetDeviceState(&selectedDevice->doxm->deviceID, PDM_DEVICE_INIT);
1751 if(OC_STACK_OK != res)
1753 OIC_LOG(ERROR, TAG, "Internal error in PDMSetDeviceState");
1755 SetResult(otmCtx, res);
1759 else if(PDM_DEVICE_INIT == state)
1761 OIC_LOG_V(ERROR, TAG, "[%s]'s ownership transfer process is already started.", strUuid);
1763 SetResult(otmCtx, OC_STACK_DUPLICATE_REQUEST);
1768 OIC_LOG(ERROR, TAG, "Unknow device status while OTM.");
1770 SetResult(otmCtx, OC_STACK_ERROR);
1771 return OC_STACK_ERROR;
1776 res = PDMAddDevice(&selectedDevice->doxm->deviceID);
1777 if(OC_STACK_OK != res)
1779 OIC_LOG(ERROR, TAG, "Internal error in PDMAddDevice");
1780 SetResult(otmCtx, res);
1786 //Set to the lowest level OxM, and then find more higher level OxM.
1787 res = SelectProvisioningMethod(selectedDevice->doxm->oxm,
1788 selectedDevice->doxm->oxmLen,
1789 &selectedDevice->doxm->oxmSel);
1790 if(OC_STACK_OK != res)
1792 OIC_LOG(ERROR, TAG, "Failed to select the provisioning method");
1793 SetResult(otmCtx, res);
1796 OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
1798 res = OTMSetOTCallback(selectedDevice->doxm->oxmSel, &otmCtx->otmCallback);
1799 if(OC_STACK_OK != res)
1801 OIC_LOG_V(ERROR, TAG, "Error in OTMSetOTCallback : %d", res);
1805 //Send Req: POST /oic/sec/doxm [{..."OxmSel" :g_OTMCbDatas[Index of Selected OxM].OXMString,...}]
1806 res = PostOwnerTransferModeToResource(otmCtx);
1807 if(OC_STACK_OK != res)
1809 OIC_LOG(WARNING, TAG, "Failed to select the provisioning method");
1810 SetResult(otmCtx, res);
1814 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1815 //Register TLS event handler to catch the tls event while handshake
1816 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(DTLSHandshakeCB))
1818 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register TLS handshake callback.");
1820 #endif // __WITH_DTLS__ or __WITH_TLS__
1821 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
1827 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
1829 OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
1833 OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
1834 return OC_STACK_INVALID_PARAM;
1836 if(oxmType >= OIC_OXM_COUNT)
1838 OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
1839 return OC_STACK_INVALID_PARAM;
1842 // TODO: Remove this API, Please see the jira ticket IOT-1484
1844 OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
1850 * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
1852 OCStackResult OTMDoOwnershipTransfer(void* ctx,
1853 OCProvisionDev_t *selectedDevicelist,
1854 OCProvisionResultCB resultCallback)
1856 OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
1858 if (NULL == selectedDevicelist)
1860 return OC_STACK_INVALID_PARAM;
1862 if (NULL == resultCallback)
1864 return OC_STACK_INVALID_CALLBACK;
1867 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
1870 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
1871 return OC_STACK_NO_MEMORY;
1873 otmCtx->ctxResultCallback = resultCallback;
1874 otmCtx->ctxHasError = false;
1875 otmCtx->userCtx = ctx;
1876 OCProvisionDev_t* pCurDev = selectedDevicelist;
1878 //Counting number of selected devices.
1879 otmCtx->ctxResultArraySize = 0;
1880 while(NULL != pCurDev)
1882 otmCtx->ctxResultArraySize++;
1883 pCurDev = pCurDev->next;
1886 otmCtx->ctxResultArray =
1887 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
1888 if(NULL == otmCtx->ctxResultArray)
1890 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
1892 return OC_STACK_NO_MEMORY;
1894 pCurDev = selectedDevicelist;
1896 OCStackResult res = OC_STACK_OK;
1897 //Fill the device UUID for result array.
1898 for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
1900 memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
1901 pCurDev->doxm->deviceID.id,
1903 otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
1904 pCurDev = pCurDev->next;
1907 StartOwnershipTransfer(otmCtx, selectedDevicelist);
1909 OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
1913 OCStackResult OTMSetOxmAllowStatus(const OicSecOxm_t oxm, const bool allowStatus)
1915 OIC_LOG_V(INFO, TAG, "IN %s : oxm=%d, allow status=%s",
1916 __func__, oxm, (allowStatus ? "true" : "false"));
1918 if(OIC_OXM_COUNT <= oxm)
1920 return OC_STACK_INVALID_PARAM;
1923 g_OxmAllowStatus[oxm] = (allowStatus ? ALLOWED_OXM : NOT_ALLOWED_OXM);
1925 OIC_LOG_V(INFO, TAG, "OUT %s", __func__);
1930 OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx)
1932 OIC_LOG(INFO, TAG, "IN PostProvisioningStatus");
1934 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1936 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1937 return OC_STACK_INVALID_PARAM;
1940 //Change the TAKE_OWNER bit of CM to 0.
1941 otmCtx->selectedDeviceInfo->pstat->cm &= (~TAKE_OWNER);
1943 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1946 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1947 return OC_STACK_NO_MEMORY;
1949 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1950 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1951 &secPayload->securityData, &secPayload->payloadSize, true))
1953 OCPayloadDestroy((OCPayload *)secPayload);
1954 return OC_STACK_INVALID_JSON;
1956 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
1957 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1959 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1960 if(!PMGenerateQuery(true,
1961 otmCtx->selectedDeviceInfo->endpoint.addr,
1962 otmCtx->selectedDeviceInfo->securePort,
1963 otmCtx->selectedDeviceInfo->connType,
1964 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1966 OIC_LOG(ERROR, TAG, "PostProvisioningStatus : Failed to generate query");
1967 return OC_STACK_ERROR;
1969 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1971 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1972 cbData.cb = &ProvisioningStatusHandler;
1973 cbData.context = (void*)otmCtx;
1975 OCStackResult ret = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1976 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1977 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1978 if (ret != OC_STACK_OK)
1980 OIC_LOG(ERROR, TAG, "OCStack resource error");
1983 OIC_LOG(INFO, TAG, "OUT PostProvisioningStatus");
1988 OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx)
1990 OIC_LOG(INFO, TAG, "IN PostNormalOperationStatus");
1992 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1994 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1995 return OC_STACK_INVALID_PARAM;
1999 otmCtx->selectedDeviceInfo->pstat->isOp = true;
2001 OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
2004 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
2005 return OC_STACK_NO_MEMORY;
2007 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2008 if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
2009 &secPayload->securityData, &secPayload->payloadSize, true))
2011 OCPayloadDestroy((OCPayload *)secPayload);
2012 return OC_STACK_INVALID_JSON;
2014 OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
2015 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2017 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2018 if(!PMGenerateQuery(true,
2019 otmCtx->selectedDeviceInfo->endpoint.addr,
2020 otmCtx->selectedDeviceInfo->securePort,
2021 otmCtx->selectedDeviceInfo->connType,
2022 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2024 OIC_LOG(ERROR, TAG, "PostNormalOperationStatus : Failed to generate query");
2025 return OC_STACK_ERROR;
2027 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2029 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2030 cbData.cb = &ReadyForNomalStatusHandler;
2031 cbData.context = (void*)otmCtx;
2033 OCStackResult ret = OCDoResource(&otmCtx->ocDoHandle, OC_REST_POST, query, 0, (OCPayload*)secPayload,
2034 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2035 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
2036 if (ret != OC_STACK_OK)
2038 OIC_LOG(ERROR, TAG, "OCStack resource error");
2041 OIC_LOG(INFO, TAG, "OUT PostNormalOperationStatus");