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"
65 * Array to store the callbacks for each owner transfer method.
67 static OTMCallbackData_t g_OTMDatas[OIC_OXM_COUNT];
70 * Variable for storing provisioning tool's provisioning capabilities
71 * Must be in decreasing order of preference. More prefered method should
72 * have lower array index.
74 static OicSecDpom_t gProvisioningToolCapability[] = { SINGLE_SERVICE_CLIENT_DRIVEN };
77 * Number of supported provisioning methods
78 * current version supports only one.
80 static size_t gNumOfProvisioningMethodsPT = 1;
83 * Variables for pointing the OTMContext to be used in the DTLS handshake result callback.
85 static OTMContext_t* g_otmCtx = NULL;
88 * Function to select appropriate provisioning method.
90 * @param[in] supportedMethods Array of supported methods
91 * @param[in] numberOfMethods number of supported methods
92 * @param[out] selectedMethod Selected methods
93 * @return OC_STACK_OK on success
95 static OCStackResult SelectProvisioningMethod(const OicSecOxm_t *supportedMethods,
96 size_t numberOfMethods,
97 OicSecOxm_t *selectedMethod)
99 OIC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
101 if(numberOfMethods == 0 || !supportedMethods)
103 OIC_LOG(WARNING, TAG, "Could not find a supported OxM.");
104 return OC_STACK_ERROR;
107 *selectedMethod = supportedMethods[0];
108 for(size_t i = 0; i < numberOfMethods; i++)
110 if(*selectedMethod < supportedMethods[i])
112 *selectedMethod = supportedMethods[i];
120 * Function to select operation mode.This function will return most secure common operation mode.
122 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
123 * @param[out] selectedMode selected operation mode
124 * @return OC_STACK_OK on success
126 static void SelectOperationMode(const OCProvisionDev_t *selectedDeviceInfo,
127 OicSecDpom_t *selectedMode)
129 OIC_LOG(DEBUG, TAG, "IN SelectOperationMode");
134 while (i < gNumOfProvisioningMethodsPT && j < selectedDeviceInfo->pstat->smLen)
136 if (gProvisioningToolCapability[i] < selectedDeviceInfo->pstat->sm[j])
140 else if (selectedDeviceInfo->pstat->sm[j] < gProvisioningToolCapability[i])
144 else /* if gProvisioningToolCapability[i] == deviceSupportedMethods[j] */
146 *selectedMode = gProvisioningToolCapability[j];
150 OIC_LOG_V(DEBUG, TAG, "Selected Operation Mode = %d", *selectedMode);
152 OIC_LOG(DEBUG, TAG, "OUT SelectOperationMode");
156 * Function to start ownership transfer.
157 * This function will send the first request for provisioning,
158 * The next request message is sent from the response handler for this request.
160 * @param[in] ctx context value passed to callback from calling function.
161 * @param[in] selectedDevice selected device information to performing provisioning.
162 * @return OC_STACK_OK on success
164 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
167 * Function to update owner transfer mode
169 * @param[in] otmCtx Context value of ownership transfer.
170 * @return OC_STACK_OK on success
172 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx);
175 * Function to send request to resource to get its pstat resource information.
177 * @param[in] otmCtx Context value of ownership transfer.
178 * @return OC_STACK_OK on success
180 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx);
184 * Function to send uuid of owner device to new device.
185 * This function would update 'owner of doxm' as UUID for provisioning tool.
187 * @param[in] otmCtx Context value of ownership transfer.
188 * @return OC_STACK_OK on success
190 static OCStackResult PutOwnerUuid(OTMContext_t* otmCtx);
193 * Function to update the operation mode. As per the spec. Operation mode in client driven
194 * single service provisioning it will be updated to 0x3
196 * @param[in] otmCtx Context value of ownership transfer.
197 * @return OC_STACK_OK on success
199 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx);
202 * Function to update the owner credential to new device
204 * @param[in] otmCtx Context value of ownership transfer.
205 * @param[in] selectedOperationMode selected operation mode
206 * @return OC_STACK_OK on success
208 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx);
211 * Function to send ownerShip info.
212 * This function would update 'owned of doxm' as true.
214 * @param[in] otmCtx Context value of ownership transfer.
215 * @return OC_STACK_OK on success
217 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx);
220 * Function to update pstat when finalize provisioning.
221 * This function would update 'cm' as bx0011,1100 and 'tm' as bx0000,0000.
223 * @param[in] ctx context value passed to callback from calling function.
224 * @param[in] selectedDevice selected device information to performing provisioning.
225 * @return OC_STACK_OK on success
227 static OCStackResult PutProvisioningStatus(OTMContext_t* otmCtx);
230 * Function to finalize provisioning.
231 * This function will send default ACL and commit hash.
233 * @param[in] otmCtx Context value of ownership transfer.
234 * @return OC_STACK_OK on success
236 static OCStackResult FinalizeProvisioning(OTMContext_t* otmCtx);
239 static bool IsComplete(OTMContext_t* otmCtx)
241 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
243 if(OC_STACK_CONTINUE == otmCtx->ctxResultArray[i].res)
253 * Function to save the result of provisioning.
255 * @param[in,out] otmCtx Context value of ownership transfer.
256 * @param[in] res result of provisioning
258 static void SetResult(OTMContext_t* otmCtx, const OCStackResult res)
260 OIC_LOG_V(DEBUG, TAG, "IN SetResult : %d ", res);
264 OIC_LOG(WARNING, TAG, "OTMContext is NULL");
268 if(otmCtx->selectedDeviceInfo)
270 //Revert psk_info callback and new deivce uuid in case of random PIN OxM
271 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
273 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
275 OIC_LOG(WARNING, TAG, "Failed to revert is DTLS credential handler.");
277 OicUuid_t emptyUuid = { .id={0}};
278 SetUuidForRandomPinOxm(&emptyUuid);
281 for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
283 if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
284 otmCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
286 otmCtx->ctxResultArray[i].res = res;
287 if(OC_STACK_OK != res)
289 otmCtx->ctxHasError = true;
296 //If all request is completed, invoke the user callback.
297 if(IsComplete(otmCtx))
299 otmCtx->ctxResultCallback(otmCtx->userCtx, otmCtx->ctxResultArraySize,
300 otmCtx->ctxResultArray, otmCtx->ctxHasError);
301 OICFree(otmCtx->ctxResultArray);
306 if(OC_STACK_OK != StartOwnershipTransfer(otmCtx,
307 otmCtx->selectedDeviceInfo->next))
309 OIC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
314 OIC_LOG(DEBUG, TAG, "OUT SetResult");
318 * Function to handle the handshake result in OTM.
319 * This function will be invoked after DTLS handshake
320 * @param endPoint [IN] The remote endpoint.
321 * @param errorInfo [IN] Error information from the endpoint.
324 void DTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
326 if(NULL != g_otmCtx && NULL != g_otmCtx->selectedDeviceInfo &&
327 NULL != endpoint && NULL != info)
329 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
330 endpoint->addr, endpoint->port, info->result);
332 OicSecDoxm_t* newDevDoxm = g_otmCtx->selectedDeviceInfo->doxm;
334 if(NULL != newDevDoxm)
336 OicUuid_t emptyUuid = {.id={0}};
338 //Make sure the address matches.
339 if(strncmp(g_otmCtx->selectedDeviceInfo->endpoint.addr,
341 sizeof(endpoint->addr)) == 0 &&
342 g_otmCtx->selectedDeviceInfo->securePort == endpoint->port)
344 OCStackResult res = OC_STACK_ERROR;
346 //If temporal secure sesstion established successfully
347 if(CA_STATUS_OK == info->result &&
348 false == newDevDoxm->owned &&
349 memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) == 0)
351 //Send request : PUT /oic/sec/doxm [{... , "devowner":"PT's UUID"}]
352 res = PutOwnerUuid(g_otmCtx);
353 if(OC_STACK_OK != res)
355 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
356 SetResult(g_otmCtx, res);
359 //In case of authentication failure
360 else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
362 //in case of error from owner credential
363 if(memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) != 0 &&
364 true == newDevDoxm->owned)
366 OIC_LOG(ERROR, TAG, "The owner credential may incorrect.");
368 if(OC_STACK_OK != RemoveCredential(&(newDevDoxm->deviceID)))
370 OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
372 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
374 //in case of error from wrong PIN, re-start the ownership transfer
375 else if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
377 OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
379 memcpy(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
380 newDevDoxm->owned = false;
381 g_otmCtx->attemptCnt++;
383 if(WRONG_PIN_MAX_ATTEMP > g_otmCtx->attemptCnt)
385 res = StartOwnershipTransfer(g_otmCtx, g_otmCtx->selectedDeviceInfo);
386 if(OC_STACK_OK != res)
388 SetResult(g_otmCtx, res);
389 OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
394 OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
395 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
400 OIC_LOG(ERROR, TAG, "Failed to establish secure session.");
401 SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
410 * Function to save ownerPSK at provisioning tool end.
412 * @param[in] selectedDeviceInfo selected device information to performing provisioning.
413 * @return OC_STACK_OK on success
415 static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
417 OIC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
419 OCStackResult res = OC_STACK_ERROR;
421 CAEndpoint_t endpoint;
422 memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
423 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
424 endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
425 endpoint.port = selectedDeviceInfo->securePort;
427 OicUuid_t ptDeviceID = {.id={0}};
428 if (OC_STACK_OK != GetDoxmDeviceID(&ptDeviceID))
430 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
434 uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
436 //Generating OwnerPSK
437 CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
438 (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
439 strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
440 ptDeviceID.id, sizeof(ptDeviceID.id),
441 selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
442 ownerPSK, OWNER_PSK_LENGTH_128);
444 if (CA_STATUS_OK == pskRet)
446 OIC_LOG(INFO, TAG,"ownerPSK dump:\n");
447 OIC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
448 //Generating new credential for provisioning tool
452 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(ownerPSK)) + 1] = {};
453 B64Result b64Ret = b64Encode(ownerPSK, sizeof(ownerPSK), base64Buff, sizeof(base64Buff),
455 VERIFY_SUCCESS(TAG, B64_OK == b64Ret, ERROR);
457 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
458 SYMMETRIC_PAIR_WISE_KEY, NULL,
459 base64Buff, ownLen, &ptDeviceID);
460 VERIFY_NON_NULL(TAG, cred, ERROR);
462 res = AddCredential(cred);
463 if(res != OC_STACK_OK)
465 DeleteCredList(cred);
471 OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
474 OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
480 * Callback handler for OwnerShipTransferModeHandler API.
482 * @param[in] ctx ctx value passed to callback from calling function.
483 * @param[in] UNUSED handle to an invocation
484 * @param[in] clientResponse Response from queries to remote servers.
485 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
486 * and OC_STACK_KEEP_TRANSACTION to keep it.
488 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
489 OCClientResponse *clientResponse)
491 OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
493 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
494 VERIFY_NON_NULL(TAG, ctx, WARNING);
496 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
498 if(clientResponse->result == OC_STACK_OK)
500 OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
501 //Send request : GET /oic/sec/pstat
502 OCStackResult res = GetProvisioningStatusResource(otmCtx);
503 if(OC_STACK_OK != res)
505 OIC_LOG(WARNING, TAG, "Failed to get pstat information");
506 SetResult(otmCtx, res);
511 OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
512 clientResponse->result);
513 SetResult(otmCtx, clientResponse->result);
516 OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
519 return OC_STACK_DELETE_TRANSACTION;
523 * Callback handler for ProvisioningStatusResouceHandler API.
525 * @param[in] ctx ctx value passed to callback from calling function.
526 * @param[in] UNUSED handle to an invocation
527 * @param[in] clientResponse Response from queries to remote servers.
528 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
529 * and OC_STACK_KEEP_TRANSACTION to keep it.
531 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
532 OCClientResponse *clientResponse)
534 OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
536 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
537 VERIFY_NON_NULL(TAG, ctx, WARNING);
539 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
541 if (OC_STACK_OK == clientResponse->result)
543 if (NULL == clientResponse->payload)
545 OIC_LOG(INFO, TAG, "Skiping Null payload");
546 SetResult(otmCtx, OC_STACK_ERROR);
547 return OC_STACK_DELETE_TRANSACTION;
550 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
552 OIC_LOG(INFO, TAG, "Unknown payload type");
553 SetResult(otmCtx, OC_STACK_ERROR);
554 return OC_STACK_DELETE_TRANSACTION;
557 OicSecPstat_t* pstat = JSONToPstatBin(
558 ((OCSecurityPayload*)clientResponse->payload)->securityData);
561 OIC_LOG(ERROR, TAG, "Error while converting json to pstat bin");
562 SetResult(otmCtx, OC_STACK_ERROR);
563 return OC_STACK_DELETE_TRANSACTION;
565 otmCtx->selectedDeviceInfo->pstat = pstat;
567 //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
568 SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
570 //Send request : PUT /oic/sec/pstat [{"om":"bx11", .. }]
571 OCStackResult res = PutUpdateOperationMode(otmCtx);
572 if (OC_STACK_OK != res)
574 OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
575 SetResult(otmCtx, res);
580 OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
581 clientResponse->result);
582 SetResult(otmCtx, clientResponse->result);
585 OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
587 return OC_STACK_DELETE_TRANSACTION;
591 * Response handler for update owner uuid request.
593 * @param[in] ctx ctx value passed to callback from calling function.
594 * @param[in] UNUSED handle to an invocation
595 * @param[in] clientResponse Response from queries to remote servers.
596 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
597 * and OC_STACK_KEEP_TRANSACTION to keep it.
599 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
600 OCClientResponse *clientResponse)
602 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
603 VERIFY_NON_NULL(TAG, ctx, WARNING);
605 OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
607 OCStackResult res = OC_STACK_OK;
608 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
610 if(OC_STACK_OK == clientResponse->result)
612 if(otmCtx && otmCtx->selectedDeviceInfo)
614 res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
615 if(OC_STACK_OK != res)
617 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
618 SetResult(otmCtx, res);
619 return OC_STACK_DELETE_TRANSACTION;
622 //PUT owner credential to new device according to security spec B.
623 res = PutOwnerCredential(otmCtx);
624 if(OC_STACK_OK != res)
627 "OwnerUuidUpdateHandler:Failed to send PUT request for onwer credential");
628 SetResult(otmCtx, res);
629 return OC_STACK_DELETE_TRANSACTION;
635 res = clientResponse->result;
636 OIC_LOG_V(ERROR, TAG, "OwnerUuidHandler : Unexpected result %d", res);
637 SetResult(otmCtx, res);
640 OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
643 return OC_STACK_DELETE_TRANSACTION;
647 * Response handler for update operation mode.
649 * @param[in] ctx ctx value passed to callback from calling function.
650 * @param[in] UNUSED handle to an invocation
651 * @param[in] clientResponse Response from queries to remote servers.
652 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
653 * and OC_STACK_KEEP_TRANSACTION to keep it.
655 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
656 OCClientResponse *clientResponse)
658 OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
660 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
661 VERIFY_NON_NULL(TAG, ctx, WARNING);
663 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
665 if (OC_STACK_OK == clientResponse->result)
667 OCStackResult res = OC_STACK_ERROR;
668 OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
670 //Load secret for temporal secure session.
671 if(g_OTMDatas[selOxm].loadSecretCB)
673 res = g_OTMDatas[selOxm].loadSecretCB(otmCtx);
674 if(OC_STACK_OK != res)
676 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
677 SetResult(otmCtx, res);
678 return OC_STACK_DELETE_TRANSACTION;
682 //It will be used in handshake event handler
685 //Try DTLS handshake to generate secure session
686 if(g_OTMDatas[selOxm].createSecureSessionCB)
688 res = g_OTMDatas[selOxm].createSecureSessionCB(otmCtx);
689 if(OC_STACK_OK != res)
691 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
692 SetResult(otmCtx, res);
693 return OC_STACK_DELETE_TRANSACTION;
699 OIC_LOG(ERROR, TAG, "Error while update operation mode");
700 SetResult(otmCtx, clientResponse->result);
703 OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
706 return OC_STACK_DELETE_TRANSACTION;
710 * Response handler for update owner crendetial request.
712 * @param[in] ctx ctx value passed to callback from calling function.
713 * @param[in] UNUSED handle to an invocation
714 * @param[in] clientResponse Response from queries to remote servers.
715 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
716 * and OC_STACK_KEEP_TRANSACTION to keep it.
718 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
719 OCClientResponse *clientResponse)
721 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
722 VERIFY_NON_NULL(TAG, ctx, WARNING);
724 OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
726 OCStackResult res = OC_STACK_OK;
727 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
729 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
731 if(otmCtx && otmCtx->selectedDeviceInfo)
733 //Close the temporal secure session to verify the owner credential
734 CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
735 endpoint->port = otmCtx->selectedDeviceInfo->securePort;
736 CAResult_t caResult = CACloseDtlsSession(endpoint);
737 if(CA_STATUS_OK != caResult)
739 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
740 SetResult(otmCtx, caResult);
741 return OC_STACK_DELETE_TRANSACTION;
745 * If we select NULL cipher,
746 * client will select appropriate cipher suite according to server's cipher-suite list.
748 caResult = CASelectCipherSuite(TLS_NULL_WITH_NULL_NULL);
749 if(CA_STATUS_OK != caResult)
751 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
752 SetResult(otmCtx, caResult);
753 return OC_STACK_DELETE_TRANSACTION;
757 * in case of random PIN based OxM,
758 * revert get_psk_info callback of tinyDTLS to use owner credential.
760 if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
762 OicUuid_t emptyUuid = { .id={0}};
763 SetUuidForRandomPinOxm(&emptyUuid);
765 if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskCredentials))
767 OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
768 SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
769 return OC_STACK_DELETE_TRANSACTION;
773 //PUT /oic/sec/doxm [{ ..., "owned":"TRUE" }]
774 res = PutOwnershipInformation(otmCtx);
775 if(OC_STACK_OK != res)
777 OIC_LOG(ERROR, TAG, "Failed to put ownership information to new device");
778 SetResult(otmCtx, res);
779 return OC_STACK_DELETE_TRANSACTION;
785 res = clientResponse->result;
786 OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
787 SetResult(otmCtx, res);
790 OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
793 return OC_STACK_DELETE_TRANSACTION;
798 * Response handler for update owner information request.
800 * @param[in] ctx ctx value passed to callback from calling function.
801 * @param[in] UNUSED handle to an invocation
802 * @param[in] clientResponse Response from queries to remote servers.
803 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
804 * and OC_STACK_KEEP_TRANSACTION to keep it.
806 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
807 OCClientResponse *clientResponse)
809 VERIFY_NON_NULL(TAG, clientResponse, WARNING);
810 VERIFY_NON_NULL(TAG, ctx, WARNING);
812 OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
814 OCStackResult res = OC_STACK_OK;
815 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
817 if(OC_STACK_OK == clientResponse->result)
819 if(otmCtx && otmCtx->selectedDeviceInfo)
821 OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
822 OIC_LOG(INFO, TAG, "Start defualt ACL & commit-hash provisioning.");
824 res = PutProvisioningStatus(otmCtx);
825 if(OC_STACK_OK != res)
827 OIC_LOG(ERROR, TAG, "Failed to update pstat");
828 SetResult(otmCtx, res);
834 res = clientResponse->result;
835 OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
836 SetResult(otmCtx, res);
839 OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
842 return OC_STACK_DELETE_TRANSACTION;
846 * Response handler of update provisioning status.
848 * @param[in] ctx ctx value passed to callback from calling function.
849 * @param[in] UNUSED handle to an invocation
850 * @param[in] clientResponse Response from queries to remote servers.
851 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
852 * and OC_STACK_KEEP_TRANSACTION to keep it.
854 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
855 OCClientResponse *clientResponse)
857 OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
859 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
860 VERIFY_NON_NULL(TAG, ctx, ERROR);
862 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
864 if(OC_STACK_OK == clientResponse->result)
866 OCStackResult res = FinalizeProvisioning(otmCtx);
867 if (OC_STACK_OK != res)
869 OIC_LOG_V(INFO, TAG, "Failed to finalize provisioning.");
870 SetResult(otmCtx, res);
871 return OC_STACK_DELETE_TRANSACTION;
876 OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
877 return OC_STACK_DELETE_TRANSACTION;
881 * Callback handler of finalize provisioning.
883 * @param[in] ctx ctx value passed to callback from calling function.
884 * @param[in] UNUSED handle to an invocation
885 * @param[in] clientResponse Response from queries to remote servers.
886 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
887 * and OC_STACK_KEEP_TRANSACTION to keep it.
889 static OCStackApplicationResult FinalizeProvisioningCB(void *ctx, OCDoHandle UNUSED,
890 OCClientResponse *clientResponse)
892 OIC_LOG_V(INFO, TAG, "IN ProvisionDefaultACLCB.");
894 VERIFY_NON_NULL(TAG, clientResponse, ERROR);
895 VERIFY_NON_NULL(TAG, ctx, ERROR);
897 OTMContext_t* otmCtx = (OTMContext_t*) ctx;
900 if (OC_STACK_RESOURCE_CREATED == clientResponse->result)
902 OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
903 if (OC_STACK_OK == res)
905 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
906 SetResult(otmCtx, OC_STACK_OK);
907 return OC_STACK_DELETE_TRANSACTION;
911 OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
916 OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
917 clientResponse->result);
918 SetResult(otmCtx, clientResponse->result);
921 return OC_STACK_DELETE_TRANSACTION;
924 static OCStackResult PutOwnerCredential(OTMContext_t* otmCtx)
926 OIC_LOG(DEBUG, TAG, "IN PutOwnerCredential");
928 if(!otmCtx || !otmCtx->selectedDeviceInfo)
930 OIC_LOG(ERROR, TAG, "Invalid parameters");
931 return OC_STACK_INVALID_PARAM;
934 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
935 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
937 if(!PMGenerateQuery(true,
938 deviceInfo->endpoint.addr, deviceInfo->securePort,
939 deviceInfo->connType,
940 query, sizeof(query), OIC_RSRC_CRED_URI))
942 OIC_LOG(ERROR, TAG, "PutOwnerCredential : Failed to generate query");
943 return OC_STACK_ERROR;
945 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
946 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
949 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
950 return OC_STACK_NO_MEMORY;
953 //Generate owner credential for new device
954 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
955 OicSecCred_t* ownerCredential =
956 GetCredResourceData(&(deviceInfo->doxm->deviceID));
959 OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
960 return OC_STACK_NO_RESOURCE;
963 OicUuid_t credSubjectId = {.id={0}};
964 if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
966 OicSecCred_t newCredential;
967 memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
968 newCredential.next = NULL;
970 //Set subject ID as PT's ID
971 memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
973 //Fill private data as empty string
974 newCredential.privateData.data = NULL;
976 //Send owner credential to new device : PUT /oic/sec/cred [ owner credential ]
977 secPayload->securityData = BinToCredJSON(&newCredential);
978 if (NULL == secPayload->securityData)
981 OIC_LOG(ERROR, TAG, "Error while converting bin to json");
982 return OC_STACK_ERROR;
984 OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData);
986 OCCallbackData cbData;
987 cbData.cb = &OwnerCredentialHandler;
988 cbData.context = (void *)otmCtx;
990 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
991 &deviceInfo->endpoint, (OCPayload*)secPayload,
992 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
993 if (res != OC_STACK_OK)
995 OIC_LOG(ERROR, TAG, "OCStack resource error");
1000 OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
1001 return OC_STACK_NO_RESOURCE;
1004 OIC_LOG(DEBUG, TAG, "OUT PutOwnerCredential");
1009 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx)
1011 OIC_LOG(DEBUG, TAG, "IN PutOwnerTransferModeToResource");
1013 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1015 OIC_LOG(ERROR, TAG, "Invalid parameters");
1016 return OC_STACK_INVALID_PARAM;
1019 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1020 OicSecOxm_t selectedOxm = deviceInfo->doxm->oxmSel;
1021 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1023 if(!PMGenerateQuery(false,
1024 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1025 deviceInfo->connType,
1026 query, sizeof(query), OIC_RSRC_DOXM_URI))
1028 OIC_LOG(ERROR, TAG, "PutOwnerTransferModeToResource : Failed to generate query");
1029 return OC_STACK_ERROR;
1031 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1032 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1035 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1036 return OC_STACK_NO_MEMORY;
1038 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1039 secPayload->securityData = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx);
1040 if (NULL == secPayload->securityData)
1042 OICFree(secPayload);
1043 OIC_LOG(ERROR, TAG, "Error while converting bin to json");
1044 return OC_STACK_ERROR;
1047 OCCallbackData cbData;
1048 cbData.cb = &OwnerTransferModeHandler;
1049 cbData.context = (void *)otmCtx;
1051 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
1052 &deviceInfo->endpoint, (OCPayload*)secPayload,
1053 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1054 if (res != OC_STACK_OK)
1056 OIC_LOG(ERROR, TAG, "OCStack resource error");
1059 OIC_LOG(DEBUG, TAG, "OUT PutOwnerTransferModeToResource");
1064 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1066 OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1068 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1070 OIC_LOG(ERROR, TAG, "Invailed parameters");
1071 return OC_STACK_INVALID_PARAM;
1074 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1075 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1076 if(!PMGenerateQuery(false,
1077 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1078 deviceInfo->connType,
1079 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1081 OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1082 return OC_STACK_ERROR;
1084 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1086 OCCallbackData cbData;
1087 cbData.cb = &ListMethodsHandler;
1088 cbData.context = (void *)otmCtx;
1090 OCStackResult res = OCDoResource(NULL, OC_REST_GET, query, NULL, NULL,
1091 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1092 if (res != OC_STACK_OK)
1094 OIC_LOG(ERROR, TAG, "OCStack resource error");
1097 OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1102 static OCStackResult PutOwnerUuid(OTMContext_t* otmCtx)
1104 OIC_LOG(DEBUG, TAG, "IN PutOwnerUuid");
1106 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1108 OIC_LOG(ERROR, TAG, "Invailed parameters");
1109 return OC_STACK_INVALID_PARAM;
1112 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1113 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1114 if(!PMGenerateQuery(true,
1115 deviceInfo->endpoint.addr, deviceInfo->securePort,
1116 deviceInfo->connType,
1117 query, sizeof(query), OIC_RSRC_DOXM_URI))
1119 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1120 return OC_STACK_ERROR;
1122 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1124 //PUT PT's uuid to new device
1125 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1128 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1129 return OC_STACK_NO_MEMORY;
1131 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1132 secPayload->securityData =
1133 g_OTMDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(otmCtx);
1134 if (NULL == secPayload->securityData)
1136 OICFree(secPayload);
1137 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1138 return OC_STACK_INVALID_PARAM;
1140 OIC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData);
1142 OCCallbackData cbData;
1143 cbData.cb = &OwnerUuidUpdateHandler;
1144 cbData.context = (void *)otmCtx;
1147 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1148 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1149 if (res != OC_STACK_OK)
1151 OIC_LOG(ERROR, TAG, "OCStack resource error");
1154 OIC_LOG(DEBUG, TAG, "OUT PutOwnerUuid");
1159 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx)
1161 OIC_LOG(DEBUG, TAG, "IN PutOwnershipInformation");
1163 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1165 OIC_LOG(ERROR, TAG, "Invailed parameters");
1166 return OC_STACK_INVALID_PARAM;
1169 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1170 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1171 if(!PMGenerateQuery(true,
1172 deviceInfo->endpoint.addr, deviceInfo->securePort,
1173 deviceInfo->connType,
1174 query, sizeof(query), OIC_RSRC_DOXM_URI))
1176 OIC_LOG(ERROR, TAG, "PutOwnershipInformation : Failed to generate query");
1177 return OC_STACK_ERROR;
1179 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1181 //OwnershipInformationHandler
1182 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1185 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1186 return OC_STACK_NO_MEMORY;
1189 otmCtx->selectedDeviceInfo->doxm->owned = true;
1190 secPayload->securityData = BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
1191 if (NULL == secPayload->securityData)
1193 OICFree(secPayload);
1194 OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1195 return OC_STACK_INVALID_PARAM;
1197 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1199 OCCallbackData cbData;
1200 cbData.cb = &OwnershipInformationHandler;
1201 cbData.context = (void *)otmCtx;
1204 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1205 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1206 if (res != OC_STACK_OK)
1208 OIC_LOG(ERROR, TAG, "OCStack resource error");
1211 OIC_LOG(DEBUG, TAG, "OUT PutOwnershipInformation");
1216 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx)
1218 OIC_LOG(DEBUG, TAG, "IN PutUpdateOperationMode");
1220 if(!otmCtx || !otmCtx->selectedDeviceInfo)
1222 return OC_STACK_INVALID_PARAM;
1225 OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1226 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1227 if(!PMGenerateQuery(false,
1228 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1229 deviceInfo->connType,
1230 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1232 OIC_LOG(ERROR, TAG, "PutUpdateOperationMode : Failed to generate query");
1233 return OC_STACK_ERROR;
1235 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1237 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1240 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1241 return OC_STACK_NO_MEMORY;
1243 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1244 secPayload->securityData = BinToPstatJSON(deviceInfo->pstat);
1245 if (NULL == secPayload->securityData)
1247 OICFree(secPayload);
1248 OIC_LOG(ERROR, TAG, "Error while converting pstat bin to json");
1249 return OC_STACK_INVALID_PARAM;
1252 OCCallbackData cbData;
1253 cbData.cb = &OperationModeUpdateHandler;
1254 cbData.context = (void *)otmCtx;
1256 OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1257 deviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1258 if (res != OC_STACK_OK)
1260 OIC_LOG(ERROR, TAG, "OCStack resource error");
1263 OIC_LOG(DEBUG, TAG, "OUT PutUpdateOperationMode");
1268 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
1270 OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
1271 OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1272 otmCtx->selectedDeviceInfo = selectedDevice;
1274 //Set to the lowest level OxM, and then find more higher level OxM.
1275 OCStackResult res = SelectProvisioningMethod(selectedDevice->doxm->oxm,
1276 selectedDevice->doxm->oxmLen,
1277 &selectedDevice->doxm->oxmSel);
1278 if(OC_STACK_OK != res)
1280 OIC_LOG(ERROR, TAG, "Failed to select the provisioning method");
1281 SetResult(otmCtx, res);
1284 OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
1286 //Send Req: PUT /oic/sec/doxm [{..."OxmSel" :g_OTMDatas[Index of Selected OxM].OXMString,...}]
1287 res = PutOwnerTransferModeToResource(otmCtx);
1288 if(OC_STACK_OK != res)
1290 OIC_LOG(WARNING, TAG, "Failed to select the provisioning method");
1291 SetResult(otmCtx, res);
1295 //Register DTLS event handler to catch the dtls event while handshake
1296 if(CA_STATUS_OK != CARegisterDTLSHandshakeCallback(DTLSHandshakeCB))
1298 OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register DTLS handshake callback.");
1301 OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
1307 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
1309 OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
1313 OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
1314 return OC_STACK_INVALID_PARAM;
1316 if(oxmType >= OIC_OXM_COUNT)
1318 OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
1319 return OC_STACK_INVALID_PARAM;
1322 g_OTMDatas[oxmType].loadSecretCB= data->loadSecretCB;
1323 g_OTMDatas[oxmType].createSecureSessionCB = data->createSecureSessionCB;
1324 g_OTMDatas[oxmType].createSelectOxmPayloadCB = data->createSelectOxmPayloadCB;
1325 g_OTMDatas[oxmType].createOwnerTransferPayloadCB = data->createOwnerTransferPayloadCB;
1327 OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
1333 * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
1335 OCStackResult OTMDoOwnershipTransfer(void* ctx,
1336 OCProvisionDev_t *selectedDevicelist,
1337 OCProvisionResultCB resultCallback)
1339 OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
1341 if (NULL == selectedDevicelist)
1343 return OC_STACK_INVALID_PARAM;
1345 if (NULL == resultCallback)
1347 return OC_STACK_INVALID_CALLBACK;
1350 OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
1353 OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
1354 return OC_STACK_NO_MEMORY;
1356 otmCtx->ctxResultCallback = resultCallback;
1357 otmCtx->ctxHasError = false;
1358 otmCtx->userCtx = ctx;
1359 OCProvisionDev_t* pCurDev = selectedDevicelist;
1361 //Counting number of selected devices.
1362 otmCtx->ctxResultArraySize = 0;
1363 while(NULL != pCurDev)
1365 otmCtx->ctxResultArraySize++;
1366 pCurDev = pCurDev->next;
1369 otmCtx->ctxResultArray =
1370 (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
1371 if(NULL == otmCtx->ctxResultArray)
1373 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
1375 return OC_STACK_NO_MEMORY;
1377 pCurDev = selectedDevicelist;
1379 OCStackResult res = OC_STACK_OK;
1380 //Fill the device UUID for result array.
1381 for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
1383 //Checking duplication of Device ID.
1384 bool isDuplicate = true;
1385 res = PDMIsDuplicateDevice(&pCurDev->doxm->deviceID, &isDuplicate);
1386 if (OC_STACK_OK != res)
1392 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Device ID is duplicated");
1393 res = OC_STACK_INVALID_PARAM;
1396 memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
1397 pCurDev->doxm->deviceID.id,
1399 otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
1400 pCurDev = pCurDev->next;
1403 StartOwnershipTransfer(otmCtx, selectedDevicelist);
1405 OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
1409 OICFree(otmCtx->ctxResultArray);
1415 OCStackResult PutProvisioningStatus(OTMContext_t* otmCtx)
1417 OIC_LOG(INFO, TAG, "IN PutProvisioningStatus");
1421 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1422 return OC_STACK_INVALID_PARAM;
1424 if(!otmCtx->selectedDeviceInfo)
1426 OIC_LOG(ERROR, TAG, "Can't find device information in OTMContext");
1428 return OC_STACK_INVALID_PARAM;
1431 otmCtx->selectedDeviceInfo->pstat->tm = NORMAL;
1432 otmCtx->selectedDeviceInfo->pstat->cm = PROVISION_ACLS | PROVISION_CREDENTIALS |
1433 SECURITY_MANAGEMENT_SERVICES | BOOTSTRAP_SERVICE;
1434 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1437 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1438 return OC_STACK_NO_MEMORY;
1440 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1441 secPayload->securityData = BinToPstatJSON(otmCtx->selectedDeviceInfo->pstat);
1442 if (NULL == secPayload->securityData)
1444 OICFree(secPayload);
1445 SetResult(otmCtx, OC_STACK_INVALID_JSON);
1446 return OC_STACK_INVALID_JSON;
1448 OIC_LOG_V(INFO, TAG, "Created payload for commit hash: %s",secPayload->securityData);
1450 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1451 if(!PMGenerateQuery(true,
1452 otmCtx->selectedDeviceInfo->endpoint.addr,
1453 otmCtx->selectedDeviceInfo->securePort,
1454 otmCtx->selectedDeviceInfo->connType,
1455 query, sizeof(query), OIC_RSRC_PSTAT_URI))
1457 OIC_LOG(ERROR, TAG, "PutProvisioningStatus : Failed to generate query");
1458 return OC_STACK_ERROR;
1460 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1462 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1463 cbData.cb = &ProvisioningStatusHandler;
1464 cbData.context = (void*)otmCtx;
1466 OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
1467 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1468 OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1469 if (ret != OC_STACK_OK)
1471 OIC_LOG(ERROR, TAG, "OCStack resource error");
1472 SetResult(otmCtx, ret);
1475 OIC_LOG(INFO, TAG, "OUT PutProvisioningStatus");
1480 OCStackResult FinalizeProvisioning(OTMContext_t* otmCtx)
1482 OIC_LOG(INFO, TAG, "IN FinalizeProvisioning");
1486 OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1487 return OC_STACK_INVALID_PARAM;
1489 if(!otmCtx->selectedDeviceInfo)
1491 OIC_LOG(ERROR, TAG, "Can't find device information in OTMContext");
1493 return OC_STACK_INVALID_PARAM;
1495 // Provision Default ACL to device
1496 OicSecAcl_t defaultAcl =
1509 OicUuid_t provTooldeviceID = {.id={0}};
1510 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1512 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1513 SetResult(otmCtx, OC_STACK_ERROR);
1514 return OC_STACK_ERROR;
1516 OIC_LOG(INFO, TAG, "Retieved deviceID");
1517 memcpy(defaultAcl.subject.id, provTooldeviceID.id, sizeof(defaultAcl.subject.id));
1518 char *wildCardResource = "*";
1519 defaultAcl.resources = &wildCardResource;
1521 defaultAcl.owners = (OicUuid_t *) OICCalloc(1, UUID_LENGTH);
1522 if(!defaultAcl.owners)
1524 OIC_LOG(ERROR, TAG, "Failed to memory allocation for default ACL");
1525 SetResult(otmCtx, OC_STACK_NO_MEMORY);
1526 return OC_STACK_NO_MEMORY;
1528 memcpy(defaultAcl.owners->id, provTooldeviceID.id, UUID_LENGTH);
1529 OIC_LOG(INFO, TAG, "Provisioning default ACL");
1531 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1534 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1535 return OC_STACK_NO_MEMORY;
1537 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1538 secPayload->securityData = BinToAclJSON(&defaultAcl);
1539 OICFree(defaultAcl.owners);
1540 if(!secPayload->securityData)
1542 OICFree(secPayload);
1543 OIC_LOG(INFO, TAG, "FinalizeProvisioning : Failed to BinToAclJSON");
1544 SetResult(otmCtx, OC_STACK_ERROR);
1545 return OC_STACK_ERROR;
1547 OIC_LOG_V(INFO, TAG, "Provisioning default ACL : %s",secPayload->securityData);
1549 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1550 if(!PMGenerateQuery(true,
1551 otmCtx->selectedDeviceInfo->endpoint.addr,
1552 otmCtx->selectedDeviceInfo->securePort,
1553 otmCtx->selectedDeviceInfo->connType,
1554 query, sizeof(query), OIC_RSRC_ACL_URI))
1556 OIC_LOG(ERROR, TAG, "FinalizeProvisioning : Failed to generate query");
1557 return OC_STACK_ERROR;
1559 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1561 OIC_LOG_V(INFO, TAG, "Request URI for Provisioning default ACL : %s", query);
1563 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1564 cbData.cb = &FinalizeProvisioningCB;
1565 cbData.context = (void *)otmCtx;
1567 OCStackResult ret = OCDoResource(NULL, OC_REST_POST, query,
1568 &otmCtx->selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1569 otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1570 if (OC_STACK_OK != ret)
1572 SetResult(otmCtx, ret);
1576 OIC_LOG(INFO, TAG, "OUT FinalizeProvisioning");