Multiple Ownership Transfer support.
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / ownershiptransfermanager.c
1 /* *****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * *****************************************************************/
20
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
25 //
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
31 #endif
32
33 #include "iotivity_config.h"
34 #ifdef HAVE_TIME_H
35 #include <time.h>
36 #endif
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #endif
40 #ifdef HAVE_SYS_TIME_H
41 #include <sys/time.h>
42 #endif
43 #include <stdbool.h>
44 #include <string.h>
45
46 #include "logger.h"
47 #include "oic_malloc.h"
48 #include "oic_string.h"
49 #include "cacommon.h"
50 #include "cainterface.h"
51 #include "base64.h"
52 #include "cJSON.h"
53 #include "global.h"
54 #include "utlist.h"
55
56 #include "srmresourcestrings.h"
57 #include "doxmresource.h"
58 #include "pstatresource.h"
59 #include "credresource.h"
60 #include "aclresource.h"
61 #include "ownershiptransfermanager.h"
62 #include "securevirtualresourcetypes.h"
63 #include "oxmjustworks.h"
64 #ifdef _ENABLE_MULTIPLE_OWNER_
65 #include "oxmrandompin.h"
66 #include "oxmpreconfpin.h"
67 #endif //_ENABLE_MULTIPLE_OWNER_
68 #include "pmtypes.h"
69 #include "pmutility.h"
70 #include "srmutility.h"
71 #include "provisioningdatabasemanager.h"
72 #include "oxmrandompin.h"
73 #include "ocpayload.h"
74 #include "payload_logging.h"
75
76 #define TAG "OTM"
77
78 /**
79  * Array to store the callbacks for each owner transfer method.
80  */
81 static OTMCallbackData_t g_OTMCbDatas[OIC_OXM_COUNT] = {
82         //Just works
83         {.loadSecretCB = LoadSecretJustWorksCallback,
84           .createSecureSessionCB = CreateSecureSessionJustWorksCallback,
85           .createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload,
86           .createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload},
87
88           //Random PIN
89         {.loadSecretCB = InputPinCodeCallback,
90           .createSecureSessionCB = CreateSecureSessionRandomPinCallback,
91           .createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload,
92           .createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload},
93
94         //Manufacturer Cert
95         {.loadSecretCB = NULL,
96           .createSecureSessionCB = NULL,
97           .createSelectOxmPayloadCB = NULL,
98           .createOwnerTransferPayloadCB = NULL},
99
100 #ifdef _ENABLE_MULTIPLE_OWNER_
101           //Preconfig PIN
102         {.loadSecretCB = LoadPreconfPinCodeCallback,
103           .createSecureSessionCB = CreateSecureSessionPreconfPinCallback,
104           .createSelectOxmPayloadCB = CreatePreconfPinBasedSelectOxmPayload,
105           .createOwnerTransferPayloadCB = CreatePreconfPinBasedOwnerTransferPayload},
106 #endif //_ENABLE_MULTIPLE_OWNER_
107 };
108
109 /**
110  * Variables for pointing the OTMContext to be used in the DTLS handshake result callback.
111  */
112 static OTMContext_t* g_otmCtx = NULL;
113
114 /**
115  * Function to select appropriate  provisioning method.
116  *
117  * @param[in] supportedMethods   Array of supported methods
118  * @param[in] numberOfMethods   number of supported methods
119  * @param[out]  selectedMethod         Selected methods
120  * @return  OC_STACK_OK on success
121  */
122 static OCStackResult SelectProvisioningMethod(const OicSecOxm_t *supportedMethods,
123         size_t numberOfMethods, OicSecOxm_t *selectedMethod)
124 {
125     OIC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
126
127     if(numberOfMethods == 0 || !supportedMethods)
128     {
129         OIC_LOG(WARNING, TAG, "Could not find a supported OxM.");
130         return OC_STACK_ERROR;
131     }
132
133     *selectedMethod  = supportedMethods[0];
134     for(size_t i = 0; i < numberOfMethods; i++)
135     {
136         if(*selectedMethod < supportedMethods[i])
137         {
138             *selectedMethod =  supportedMethods[i];
139         }
140     }
141
142     if(NULL == g_OTMCbDatas[(*selectedMethod)].loadSecretCB ||
143        NULL == g_OTMCbDatas[(*selectedMethod)].createSecureSessionCB ||
144        NULL == g_OTMCbDatas[(*selectedMethod)].createSelectOxmPayloadCB ||
145        NULL == g_OTMCbDatas[(*selectedMethod)].createOwnerTransferPayloadCB)
146     {
147         OIC_LOG_V(ERROR, TAG, "Please make sure the OxM(%d)'s callback registration", (int)(*selectedMethod));
148         return OC_STACK_INVALID_CALLBACK;
149     }
150
151     OIC_LOG(DEBUG, TAG, "OUT SelectProvisioningMethod");
152
153     return OC_STACK_OK;
154 }
155
156 /**
157  * Function to select operation mode.This function will return most secure common operation mode.
158  *
159  * @param[in] selectedDeviceInfo   selected device information to performing provisioning.
160  * @param[out]   selectedMode   selected operation mode
161  * @return  OC_STACK_OK on success
162  */
163 static void SelectOperationMode(const OCProvisionDev_t *selectedDeviceInfo,
164                                 OicSecDpom_t *selectedMode)
165 {
166     OIC_LOG(DEBUG, TAG, "IN SelectOperationMode");
167     *selectedMode = selectedDeviceInfo->pstat->sm[0];
168     OIC_LOG_V(DEBUG, TAG, "Selected Operation Mode = %d", *selectedMode);
169 }
170
171 /**
172  * Function to start ownership transfer.
173  * This function will send the first request for provisioning,
174  * The next request message is sent from the response handler for this request.
175  *
176  * @param[in] ctx   context value passed to callback from calling function.
177  * @param[in] selectedDevice   selected device information to performing provisioning.
178  * @return  OC_STACK_OK on success
179  */
180 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
181
182 /**
183  * Function to update owner transfer mode
184  *
185  * @param[in]  otmCtx  Context value of ownership transfer.
186  * @return  OC_STACK_OK on success
187  */
188 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx);
189
190 /**
191  * Function to send request to resource to get its pstat resource information.
192  *
193  * @param[in]  otmCtx  Context value of ownership transfer.
194  * @return  OC_STACK_OK on success
195  */
196 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx);
197
198
199 /**
200  * Function to send  uuid of owner device to new device.
201  * This function would update 'owner of doxm' as UUID for provisioning tool.
202  *
203  * @param[in]  otmCtx  Context value of ownership transfer.
204  * @return  OC_STACK_OK on success
205  */
206 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx);
207
208 /**
209  * Function to update the operation mode. As per the spec. Operation mode in client driven
210  * single service provisioning it will be updated to 0x3
211  *
212  * @param[in]  otmCtx  Context value of ownership transfer.
213  * @return  OC_STACK_OK on success
214  */
215 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx);
216
217 /**
218  * Function to update the owner credential to new device
219  *
220  * @param[in]  otmCtx  Context value of ownership transfer.
221  * @param[in] selectedOperationMode selected operation mode
222  * @return  OC_STACK_OK on success
223  */
224 static OCStackResult PostOwnerCredential(OTMContext_t* otmCtx);
225
226 /**
227  * Function to update the owner ACL to new device.
228  *
229  * @param[in]  otmCtx  Context value of ownership transfer.
230  * @return  OC_STACK_OK on success
231  */
232 static OCStackResult PostOwnerAcl(OTMContext_t* otmCtx);
233
234 /**
235  * Function to send ownerShip info.
236  * This function would update 'owned of doxm' as true.
237  *
238  * @param[in]  otmCtx  Context value of ownership transfer.
239  * @return  OC_STACK_OK on success
240  */
241 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx);
242
243 /**
244  * Function to update pstat as Ready for provisioning.
245  * This function would update 'cm' from bx0000,0010 to bx0000,0000.
246  *
247  * @param[in] ctx   context value passed to callback from calling function.
248  * @param[in] selectedDevice   selected device information to performing provisioning.
249  * @return  OC_STACK_OK on success
250  */
251 static OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx);
252
253 /**
254  * Function to update pstat as Ready for Normal Operation.
255  * This function would update 'isop' from false to true.
256  *
257  * @param[in] ctx   context value passed to callback from calling function.
258  * @param[in] selectedDevice   selected device information to performing provisioning.
259  * @return  OC_STACK_OK on success
260  */
261 static OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx);
262
263 static bool IsComplete(OTMContext_t* otmCtx)
264 {
265     for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
266     {
267         if(OC_STACK_CONTINUE == otmCtx->ctxResultArray[i].res)
268         {
269             return false;
270         }
271     }
272
273     return true;
274 }
275
276 /**
277  * Function to save the result of provisioning.
278  *
279  * @param[in,out] otmCtx   Context value of ownership transfer.
280  * @param[in] res   result of provisioning
281  */
282 static void SetResult(OTMContext_t* otmCtx, const OCStackResult res)
283 {
284     OIC_LOG_V(DEBUG, TAG, "IN SetResult : %d ", res);
285
286     if(!otmCtx)
287     {
288         OIC_LOG(WARNING, TAG, "OTMContext is NULL");
289         return;
290     }
291
292     if(otmCtx->selectedDeviceInfo)
293     {
294         //Revert psk_info callback and new deivce uuid in case of random PIN OxM
295         if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
296         {
297             if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskCredentials))
298             {
299                 OIC_LOG(WARNING, TAG, "Failed to revert  is DTLS credential handler.");
300             }
301             OicUuid_t emptyUuid = { .id={0}};
302             SetUuidForPinBasedOxm(&emptyUuid);
303         }
304
305         for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
306         {
307             if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
308                       otmCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
309             {
310                 otmCtx->ctxResultArray[i].res = res;
311                 if(OC_STACK_OK != res)
312                 {
313                     otmCtx->ctxHasError = true;
314                 }
315             }
316         }
317
318         g_otmCtx = NULL;
319
320         //If all request is completed, invoke the user callback.
321         if(IsComplete(otmCtx))
322         {
323             otmCtx->ctxResultCallback(otmCtx->userCtx, otmCtx->ctxResultArraySize,
324                                        otmCtx->ctxResultArray, otmCtx->ctxHasError);
325             OICFree(otmCtx->ctxResultArray);
326             OICFree(otmCtx);
327         }
328         else
329         {
330             if(OC_STACK_OK != StartOwnershipTransfer(otmCtx,
331                                                      otmCtx->selectedDeviceInfo->next))
332             {
333                 OIC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
334             }
335         }
336     }
337
338     OIC_LOG(DEBUG, TAG, "OUT SetResult");
339 }
340
341 /**
342  * Function to handle the handshake result in OTM.
343  * This function will be invoked after DTLS handshake
344  * @param   endPoint  [IN] The remote endpoint.
345  * @param   errorInfo [IN] Error information from the endpoint.
346  * @return  NONE
347  */
348 void DTLSHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
349 {
350     if(NULL != g_otmCtx && NULL != g_otmCtx->selectedDeviceInfo &&
351        NULL != endpoint && NULL != info)
352     {
353         OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
354                  endpoint->addr, endpoint->port, info->result);
355
356         OicSecDoxm_t* newDevDoxm = g_otmCtx->selectedDeviceInfo->doxm;
357
358         if(NULL != newDevDoxm)
359         {
360             OicUuid_t emptyUuid = {.id={0}};
361
362             //Make sure the address matches.
363             if(strncmp(g_otmCtx->selectedDeviceInfo->endpoint.addr,
364                endpoint->addr,
365                sizeof(endpoint->addr)) == 0 &&
366                g_otmCtx->selectedDeviceInfo->securePort == endpoint->port)
367             {
368                 OCStackResult res = OC_STACK_ERROR;
369
370                 //If temporal secure sesstion established successfully
371                 if(CA_STATUS_OK == info->result &&
372                    false == newDevDoxm->owned &&
373                    memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) == 0)
374                 {
375                     //Send request : POST /oic/sec/doxm [{... , "devowner":"PT's UUID"}]
376                     res = PostOwnerUuid(g_otmCtx);
377                     if(OC_STACK_OK != res)
378                     {
379                         OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
380                         SetResult(g_otmCtx, res);
381                     }
382                 }
383                 //In case of authentication failure
384                 else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
385                 {
386                     //in case of error from owner credential
387                     if(memcmp(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t)) != 0 &&
388                         true == newDevDoxm->owned)
389                     {
390                         OIC_LOG(ERROR, TAG, "The owner credential may incorrect.");
391
392                         if(OC_STACK_OK != RemoveCredential(&(newDevDoxm->deviceID)))
393                         {
394                             OIC_LOG(WARNING, TAG, "Failed to remove the invaild owner credential");
395                         }
396                         SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
397                     }
398                     //in case of error from wrong PIN, re-start the ownership transfer
399                     else if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
400                     {
401                         OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
402
403                         memcpy(&(newDevDoxm->owner), &emptyUuid, sizeof(OicUuid_t));
404                         newDevDoxm->owned = false;
405                         g_otmCtx->attemptCnt++;
406
407                         if(WRONG_PIN_MAX_ATTEMP > g_otmCtx->attemptCnt)
408                         {
409                             res = StartOwnershipTransfer(g_otmCtx, g_otmCtx->selectedDeviceInfo);
410                             if(OC_STACK_OK != res)
411                             {
412                                 SetResult(g_otmCtx, res);
413                                 OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
414                             }
415                         }
416                         else
417                         {
418                             OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
419                             SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
420                         }
421                     }
422                     else
423                     {
424                         OIC_LOG(ERROR, TAG, "Failed to establish secure session.");
425                         SetResult(g_otmCtx, OC_STACK_AUTHENTICATION_FAILURE);
426                     }
427                 }
428             }
429         }
430     }
431 }
432
433 /**
434  * Function to save the Owner/SubOwner PSK.
435  *
436  * @param[in] selectedDeviceInfo   selected device information to performing provisioning.
437  * @return  OC_STACK_OK on success
438  */
439 static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
440 {
441     OIC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
442
443     OCStackResult res = OC_STACK_ERROR;
444
445     CAEndpoint_t endpoint;
446     memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
447     OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
448     endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
449     endpoint.port = selectedDeviceInfo->securePort;
450     endpoint.adapter = selectedDeviceInfo->endpoint.adapter;
451
452     OicUuid_t ownerDeviceID = {.id={0}};
453     if (OC_STACK_OK != GetDoxmDeviceID(&ownerDeviceID))
454     {
455         OIC_LOG(ERROR, TAG, "Error while retrieving Owner's device ID");
456         return res;
457     }
458
459     uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
460     OicSecKey_t ownerKey = {ownerPSK, OWNER_PSK_LENGTH_128};
461
462     //Generating OwnerPSK
463     CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
464             (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
465             strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
466             ownerDeviceID.id, sizeof(ownerDeviceID.id),
467             selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
468             ownerPSK, OWNER_PSK_LENGTH_128);
469
470     if (CA_STATUS_OK == pskRet)
471     {
472         OIC_LOG(INFO, TAG,"Owner PSK dump:\n");
473         OIC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
474         //Generating new credential for provisioning tool
475         OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
476                                   SYMMETRIC_PAIR_WISE_KEY, NULL,
477                                   &ownerKey, &ownerDeviceID, NULL);
478         VERIFY_NON_NULL(TAG, cred, ERROR);
479
480         // TODO: Added as workaround. Will be replaced soon.
481         cred->privateData.encoding = OIC_ENCODING_RAW;
482
483 #if 1
484         // NOTE: Test codes to use BASE64 encoded owner PSK.
485         uint32_t outSize = 0;
486         size_t b64BufSize = B64ENCODE_OUT_SAFESIZE((OWNER_PSK_LENGTH_128 + 1));
487         char* b64Buf = (uint8_t *)OICCalloc(1, b64BufSize);
488         VERIFY_NON_NULL(TAG, b64Buf, ERROR);
489         b64Encode(cred->privateData.data, cred->privateData.len, b64Buf, b64BufSize, &outSize);
490
491         OICFree( cred->privateData.data );
492         cred->privateData.data = (uint8_t *)OICCalloc(1, outSize + 1);
493         VERIFY_NON_NULL(TAG, cred->privateData.data, ERROR);
494
495         strncpy(cred->privateData.data, b64Buf, outSize);
496         cred->privateData.data[outSize] = '\0';
497         cred->privateData.encoding = OIC_ENCODING_BASE64;
498         cred->privateData.len = outSize;
499         OICFree(b64Buf);
500 #endif //End of Test codes
501
502         //Finding previous ownerPSK.
503         const OicSecCred_t* credList = GetCredList();
504         OicSecCred_t* prevCred = NULL;
505         uint16_t credId = 0;
506         LL_FOREACH(credList, prevCred)
507         {
508             //OwnerPSK's type is SYMMETRIC_PAIR_WISE_KEY
509             if (SYMMETRIC_PAIR_WISE_KEY == prevCred->credType &&
510                 0 == memcmp(prevCred->subject.id, cred->subject.id, sizeof(cred->subject.id)))
511             {
512                 credId = prevCred->credId;
513                 break;
514             }
515         }
516
517         //If duplicate owner PSK is exists, remove it.
518         if(0 < credId)
519         {
520             OIC_LOG(WARNING, TAG, "Duplicate OwnerPSK was detected.");
521             OIC_LOG(WARNING, TAG, "[Subject] : ");
522             OIC_LOG_BUFFER(WARNING, TAG, prevCred->subject.id, sizeof(prevCred->subject.id));
523             OIC_LOG_V(WARNING, TAG, "[Encoding Type] : %d", prevCred->privateData.encoding);
524             OIC_LOG(WARNING, TAG, "[Private Data] : ");
525             OIC_LOG_BUFFER(WARNING, TAG, prevCred->privateData.data, prevCred->privateData.len);
526             OIC_LOG(WARNING, TAG, "Previous OwnerPSK will be removed.");
527
528             res = RemoveCredentialByCredId(credId);
529             if(OC_STACK_RESOURCE_DELETED != res)
530             {
531                 OIC_LOG(ERROR, TAG, "Failed to remove the previous OwnerPSK");
532                 DeleteCredList(cred);
533                 goto exit;
534             }
535         }
536
537         res = AddCredential(cred);
538         if(res != OC_STACK_OK)
539         {
540             DeleteCredList(cred);
541             return res;
542         }
543     }
544     else
545     {
546         OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
547     }
548
549     OIC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
550 exit:
551     return res;
552 }
553
554 /**
555  * Callback handler for OwnerShipTransferModeHandler API.
556  *
557  * @param[in] ctx             ctx value passed to callback from calling function.
558  * @param[in] UNUSED          handle to an invocation
559  * @param[in] clientResponse  Response from queries to remote servers.
560  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
561  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
562  */
563 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
564                                                          OCClientResponse *clientResponse)
565 {
566     OIC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
567
568     VERIFY_NON_NULL(TAG, clientResponse, WARNING);
569     VERIFY_NON_NULL(TAG, ctx, WARNING);
570
571     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
572     (void)UNUSED;
573     if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
574     {
575         OIC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
576         //Send request : GET /oic/sec/pstat
577         OCStackResult res = GetProvisioningStatusResource(otmCtx);
578         if(OC_STACK_OK != res)
579         {
580             OIC_LOG(WARNING, TAG, "Failed to get pstat information");
581             SetResult(otmCtx, res);
582         }
583     }
584     else
585     {
586         OIC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
587         clientResponse->result);
588         SetResult(otmCtx, clientResponse->result);
589     }
590
591     OIC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
592
593 exit:
594     return  OC_STACK_DELETE_TRANSACTION;
595 }
596
597 /**
598  * Callback handler for ProvisioningStatusResouceHandler API.
599  *
600  * @param[in] ctx             ctx value passed to callback from calling function.
601  * @param[in] UNUSED          handle to an invocation
602  * @param[in] clientResponse  Response from queries to remote servers.
603  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
604  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
605  */
606 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
607                                                     OCClientResponse *clientResponse)
608 {
609     OIC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
610
611     VERIFY_NON_NULL(TAG, clientResponse, WARNING);
612     VERIFY_NON_NULL(TAG, ctx, WARNING);
613
614     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
615     (void)UNUSED;
616     if  (OC_STACK_OK == clientResponse->result)
617     {
618         if  (NULL == clientResponse->payload)
619         {
620             OIC_LOG(INFO, TAG, "Skiping Null payload");
621             SetResult(otmCtx, OC_STACK_ERROR);
622             return OC_STACK_DELETE_TRANSACTION;
623         }
624
625         if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
626         {
627             OIC_LOG(INFO, TAG, "Unknown payload type");
628             SetResult(otmCtx, OC_STACK_ERROR);
629             return OC_STACK_DELETE_TRANSACTION;
630         }
631         OicSecPstat_t* pstat = NULL;
632         OCStackResult result = CBORPayloadToPstat(
633                 ((OCSecurityPayload*)clientResponse->payload)->securityData,
634                 ((OCSecurityPayload*)clientResponse->payload)->payloadSize,
635                 &pstat);
636         if(NULL == pstat || result != OC_STACK_OK)
637         {
638             OIC_LOG(ERROR, TAG, "Error while converting cbor to pstat.");
639             SetResult(otmCtx, OC_STACK_ERROR);
640             return OC_STACK_DELETE_TRANSACTION;
641         }
642         if(false == (TAKE_OWNER & pstat->cm))
643         {
644             OIC_LOG(ERROR, TAG, "Device pairing mode enabling owner transfer operations is disabled");
645             SetResult(otmCtx, OC_STACK_ERROR);
646             return OC_STACK_DELETE_TRANSACTION;
647         }
648         otmCtx->selectedDeviceInfo->pstat = pstat;
649
650         //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
651         SelectOperationMode(otmCtx->selectedDeviceInfo, &(otmCtx->selectedDeviceInfo->pstat->om));
652
653         //Send request : POST /oic/sec/pstat [{"om":"bx11", .. }]
654         OCStackResult res = PostUpdateOperationMode(otmCtx);
655         if (OC_STACK_OK != res)
656         {
657             OIC_LOG(ERROR, TAG, "Error while updating operation mode.");
658             SetResult(otmCtx, res);
659         }
660     }
661     else
662     {
663         OIC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
664             clientResponse->result);
665         SetResult(otmCtx, clientResponse->result);
666     }
667
668     OIC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
669 exit:
670     return  OC_STACK_DELETE_TRANSACTION;
671 }
672
673 /**
674  * Response handler for update owner uuid request.
675  *
676  * @param[in] ctx             ctx value passed to callback from calling function.
677  * @param[in] UNUSED          handle to an invocation
678  * @param[in] clientResponse  Response from queries to remote servers.
679  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
680  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
681  */
682 static OCStackApplicationResult OwnerUuidUpdateHandler(void *ctx, OCDoHandle UNUSED,
683                                 OCClientResponse *clientResponse)
684 {
685     VERIFY_NON_NULL(TAG, clientResponse, WARNING);
686     VERIFY_NON_NULL(TAG, ctx, WARNING);
687
688     OIC_LOG(DEBUG, TAG, "IN OwnerUuidUpdateHandler");
689     (void)UNUSED;
690     OCStackResult res = OC_STACK_OK;
691     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
692
693     if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
694     {
695         if(otmCtx && otmCtx->selectedDeviceInfo)
696         {
697             res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
698             if(OC_STACK_OK != res)
699             {
700                 OIC_LOG(ERROR, TAG, "OwnerUuidUpdateHandler:Failed to owner PSK generation");
701                 SetResult(otmCtx, res);
702                 return OC_STACK_DELETE_TRANSACTION;
703             }
704
705             //POST owner credential to new device according to security spec B.
706             res = PostOwnerCredential(otmCtx);
707             if(OC_STACK_OK != res)
708             {
709                 OIC_LOG(ERROR, TAG,
710                         "OwnerUuidUpdateHandler:Failed to send PosT request for onwer credential");
711                 SetResult(otmCtx, res);
712                 return OC_STACK_DELETE_TRANSACTION;
713             }
714         }
715     }
716     else
717     {
718         res = clientResponse->result;
719         OIC_LOG_V(ERROR, TAG, "OwnerUuidHandler : Unexpected result %d", res);
720         SetResult(otmCtx, res);
721     }
722
723     OIC_LOG(DEBUG, TAG, "OUT OwnerUuidUpdateHandler");
724
725 exit:
726     return  OC_STACK_DELETE_TRANSACTION;
727 }
728
729 /**
730  * Response handler for update operation mode.
731  *
732  * @param[in] ctx             ctx value passed to callback from calling function.
733  * @param[in] UNUSED          handle to an invocation
734  * @param[in] clientResponse  Response from queries to remote servers.
735  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
736  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
737  */
738 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
739                                 OCClientResponse *clientResponse)
740 {
741     OIC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
742
743     VERIFY_NON_NULL(TAG, clientResponse, WARNING);
744     VERIFY_NON_NULL(TAG, ctx, WARNING);
745
746     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
747     (void) UNUSED;
748     if  (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
749     {
750         OCStackResult res = OC_STACK_ERROR;
751         OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
752
753         //DTLS Handshake
754         //Load secret for temporal secure session.
755         if(g_OTMCbDatas[selOxm].loadSecretCB)
756         {
757             res = g_OTMCbDatas[selOxm].loadSecretCB(otmCtx);
758             if(OC_STACK_OK != res)
759             {
760                 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
761                 SetResult(otmCtx, res);
762                 return  OC_STACK_DELETE_TRANSACTION;
763             }
764         }
765
766         //It will be used in handshake event handler
767         g_otmCtx = otmCtx;
768
769         //Try DTLS handshake to generate secure session
770         if(g_OTMCbDatas[selOxm].createSecureSessionCB)
771         {
772             res = g_OTMCbDatas[selOxm].createSecureSessionCB(otmCtx);
773             if(OC_STACK_OK != res)
774             {
775                 OIC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
776                 SetResult(otmCtx, res);
777                 return OC_STACK_DELETE_TRANSACTION;
778             }
779         }
780     }
781     else
782     {
783         OIC_LOG(ERROR, TAG, "Error while update operation mode");
784         SetResult(otmCtx, clientResponse->result);
785     }
786
787     OIC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
788
789 exit:
790     return  OC_STACK_DELETE_TRANSACTION;
791 }
792
793 /**
794  * Response handler for update owner crendetial request.
795  *
796  * @param[in] ctx             ctx value passed to callback from calling function.
797  * @param[in] UNUSED          handle to an invocation
798  * @param[in] clientResponse  Response from queries to remote servers.
799  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
800  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
801  */
802 static OCStackApplicationResult OwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
803                                 OCClientResponse *clientResponse)
804 {
805     VERIFY_NON_NULL(TAG, clientResponse, WARNING);
806     VERIFY_NON_NULL(TAG, ctx, WARNING);
807
808     OIC_LOG(DEBUG, TAG, "IN OwnerCredentialHandler");
809     (void)UNUSED;
810     OCStackResult res = OC_STACK_OK;
811     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
812
813     if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
814     {
815         if(otmCtx && otmCtx->selectedDeviceInfo)
816         {
817             //Close the temporal secure session to verify the owner credential
818             CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
819             endpoint->port = otmCtx->selectedDeviceInfo->securePort;
820             CAResult_t caResult = CA_STATUS_OK;
821             caResult = CAcloseSslConnection(endpoint);
822
823             if(CA_STATUS_OK != caResult)
824             {
825                 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
826                 SetResult(otmCtx, caResult);
827                 return OC_STACK_DELETE_TRANSACTION;
828             }
829
830             /**
831              * If we select NULL cipher,
832              * client will select appropriate cipher suite according to server's cipher-suite list.
833              */
834             // TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 = 0xC037, /**< see RFC 5489 */
835             caResult = CASelectCipherSuite(0xC037, endpoint->adapter);
836
837             if(CA_STATUS_OK != caResult)
838             {
839                 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
840                 SetResult(otmCtx, caResult);
841                 return OC_STACK_DELETE_TRANSACTION;
842             }
843
844             /**
845              * in case of random PIN based OxM,
846              * revert get_psk_info callback of tinyDTLS to use owner credential.
847              */
848             if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
849             {
850                 OicUuid_t emptyUuid = { .id={0}};
851                 SetUuidForPinBasedOxm(&emptyUuid);
852
853                 caResult = CAregisterPskCredentialsHandler(GetDtlsPskCredentials);
854
855                 if(CA_STATUS_OK != caResult)
856                 {
857                     OIC_LOG(ERROR, TAG, "Failed to revert DTLS credential handler.");
858                     SetResult(otmCtx, OC_STACK_INVALID_CALLBACK);
859                     return OC_STACK_DELETE_TRANSACTION;
860                 }
861             }
862 #ifdef __WITH_TLS__
863            otmCtx->selectedDeviceInfo->connType |= CT_FLAG_SECURE;
864 #endif
865             res = PostOwnerAcl(otmCtx);
866             if(OC_STACK_OK != res)
867             {
868                 OIC_LOG(ERROR, TAG, "Failed to update owner ACL to new device");
869                 SetResult(otmCtx, res);
870                 return OC_STACK_DELETE_TRANSACTION;
871             }
872         }
873     }
874     else
875     {
876         res = clientResponse->result;
877         OIC_LOG_V(ERROR, TAG, "OwnerCredentialHandler : Unexpected result %d", res);
878         SetResult(otmCtx, res);
879     }
880
881     OIC_LOG(DEBUG, TAG, "OUT OwnerCredentialHandler");
882
883 exit:
884     return  OC_STACK_DELETE_TRANSACTION;
885 }
886
887 /**
888  * Response handler for update owner ACL request.
889  *
890  * @param[in] ctx             ctx value passed to callback from calling function.
891  * @param[in] UNUSED          handle to an invocation
892  * @param[in] clientResponse  Response from queries to remote servers.
893  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
894  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
895  */
896 static OCStackApplicationResult OwnerAclHandler(void *ctx, OCDoHandle UNUSED,
897                                 OCClientResponse *clientResponse)
898 {
899     VERIFY_NON_NULL(TAG, clientResponse, WARNING);
900     VERIFY_NON_NULL(TAG, ctx, WARNING);
901
902     OIC_LOG(DEBUG, TAG, "IN OwnerAclHandler");
903     (void)UNUSED;
904     OCStackResult res = OC_STACK_OK;
905     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
906
907     if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
908     {
909         if(otmCtx && otmCtx->selectedDeviceInfo)
910         {
911             //POST /oic/sec/doxm [{ ..., "owned":"TRUE" }]
912             res = PostOwnershipInformation(otmCtx);
913             if(OC_STACK_OK != res)
914             {
915                 OIC_LOG(ERROR, TAG, "Failed to update ownership information to new device");
916                 SetResult(otmCtx, res);
917             }
918         }
919     }
920     else
921     {
922         res = clientResponse->result;
923         OIC_LOG_V(ERROR, TAG, "OwnerAclHandler : Unexpected result %d", res);
924         SetResult(otmCtx, res);
925     }
926
927     OIC_LOG(DEBUG, TAG, "OUT OwnerAclHandler");
928
929 exit:
930     return  OC_STACK_DELETE_TRANSACTION;
931 }
932
933
934 /**
935  * Response handler for update owner information request.
936  *
937  * @param[in] ctx             ctx value passed to callback from calling function.
938  * @param[in] UNUSED          handle to an invocation
939  * @param[in] clientResponse  Response from queries to remote servers.
940  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
941  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
942  */
943 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
944                                 OCClientResponse *clientResponse)
945 {
946     VERIFY_NON_NULL(TAG, clientResponse, WARNING);
947     VERIFY_NON_NULL(TAG, ctx, WARNING);
948
949     OIC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
950     (void)UNUSED;
951     OCStackResult res = OC_STACK_OK;
952     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
953
954     if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
955     {
956         if(otmCtx && otmCtx->selectedDeviceInfo)
957         {
958             OIC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
959             OIC_LOG(INFO, TAG, "Set Ready for provisioning state .");
960
961             res = PostProvisioningStatus(otmCtx);
962             if(OC_STACK_OK != res)
963             {
964                 OIC_LOG(ERROR, TAG, "Failed to update pstat");
965                 SetResult(otmCtx, res);
966             }
967         }
968     }
969     else
970     {
971         res = clientResponse->result;
972         OIC_LOG_V(ERROR, TAG, "OwnershipInformationHandler : Unexpected result %d", res);
973         SetResult(otmCtx, res);
974     }
975
976     OIC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
977
978 exit:
979     return  OC_STACK_DELETE_TRANSACTION;
980 }
981
982 /**
983  * Response handler of update provisioning status.
984  *
985  * @param[in] ctx             ctx value passed to callback from calling function.
986  * @param[in] UNUSED          handle to an invocation
987  * @param[in] clientResponse  Response from queries to remote servers.
988  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
989  *          and OC_STACK_KEEP_TRANSACTION to keep it.
990  */
991 static OCStackApplicationResult ProvisioningStatusHandler(void *ctx, OCDoHandle UNUSED,
992                                                        OCClientResponse *clientResponse)
993 {
994     OIC_LOG_V(INFO, TAG, "IN ProvisioningStatusHandler.");
995
996     VERIFY_NON_NULL(TAG, clientResponse, ERROR);
997     VERIFY_NON_NULL(TAG, ctx, ERROR);
998
999     OTMContext_t* otmCtx = (OTMContext_t*) ctx;
1000     (void)UNUSED;
1001     OCStackResult res = OC_STACK_OK;
1002
1003     if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1004     {
1005         if(otmCtx && otmCtx->selectedDeviceInfo)
1006         {
1007             OIC_LOG(INFO, TAG, "Device state is in Ready for Provisionig.");
1008
1009             res = PostNormalOperationStatus(otmCtx);
1010             if(OC_STACK_OK != res)
1011             {
1012                 OIC_LOG(ERROR, TAG, "Failed to update pstat");
1013                 SetResult(otmCtx, res);
1014             }
1015         }
1016     }
1017     else
1018     {
1019         OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1020                             clientResponse->result);
1021         SetResult(otmCtx, clientResponse->result);
1022     }
1023
1024 exit:
1025     OIC_LOG_V(INFO, TAG, "OUT ProvisioningStatusHandler.");
1026     return OC_STACK_DELETE_TRANSACTION;
1027 }
1028
1029 /**
1030  * Response handler of update provisioning status to Ready for Normal..
1031  *
1032  * @param[in] ctx             ctx value passed to callback from calling function.
1033  * @param[in] UNUSED          handle to an invocation
1034  * @param[in] clientResponse  Response from queries to remote servers.
1035  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
1036  *          and OC_STACK_KEEP_TRANSACTION to keep it.
1037  */
1038 static OCStackApplicationResult ReadyForNomalStatusHandler(void *ctx, OCDoHandle UNUSED,
1039                                                        OCClientResponse *clientResponse)
1040 {
1041     OIC_LOG_V(INFO, TAG, "IN ReadyForNomalStatusHandler.");
1042
1043     VERIFY_NON_NULL(TAG, clientResponse, ERROR);
1044     VERIFY_NON_NULL(TAG, ctx, ERROR);
1045
1046     OTMContext_t* otmCtx = (OTMContext_t*) ctx;
1047     (void)UNUSED;
1048
1049     if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1050     {
1051         OIC_LOG(INFO, TAG, "Device state is in Ready for Normal Operation.");
1052         OCStackResult res = PDMAddDevice(&otmCtx->selectedDeviceInfo->doxm->deviceID);
1053          if (OC_STACK_OK == res)
1054          {
1055                 OIC_LOG_V(INFO, TAG, "Add device's UUID in PDM_DB");
1056                 SetResult(otmCtx, OC_STACK_OK);
1057                 return OC_STACK_DELETE_TRANSACTION;
1058          }
1059           else
1060          {
1061               OIC_LOG(ERROR, TAG, "Ownership transfer is complete but adding information to DB is failed.");
1062          }
1063     }
1064     else
1065     {
1066         OIC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1067                             clientResponse->result);
1068         SetResult(otmCtx, clientResponse->result);
1069     }
1070
1071 exit:
1072     OIC_LOG_V(INFO, TAG, "OUT ReadyForNomalStatusHandler.");
1073     return OC_STACK_DELETE_TRANSACTION;
1074 }
1075
1076 static OCStackResult PostOwnerCredential(OTMContext_t* otmCtx)
1077 {
1078     OIC_LOG(DEBUG, TAG, "IN PostOwnerCredential");
1079
1080     if(!otmCtx || !otmCtx->selectedDeviceInfo)
1081     {
1082         OIC_LOG(ERROR, TAG, "Invalid parameters");
1083         return OC_STACK_INVALID_PARAM;
1084     }
1085
1086     OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1087     char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1088
1089     if(!PMGenerateQuery(true,
1090                         deviceInfo->endpoint.addr, deviceInfo->securePort,
1091                         deviceInfo->connType,
1092                         query, sizeof(query), OIC_RSRC_CRED_URI))
1093     {
1094         OIC_LOG(ERROR, TAG, "PostOwnerCredential : Failed to generate query");
1095         return OC_STACK_ERROR;
1096     }
1097     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1098     OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1099     if(!secPayload)
1100     {
1101         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1102         return OC_STACK_NO_MEMORY;
1103     }
1104
1105     //Generate owner credential for new device
1106     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1107     const OicSecCred_t* ownerCredential = GetCredResourceData(&(deviceInfo->doxm->deviceID));
1108     if(!ownerCredential)
1109     {
1110         OIC_LOG(ERROR, TAG, "Can not find OwnerPSK.");
1111         return OC_STACK_NO_RESOURCE;
1112     }
1113
1114     OicUuid_t credSubjectId = {.id={0}};
1115     if(OC_STACK_OK == GetDoxmDeviceID(&credSubjectId))
1116     {
1117         OicSecCred_t newCredential;
1118         memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
1119         newCredential.next = NULL;
1120
1121         //Set subject ID as PT's ID
1122         memcpy(&(newCredential.subject), &credSubjectId, sizeof(OicUuid_t));
1123
1124         //Fill private data as empty string
1125         newCredential.privateData.data = "";
1126         newCredential.privateData.len = 0;
1127         newCredential.privateData.encoding = ownerCredential->privateData.encoding;
1128
1129         newCredential.publicData.data = NULL;
1130         newCredential.publicData.len = 0;
1131
1132         int secureFlag = 0;
1133         //Send owner credential to new device : POST /oic/sec/cred [ owner credential ]
1134         if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData,
1135                                         &secPayload->payloadSize, secureFlag))
1136         {
1137             OICFree(secPayload);
1138             OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
1139             return OC_STACK_ERROR;
1140         }
1141         OIC_LOG(DEBUG, TAG, "Cred Payload:");
1142         OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1143
1144         OCCallbackData cbData;
1145         cbData.cb = &OwnerCredentialHandler;
1146         cbData.context = (void *)otmCtx;
1147         cbData.cd = NULL;
1148         OCStackResult res = OCDoResource(NULL, OC_REST_POST, query,
1149                                          &deviceInfo->endpoint, (OCPayload*)secPayload,
1150                                          deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1151         if (res != OC_STACK_OK)
1152         {
1153             OIC_LOG(ERROR, TAG, "OCStack resource error");
1154         }
1155     }
1156     else
1157     {
1158         OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
1159         return OC_STACK_NO_RESOURCE;
1160     }
1161
1162     OIC_LOG(DEBUG, TAG, "OUT PostOwnerCredential");
1163
1164     return OC_STACK_OK;
1165 }
1166
1167 static OicSecAcl_t* GenerateOwnerAcl(const OicUuid_t* owner)
1168 {
1169     OicSecAcl_t* ownerAcl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
1170     OicSecAce_t* ownerAce = (OicSecAce_t*)OICCalloc(1, sizeof(OicSecAce_t));
1171     OicSecRsrc_t* wildcardRsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
1172     if(NULL == ownerAcl || NULL == ownerAce || NULL == wildcardRsrc)
1173     {
1174         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1175         goto error;
1176     }
1177     LL_APPEND(ownerAcl->aces, ownerAce);
1178     LL_APPEND(ownerAce->resources, wildcardRsrc);
1179
1180     //Set resource owner as PT
1181     memcpy(ownerAcl->rownerID.id, owner->id, sizeof(owner->id));
1182
1183     //PT has full permission.
1184     ownerAce->permission = PERMISSION_FULL_CONTROL;
1185
1186     //Set subject as PT's UUID
1187     memcpy(ownerAce->subjectuuid.id, owner->id, sizeof(owner->id));
1188
1189     wildcardRsrc->href = OICStrdup(WILDCARD_RESOURCE_URI);
1190     if(NULL == wildcardRsrc->href)
1191     {
1192         goto error;
1193     }
1194
1195     wildcardRsrc->interfaceLen = 1;
1196     wildcardRsrc->interfaces = (char**)OICMalloc(wildcardRsrc->interfaceLen * sizeof(char*));
1197     if(NULL == wildcardRsrc->interfaces)
1198     {
1199         goto error;
1200     }
1201     wildcardRsrc->interfaces[0] = OICStrdup(WILDCARD_RESOURCE_URI);
1202     if(NULL == wildcardRsrc->interfaces[0])
1203     {
1204         goto error;
1205     }
1206
1207     wildcardRsrc->typeLen = 1;
1208     wildcardRsrc->types = (char**)OICMalloc(wildcardRsrc->typeLen * sizeof(char*));
1209     if(NULL == wildcardRsrc->types)
1210     {
1211         goto error;
1212     }
1213     wildcardRsrc->types[0] = OICStrdup(WILDCARD_RESOURCE_URI);
1214     if(NULL == wildcardRsrc->types[0])
1215     {
1216         goto error;
1217     }
1218
1219     return ownerAcl;
1220
1221 error:
1222     //in case of memory allocation failed, each resource should be removed individually.
1223     if(NULL == ownerAcl || NULL == ownerAce || NULL == wildcardRsrc)
1224     {
1225         OICFree(ownerAcl);
1226         OICFree(ownerAce);
1227         OICFree(wildcardRsrc);
1228     }
1229     else
1230     {
1231         DeleteACLList(ownerAcl);
1232     }
1233     return NULL;
1234 }
1235
1236 /**
1237  * Function to update the owner ACL to new device.
1238  *
1239  * @param[in]  otmCtx  Context value of ownership transfer.
1240  * @return  OC_STACK_OK on success
1241  */
1242 static OCStackResult PostOwnerAcl(OTMContext_t* otmCtx)
1243 {
1244     OCStackResult res = OC_STACK_ERROR;
1245
1246     OIC_LOG(DEBUG, TAG, "IN PostOwnerAcl");
1247
1248     if(!otmCtx || !otmCtx->selectedDeviceInfo)
1249     {
1250         OIC_LOG(ERROR, TAG, "Invalid parameters");
1251         return OC_STACK_INVALID_PARAM;
1252     }
1253
1254     OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1255     char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1256     OicSecAcl_t* ownerAcl = NULL;
1257
1258     if(!PMGenerateQuery(true,
1259                         deviceInfo->endpoint.addr, deviceInfo->securePort,
1260                         deviceInfo->connType,
1261                         query, sizeof(query), OIC_RSRC_ACL_URI))
1262     {
1263         OIC_LOG(ERROR, TAG, "Failed to generate query");
1264         return OC_STACK_ERROR;
1265     }
1266     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1267
1268     OicUuid_t ownerID;
1269     res = GetDoxmDeviceID(&ownerID);
1270     if(OC_STACK_OK != res)
1271     {
1272         OIC_LOG(ERROR, TAG, "Failed to generate owner ACL");
1273         return res;
1274     }
1275
1276     //Generate owner ACL for new device
1277     ownerAcl = GenerateOwnerAcl(&ownerID);
1278     if(NULL == ownerAcl)
1279     {
1280         OIC_LOG(ERROR, TAG, "Failed to generate owner ACL");
1281         return OC_STACK_NO_MEMORY;
1282     }
1283
1284     //Generate ACL payload
1285     OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1286     if(!secPayload)
1287     {
1288         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1289         res = OC_STACK_NO_MEMORY;
1290         goto error;
1291     }
1292
1293     res = AclToCBORPayload(ownerAcl, &secPayload->securityData, &secPayload->payloadSize);
1294     if (OC_STACK_OK != res)
1295     {
1296         OICFree(secPayload);
1297         OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
1298         goto error;
1299     }
1300     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1301
1302     OIC_LOG(DEBUG, TAG, "Owner ACL Payload:");
1303     OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1304
1305     //Send owner ACL to new device : POST /oic/sec/cred [ owner credential ]
1306     OCCallbackData cbData;
1307     cbData.cb = &OwnerAclHandler;
1308     cbData.context = (void *)otmCtx;
1309     cbData.cd = NULL;
1310     res = OCDoResource(NULL, OC_REST_POST, query,
1311                                      &deviceInfo->endpoint, (OCPayload*)secPayload,
1312                                      deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1313     if (res != OC_STACK_OK)
1314     {
1315         OIC_LOG(ERROR, TAG, "OCStack resource error");
1316         goto error;
1317     }
1318
1319     OIC_LOG(DEBUG, TAG, "OUT PostOwnerAcl");
1320
1321 error:
1322     DeleteACLList(ownerAcl);
1323
1324     return OC_STACK_OK;
1325 }
1326
1327 static OCStackResult PostOwnerTransferModeToResource(OTMContext_t* otmCtx)
1328 {
1329     OIC_LOG(DEBUG, TAG, "IN PostOwnerTransferModeToResource");
1330
1331     if(!otmCtx || !otmCtx->selectedDeviceInfo)
1332     {
1333         OIC_LOG(ERROR, TAG, "Invalid parameters");
1334         return OC_STACK_INVALID_PARAM;
1335     }
1336
1337     OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1338     OicSecOxm_t selectedOxm = deviceInfo->doxm->oxmSel;
1339     char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1340
1341     if(!PMGenerateQuery(false,
1342                         deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1343                         deviceInfo->connType,
1344                         query, sizeof(query), OIC_RSRC_DOXM_URI))
1345     {
1346         OIC_LOG(ERROR, TAG, "PostOwnerTransferModeToResource : Failed to generate query");
1347         return OC_STACK_ERROR;
1348     }
1349     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1350     OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1351     if(!secPayload)
1352     {
1353         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1354         return OC_STACK_NO_MEMORY;
1355     }
1356     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1357     OCStackResult res = g_OTMCbDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx,
1358             &secPayload->securityData, &secPayload->payloadSize);
1359     if (OC_STACK_OK != res && NULL == secPayload->securityData)
1360     {
1361         OCPayloadDestroy((OCPayload *)secPayload);
1362         OIC_LOG(ERROR, TAG, "Error while converting bin to cbor");
1363         return OC_STACK_ERROR;
1364     }
1365
1366     OCCallbackData cbData;
1367     cbData.cb = &OwnerTransferModeHandler;
1368     cbData.context = (void *)otmCtx;
1369     cbData.cd = NULL;
1370     res = OCDoResource(NULL, OC_REST_POST, query,
1371                        &deviceInfo->endpoint, (OCPayload *)secPayload,
1372                        deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1373     if (res != OC_STACK_OK)
1374     {
1375         OIC_LOG(ERROR, TAG, "OCStack resource error");
1376     }
1377
1378     OIC_LOG(DEBUG, TAG, "OUT PostOwnerTransferModeToResource");
1379
1380     return res;
1381 }
1382
1383 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
1384 {
1385     OIC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
1386
1387     if(!otmCtx || !otmCtx->selectedDeviceInfo)
1388     {
1389         OIC_LOG(ERROR, TAG, "Invailed parameters");
1390         return OC_STACK_INVALID_PARAM;
1391     }
1392
1393     OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1394     char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1395     if(!PMGenerateQuery(false,
1396                         deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1397                         deviceInfo->connType,
1398                         query, sizeof(query), OIC_RSRC_PSTAT_URI))
1399     {
1400         OIC_LOG(ERROR, TAG, "GetProvisioningStatusResource : Failed to generate query");
1401         return OC_STACK_ERROR;
1402     }
1403     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1404
1405     OCCallbackData cbData;
1406     cbData.cb = &ListMethodsHandler;
1407     cbData.context = (void *)otmCtx;
1408     cbData.cd = NULL;
1409     OCStackResult res = OCDoResource(NULL, OC_REST_GET, query, NULL, NULL,
1410                                      deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1411     if (res != OC_STACK_OK)
1412     {
1413         OIC_LOG(ERROR, TAG, "OCStack resource error");
1414     }
1415
1416     OIC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
1417
1418     return res;
1419 }
1420
1421 static OCStackResult PostOwnerUuid(OTMContext_t* otmCtx)
1422 {
1423     OIC_LOG(DEBUG, TAG, "IN PostOwnerUuid");
1424
1425     if(!otmCtx || !otmCtx->selectedDeviceInfo)
1426     {
1427         OIC_LOG(ERROR, TAG, "Invailed parameters");
1428         return OC_STACK_INVALID_PARAM;
1429     }
1430
1431     OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1432     char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1433     if(!PMGenerateQuery(true,
1434                         deviceInfo->endpoint.addr, deviceInfo->securePort,
1435                         deviceInfo->connType,
1436                         query, sizeof(query), OIC_RSRC_DOXM_URI))
1437     {
1438         OIC_LOG(ERROR, TAG, "PostOwnerUuid : Failed to generate query");
1439         return OC_STACK_ERROR;
1440     }
1441     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1442
1443     //Post PT's uuid to new device
1444     OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1445     if(!secPayload)
1446     {
1447         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1448         return OC_STACK_NO_MEMORY;
1449     }
1450     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1451     OCStackResult res =  g_OTMCbDatas[deviceInfo->doxm->oxmSel].createOwnerTransferPayloadCB(
1452             otmCtx, &secPayload->securityData, &secPayload->payloadSize);
1453     if (OC_STACK_OK != res && NULL == secPayload->securityData)
1454     {
1455         OCPayloadDestroy((OCPayload *)secPayload);
1456         OIC_LOG(ERROR, TAG, "Error while converting doxm bin to cbor.");
1457         return OC_STACK_INVALID_PARAM;
1458     }
1459     OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1460
1461     OCCallbackData cbData;
1462     cbData.cb = &OwnerUuidUpdateHandler;
1463     cbData.context = (void *)otmCtx;
1464     cbData.cd = NULL;
1465
1466     res = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1467             deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1468     if (res != OC_STACK_OK)
1469     {
1470         OIC_LOG(ERROR, TAG, "OCStack resource error");
1471     }
1472
1473     OIC_LOG(DEBUG, TAG, "OUT PostOwnerUuid");
1474
1475     return res;
1476 }
1477
1478 static OCStackResult PostOwnershipInformation(OTMContext_t* otmCtx)
1479 {
1480     OIC_LOG(DEBUG, TAG, "IN PostOwnershipInformation");
1481
1482     if(!otmCtx || !otmCtx->selectedDeviceInfo)
1483     {
1484         OIC_LOG(ERROR, TAG, "Invailed parameters");
1485         return OC_STACK_INVALID_PARAM;
1486     }
1487
1488     OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1489     char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1490     if(!PMGenerateQuery(true,
1491                         deviceInfo->endpoint.addr, deviceInfo->securePort,
1492                         deviceInfo->connType,
1493                         query, sizeof(query), OIC_RSRC_DOXM_URI))
1494     {
1495         OIC_LOG(ERROR, TAG, "PostOwnershipInformation : Failed to generate query");
1496         return OC_STACK_ERROR;
1497     }
1498     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1499
1500     //OwnershipInformationHandler
1501     OCSecurityPayload *secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1502     if (!secPayload)
1503     {
1504         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1505         return OC_STACK_NO_MEMORY;
1506     }
1507
1508     otmCtx->selectedDeviceInfo->doxm->owned = true;
1509
1510     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1511     OCStackResult res = DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm,
1512             &secPayload->securityData, &secPayload->payloadSize, true);
1513     if (OC_STACK_OK != res && NULL == secPayload->securityData)
1514     {
1515         OCPayloadDestroy((OCPayload *)secPayload);
1516         OIC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1517         return OC_STACK_INVALID_PARAM;
1518     }
1519
1520     OCCallbackData cbData;
1521     cbData.cb = &OwnershipInformationHandler;
1522     cbData.context = (void *)otmCtx;
1523     cbData.cd = NULL;
1524
1525     res = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1526                        deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1527     if (res != OC_STACK_OK)
1528     {
1529         OIC_LOG(ERROR, TAG, "OCStack resource error");
1530     }
1531
1532     OIC_LOG(DEBUG, TAG, "OUT PostOwnershipInformation");
1533
1534     return res;
1535 }
1536
1537 static OCStackResult PostUpdateOperationMode(OTMContext_t* otmCtx)
1538 {
1539     OIC_LOG(DEBUG, TAG, "IN PostUpdateOperationMode");
1540
1541     if(!otmCtx || !otmCtx->selectedDeviceInfo)
1542     {
1543         return OC_STACK_INVALID_PARAM;
1544     }
1545
1546     OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
1547     char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1548     if(!PMGenerateQuery(false,
1549                         deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
1550                         deviceInfo->connType,
1551                         query, sizeof(query), OIC_RSRC_PSTAT_URI))
1552     {
1553         OIC_LOG(ERROR, TAG, "PostUpdateOperationMode : Failed to generate query");
1554         return OC_STACK_ERROR;
1555     }
1556     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1557
1558     OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1559     if(!secPayload)
1560     {
1561         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1562         return OC_STACK_NO_MEMORY;
1563     }
1564     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1565     OCStackResult res = PstatToCBORPayload(deviceInfo->pstat, &secPayload->securityData,
1566                                            &secPayload->payloadSize, true);
1567    if (OC_STACK_OK != res)
1568     {
1569         OCPayloadDestroy((OCPayload *)secPayload);
1570         OIC_LOG(ERROR, TAG, "Error while converting pstat to cbor.");
1571         return OC_STACK_INVALID_PARAM;
1572     }
1573
1574     OCCallbackData cbData;
1575     cbData.cb = &OperationModeUpdateHandler;
1576     cbData.context = (void *)otmCtx;
1577     cbData.cd = NULL;
1578     res = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload *)secPayload,
1579                        deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1580     if (res != OC_STACK_OK)
1581     {
1582         OIC_LOG(ERROR, TAG, "OCStack resource error");
1583     }
1584
1585     OIC_LOG(DEBUG, TAG, "OUT PostUpdateOperationMode");
1586
1587     return res;
1588 }
1589
1590 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
1591 {
1592     OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
1593     OCStackResult res = OC_STACK_INVALID_PARAM;
1594
1595     VERIFY_NON_NULL(TAG, selectedDevice, ERROR);
1596     VERIFY_NON_NULL(TAG, selectedDevice->doxm, ERROR);
1597
1598     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
1599     otmCtx->selectedDeviceInfo = selectedDevice;
1600
1601     //Set to the lowest level OxM, and then find more higher level OxM.
1602     res = SelectProvisioningMethod(selectedDevice->doxm->oxm,
1603                                    selectedDevice->doxm->oxmLen,
1604                                    &selectedDevice->doxm->oxmSel);
1605     if(OC_STACK_OK != res)
1606     {
1607         OIC_LOG(ERROR, TAG, "Failed to select the provisioning method");
1608         SetResult(otmCtx, res);
1609         return res;
1610     }
1611     OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
1612
1613     //Send Req: POST /oic/sec/doxm [{..."OxmSel" :g_OTMCbDatas[Index of Selected OxM].OXMString,...}]
1614     res = PostOwnerTransferModeToResource(otmCtx);
1615     if(OC_STACK_OK != res)
1616     {
1617         OIC_LOG(WARNING, TAG, "Failed to select the provisioning method");
1618         SetResult(otmCtx, res);
1619         return res;
1620     }
1621
1622 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
1623     //Register TLS event handler to catch the tls event while handshake
1624     if(CA_STATUS_OK != CAregisterSslHandshakeCallback(DTLSHandshakeCB))
1625     {
1626         OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register TLS handshake callback.");
1627     }
1628 #endif // __WITH_DTLS__ or __WITH_TLS__
1629     OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
1630
1631 exit:
1632     return res;
1633 }
1634
1635 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
1636 {
1637     OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
1638
1639     if(!data)
1640     {
1641         OIC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
1642         return OC_STACK_INVALID_PARAM;
1643     }
1644     if(oxmType >= OIC_OXM_COUNT)
1645     {
1646         OIC_LOG(INFO, TAG, "Unknow ownership transfer method");
1647         return OC_STACK_INVALID_PARAM;
1648     }
1649
1650     g_OTMCbDatas[oxmType].loadSecretCB= data->loadSecretCB;
1651     g_OTMCbDatas[oxmType].createSecureSessionCB = data->createSecureSessionCB;
1652     g_OTMCbDatas[oxmType].createSelectOxmPayloadCB = data->createSelectOxmPayloadCB;
1653     g_OTMCbDatas[oxmType].createOwnerTransferPayloadCB = data->createOwnerTransferPayloadCB;
1654
1655     OIC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
1656
1657     return OC_STACK_OK;
1658 }
1659
1660 /**
1661  * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
1662  */
1663 OCStackResult OTMDoOwnershipTransfer(void* ctx,
1664                                      OCProvisionDev_t *selectedDevicelist,
1665                                      OCProvisionResultCB resultCallback)
1666 {
1667     OIC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
1668
1669     if (NULL == selectedDevicelist)
1670     {
1671         return OC_STACK_INVALID_PARAM;
1672     }
1673     if (NULL == resultCallback)
1674     {
1675         return OC_STACK_INVALID_CALLBACK;
1676     }
1677
1678     OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
1679     if(!otmCtx)
1680     {
1681         OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
1682         return OC_STACK_NO_MEMORY;
1683     }
1684     otmCtx->ctxResultCallback = resultCallback;
1685     otmCtx->ctxHasError = false;
1686     otmCtx->userCtx = ctx;
1687     OCProvisionDev_t* pCurDev = selectedDevicelist;
1688
1689     //Counting number of selected devices.
1690     otmCtx->ctxResultArraySize = 0;
1691     while(NULL != pCurDev)
1692     {
1693         otmCtx->ctxResultArraySize++;
1694         pCurDev = pCurDev->next;
1695     }
1696
1697     otmCtx->ctxResultArray =
1698         (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
1699     if(NULL == otmCtx->ctxResultArray)
1700     {
1701         OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
1702         OICFree(otmCtx);
1703         return OC_STACK_NO_MEMORY;
1704     }
1705     pCurDev = selectedDevicelist;
1706
1707     OCStackResult res = OC_STACK_OK;
1708     //Fill the device UUID for result array.
1709     for(size_t devIdx = 0; devIdx < otmCtx->ctxResultArraySize; devIdx++)
1710     {
1711         //Checking duplication of Device ID.
1712         bool isDuplicate = true;
1713         res = PDMIsDuplicateDevice(&pCurDev->doxm->deviceID, &isDuplicate);
1714         if (OC_STACK_OK != res)
1715         {
1716             goto error;
1717         }
1718         if (isDuplicate)
1719         {
1720             bool isStale = false;
1721             res = PDMIsDeviceStale(&pCurDev->doxm->deviceID, &isStale);
1722             if(OC_STACK_OK != res)
1723             {
1724                 OIC_LOG(ERROR, TAG, "Internal error in PDMIsDeviceStale");
1725                 goto error;
1726             }
1727             if(isStale)
1728             {
1729                 OIC_LOG(INFO, TAG, "Detected duplicated UUID in stale status, "\
1730                                    "this UUID will be removed from PDM");
1731
1732                 res = PDMDeleteDevice(&pCurDev->doxm->deviceID);
1733                 if(OC_STACK_OK != res)
1734                 {
1735                     OIC_LOG(ERROR, TAG, "Internal error in PDMDeleteDevice");
1736                     goto error;
1737                 }
1738             }
1739             else
1740             {
1741                 OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Device UUID is duplicated");
1742                 res = OC_STACK_INVALID_PARAM;
1743                 goto error;
1744             }
1745         }
1746         memcpy(otmCtx->ctxResultArray[devIdx].deviceId.id,
1747                pCurDev->doxm->deviceID.id,
1748                UUID_LENGTH);
1749         otmCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
1750         pCurDev = pCurDev->next;
1751     }
1752
1753     StartOwnershipTransfer(otmCtx, selectedDevicelist);
1754
1755     OIC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
1756     return OC_STACK_OK;
1757
1758 error:
1759     OICFree(otmCtx->ctxResultArray);
1760     OICFree(otmCtx);
1761     return res;
1762 }
1763
1764 OCStackResult PostProvisioningStatus(OTMContext_t* otmCtx)
1765 {
1766     OIC_LOG(INFO, TAG, "IN PostProvisioningStatus");
1767
1768     if(!otmCtx || !otmCtx->selectedDeviceInfo)
1769     {
1770         OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1771         return OC_STACK_INVALID_PARAM;
1772     }
1773
1774     //Change the TAKE_OWNER bit of CM to 0.
1775     otmCtx->selectedDeviceInfo->pstat->cm &= (~TAKE_OWNER);
1776
1777     OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1778     if (!secPayload)
1779     {
1780         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1781         return OC_STACK_NO_MEMORY;
1782     }
1783     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1784     if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1785             &secPayload->securityData, &secPayload->payloadSize, true))
1786     {
1787         OCPayloadDestroy((OCPayload *)secPayload);
1788         return OC_STACK_INVALID_JSON;
1789     }
1790     OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
1791     OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1792
1793     char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1794     if(!PMGenerateQuery(true,
1795                         otmCtx->selectedDeviceInfo->endpoint.addr,
1796                         otmCtx->selectedDeviceInfo->securePort,
1797                         otmCtx->selectedDeviceInfo->connType,
1798                         query, sizeof(query), OIC_RSRC_PSTAT_URI))
1799     {
1800         OIC_LOG(ERROR, TAG, "PostProvisioningStatus : Failed to generate query");
1801         return OC_STACK_ERROR;
1802     }
1803     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1804
1805     OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1806     cbData.cb = &ProvisioningStatusHandler;
1807     cbData.context = (void*)otmCtx;
1808     cbData.cd = NULL;
1809     OCStackResult ret = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1810             otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1811     OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1812     if (ret != OC_STACK_OK)
1813     {
1814         OIC_LOG(ERROR, TAG, "OCStack resource error");
1815     }
1816
1817     OIC_LOG(INFO, TAG, "OUT PostProvisioningStatus");
1818
1819     return ret;
1820 }
1821
1822 OCStackResult PostNormalOperationStatus(OTMContext_t* otmCtx)
1823 {
1824     OIC_LOG(INFO, TAG, "IN PostNormalOperationStatus");
1825
1826     if(!otmCtx || !otmCtx->selectedDeviceInfo)
1827     {
1828         OIC_LOG(ERROR, TAG, "OTMContext is NULL");
1829         return OC_STACK_INVALID_PARAM;
1830     }
1831
1832     //Set isop to true.
1833     otmCtx->selectedDeviceInfo->pstat->isOp = true;
1834
1835     OCSecurityPayload *secPayload = (OCSecurityPayload *)OICCalloc(1, sizeof(OCSecurityPayload));
1836     if (!secPayload)
1837     {
1838         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1839         return OC_STACK_NO_MEMORY;
1840     }
1841     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1842     if (OC_STACK_OK != PstatToCBORPayload(otmCtx->selectedDeviceInfo->pstat,
1843             &secPayload->securityData, &secPayload->payloadSize, true))
1844     {
1845         OCPayloadDestroy((OCPayload *)secPayload);
1846         return OC_STACK_INVALID_JSON;
1847     }
1848     OIC_LOG(DEBUG, TAG, "Created payload for chage to Provisiong state");
1849     OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1850
1851     char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1852     if(!PMGenerateQuery(true,
1853                         otmCtx->selectedDeviceInfo->endpoint.addr,
1854                         otmCtx->selectedDeviceInfo->securePort,
1855                         otmCtx->selectedDeviceInfo->connType,
1856                         query, sizeof(query), OIC_RSRC_PSTAT_URI))
1857     {
1858         OIC_LOG(ERROR, TAG, "PostNormalOperationStatus : Failed to generate query");
1859         return OC_STACK_ERROR;
1860     }
1861     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1862
1863     OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1864     cbData.cb = &ReadyForNomalStatusHandler;
1865     cbData.context = (void*)otmCtx;
1866     cbData.cd = NULL;
1867     OCStackResult ret = OCDoResource(NULL, OC_REST_POST, query, 0, (OCPayload*)secPayload,
1868             otmCtx->selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1869     OIC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1870     if (ret != OC_STACK_OK)
1871     {
1872         OIC_LOG(ERROR, TAG, "OCStack resource error");
1873     }
1874
1875     OIC_LOG(INFO, TAG, "OUT PostNormalOperationStatus");
1876
1877     return ret;
1878 }