6a84d2772b108a0a82a311f152303362a0e59b39
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / multipleownershiptransfermanager.c
1 /* *****************************************************************
2  *
3  * Copyright 2016 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 #ifdef HAVE_TIME_H
22 #include <time.h>
23 #endif
24 #ifdef HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #ifdef HAVE_SYS_TIME_H
28 #include <sys/time.h>
29 #endif
30 #include <stdbool.h>
31 #include <string.h>
32
33 #include "utlist.h"
34 #include "logger.h"
35 #include "oic_malloc.h"
36 #include "oic_string.h"
37 #include "cacommon.h"
38 #include "cainterface.h"
39 #include "base64.h"
40 #include "cJSON.h"
41 #include "global.h"
42
43 #include "srmresourcestrings.h"
44 #include "doxmresource.h"
45 #include "pstatresource.h"
46 #include "credresource.h"
47 #include "aclresource.h"
48 #include "ownershiptransfermanager.h"
49 #include "securevirtualresourcetypes.h"
50 #include "oxmjustworks.h"
51 #include "pmtypes.h"
52 #include "pmutility.h"
53 #include "srmutility.h"
54 #include "provisioningdatabasemanager.h"
55 #include "oxmrandompin.h"
56 #include "ocpayload.h"
57 #include "payload_logging.h"
58 #include "oxmjustworks.h"
59 #include "oxmpreconfpin.h"
60 #include "oxmrandompin.h"
61 #include "otmcontextlist.h"
62
63 #define TAG "OIC_MULTIPLE_OTM"
64
65 /**********************************************************************
66  * API for Super Owner
67  **********************************************************************/
68
69 /**
70  * Callback handler of security resource's POST request.
71  *
72  * @param[in] ctx             ctx value passed to callback from calling function.
73  * @param[in] UNUSED          handle to an invocation
74  * @param[in] clientResponse  Response from queries to remote servers.
75  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
76  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
77  */
78 static OCStackApplicationResult MOTUpdateSecurityResourceCB(void *ctx, OCDoHandle UNUSED,
79                                                 OCClientResponse *clientResponse)
80 {
81     OIC_LOG_V(INFO, TAG, "Inside MOTUpdateMomCB.");
82     (void)UNUSED;
83     OTMContext_t *motCtx = (OTMContext_t*)ctx;
84     VERIFY_NON_NULL(TAG, motCtx, ERROR);
85     VERIFY_NON_NULL(TAG, motCtx->ctxResultCallback, ERROR);
86     VERIFY_NON_NULL(TAG, motCtx->ctxResultArray, ERROR);
87
88     if(clientResponse)
89     {
90         memcpy(motCtx->ctxResultArray[0].deviceId.id,
91                motCtx->selectedDeviceInfo->doxm->deviceID.id,
92                sizeof(OicUuid_t));
93         motCtx->ctxResultArray[0].res = clientResponse->result;
94
95         if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
96         {
97             motCtx->ctxHasError = false;
98         }
99         else
100         {
101             motCtx->ctxHasError = true;
102         }
103     }
104     else
105     {
106         OIC_LOG_V(ERROR, TAG, "SRPGetACLResourceCB received Null clientResponse");
107         motCtx->ctxResultArray[0].res = OC_STACK_ERROR;
108         motCtx->ctxHasError = true;
109     }
110
111     motCtx->ctxResultCallback(motCtx->userCtx, motCtx->ctxResultArraySize,
112                               motCtx->ctxResultArray, motCtx->ctxHasError);
113
114 exit:
115     if(motCtx)
116     {
117         OICFree(motCtx->ctxResultArray);
118         OICFree(motCtx);
119     }
120     return OC_STACK_DELETE_TRANSACTION;
121 }
122
123 /**
124  * Internal API to send POST doxm request
125  */
126 static OCStackResult MOTSendPostDoxm(void *ctx,
127                                      const OCProvisionDev_t *targetDeviceInfo,
128                                      OCProvisionResultCB resultCallback,
129                                      const OicSecDoxm_t* doxm)
130 {
131     OCStackResult postMomRes = OC_STACK_ERROR;
132     OCSecurityPayload* secPayload = NULL;
133     OTMContext_t *motCtx = NULL;
134     bool freeFlag = true;
135
136     OIC_LOG(DEBUG, TAG, "IN MOTSendPostDoxm");
137
138     //Generate the security payload using updated doxm
139     secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
140     VERIFY_NON_NULL(TAG, secPayload, ERROR);
141     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
142
143     postMomRes = DoxmToCBORPayload(doxm, &secPayload->securityData, &secPayload->payloadSize, true);
144     VERIFY_SUCCESS(TAG, (OC_STACK_OK == postMomRes), ERROR);
145
146     OIC_LOG(DEBUG, TAG, "Created doxm payload to update doxm:");
147     OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
148
149     char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
150     bool queryGenRes = PMGenerateQuery(true,
151                                        targetDeviceInfo->endpoint.addr,
152                                        targetDeviceInfo->securePort,
153                                        targetDeviceInfo->connType,
154                                        query, sizeof(query), OIC_RSRC_DOXM_URI);
155     VERIFY_SUCCESS(TAG, (true == queryGenRes), ERROR);
156     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
157
158     //Create the MOT Context to handle the response message
159     motCtx = (OTMContext_t*)OICCalloc(1, sizeof(OTMContext_t));
160     VERIFY_NON_NULL(TAG, motCtx, ERROR);
161     motCtx->selectedDeviceInfo = targetDeviceInfo;
162     motCtx->ctxResultCallback = resultCallback;
163     motCtx->ctxResultArraySize = 1;
164     motCtx->ctxHasError = false;
165     motCtx->userCtx = ctx;
166     motCtx->ctxResultArray= (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
167     VERIFY_NON_NULL(TAG, motCtx->ctxResultArray, ERROR);
168
169     //Send POST request
170     OCCallbackData cbData =  {.context=NULL, .cb=NULL, .cd=NULL};
171     cbData.cb = &MOTUpdateSecurityResourceCB;
172     cbData.context = (void *)motCtx;
173     OIC_LOG(DEBUG, TAG, "Sending POST 'doxm' request to resource server");
174     postMomRes = OCDoResource(NULL, OC_REST_POST, query,
175                               &targetDeviceInfo->endpoint, (OCPayload*)secPayload,
176                               targetDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
177     VERIFY_SUCCESS(TAG, (OC_STACK_OK == postMomRes), ERROR);
178
179     freeFlag = false;
180
181     OIC_LOG(DEBUG, TAG, "OUT MOTSendPostDoxm");
182
183 exit:
184     //If POST request successfully sent, motCtx will be cleaned from response handler.
185     if(freeFlag && motCtx)
186     {
187         OICFree(motCtx->ctxResultArray);
188         OICFree(motCtx);
189     }
190
191     return postMomRes;
192 }
193
194 /**
195  * API to update 'doxm.mom' to resource server.
196  *
197  * @param[in] targetDeviceInfo Selected target device.
198  * @param[in] momType Mode of multiple ownership transfer (ref. oic.sec.mom)
199  * @param[in] resultCallback callback provided by API user, callback will be called when
200  *            POST 'mom' request recieves a response from resource server.
201  * @return OC_STACK_OK in case of success and other value otherwise.
202  */
203 OCStackResult MOTChangeMode(void *ctx, const OCProvisionDev_t *targetDeviceInfo,
204                             const OicSecMomType_t momType, OCProvisionResultCB resultCallback)
205 {
206     OCStackResult postMomRes = OC_STACK_INVALID_PARAM;
207     OicSecDoxm_t* doxm = NULL;
208     uint8_t* doxmPayload = NULL;
209     size_t doxmPayloadLen = 0;
210
211     OIC_LOG(DEBUG, TAG, "IN MOTChangeMode");
212
213     VERIFY_SUCCESS(TAG, (OIC_NUMBER_OF_MOM_TYPE > momType), ERROR);
214     VERIFY_NON_NULL(TAG, targetDeviceInfo, ERROR);
215     postMomRes = OC_STACK_INVALID_CALLBACK;
216     VERIFY_NON_NULL(TAG, resultCallback, ERROR);
217
218     //Dulpicate doxm resource to update the 'mom' property
219     postMomRes = DoxmToCBORPayload(targetDeviceInfo->doxm, &doxmPayload, &doxmPayloadLen, false);
220     VERIFY_SUCCESS(TAG, (OC_STACK_OK == postMomRes), ERROR);
221
222     postMomRes = CBORPayloadToDoxm(doxmPayload, doxmPayloadLen, &doxm);
223     VERIFY_SUCCESS(TAG, (OC_STACK_OK == postMomRes), ERROR);
224     VERIFY_NON_NULL(TAG, doxm, ERROR);
225
226     if(NULL == doxm->mom)
227     {
228         postMomRes = OC_STACK_NO_MEMORY;
229         doxm->mom = (OicSecMom_t*)OICCalloc(1, sizeof(OicSecMom_t));
230         VERIFY_NON_NULL(TAG, (doxm->mom), ERROR);
231     }
232     doxm->mom->mode = momType;
233
234     //Send POST reuqest for update doxm
235     postMomRes = MOTSendPostDoxm(ctx, targetDeviceInfo, resultCallback, doxm);
236     VERIFY_SUCCESS(TAG, (OC_STACK_OK == postMomRes), ERROR);
237
238     OIC_LOG(DEBUG, TAG, "OUT MOTChangeMode");
239
240 exit:
241     OICFree(doxmPayload);
242     DeleteDoxmBinData(doxm);
243     return postMomRes;
244 }
245
246 /**
247  * API to add 'doxm.oxms' to resource server.
248  *
249  * @param[in] targetDeviceInfo Selected target device.
250  * @param[in] newOxm  OxMs to be added (ref. oic.sec.oxm)
251  * @param[in] resultCallback callback provided by API user, callback will be called when
252  *            POST 'oxms' request recieves a response from resource server.
253  * @return OC_STACK_OK in case of success and other value otherwise.
254  */
255 OCStackResult MOTAddMOTMethod(void *ctx, OCProvisionDev_t *targetDeviceInfo,
256                                  const OicSecOxm_t newOxm, OCProvisionResultCB resultCallback)
257 {
258     OCStackResult postOxmRes = OC_STACK_INVALID_PARAM;
259     OicSecOxm_t* newOxms = NULL;
260     uint8_t* doxmPayload = NULL;
261     size_t doxmPayloadLen = 0;
262
263     OIC_LOG(DEBUG, TAG, "IN MOTAddMOTMethod");
264
265     VERIFY_SUCCESS(TAG, (OIC_OXM_COUNT > newOxm || OIC_PRECONFIG_PIN == newOxm), ERROR);
266     VERIFY_NON_NULL(TAG, targetDeviceInfo, ERROR);
267     postOxmRes = OC_STACK_INVALID_CALLBACK;
268     VERIFY_NON_NULL(TAG, resultCallback, ERROR);
269     postOxmRes = OC_STACK_NO_MEMORY;
270
271     for(size_t i = 0; i < targetDeviceInfo->doxm->oxmLen; i++)
272     {
273         if(targetDeviceInfo->doxm->oxm[i] == newOxm)
274         {
275             OIC_LOG_V(INFO, TAG, "[%d] OxM already supported", (int)newOxm);
276             OCProvisionResult_t* resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
277             VERIFY_NON_NULL(TAG, resArr, ERROR);
278             resArr->res = OC_STACK_OK;
279             memcpy(resArr->deviceId.id, targetDeviceInfo->doxm->deviceID.id, sizeof(resArr->deviceId.id));
280             resultCallback(ctx, 1, resArr, false);
281             return OC_STACK_OK;
282         }
283     }
284
285     newOxms = (OicSecOxm_t*)OICMalloc(sizeof(OicSecOxm_t) * (targetDeviceInfo->doxm->oxmLen + 1));
286     VERIFY_NON_NULL(TAG, newOxms , ERROR);
287
288     for(size_t i = 0; i < targetDeviceInfo->doxm->oxmLen; i++)
289     {
290         newOxms[i] = targetDeviceInfo->doxm->oxm[i];
291     }
292     newOxms[targetDeviceInfo->doxm->oxmLen] = newOxm;
293     targetDeviceInfo->doxm->oxmLen++;
294     OICFree(targetDeviceInfo->doxm->oxm);
295     targetDeviceInfo->doxm->oxm = newOxms;
296
297     //Send POST reuqest for update doxm
298     postOxmRes = MOTSendPostDoxm(ctx, targetDeviceInfo, resultCallback, targetDeviceInfo->doxm);
299     VERIFY_SUCCESS(TAG, (OC_STACK_OK == postOxmRes), ERROR);
300
301     OIC_LOG(DEBUG, TAG, "OUT MOTAddMOTMethod");
302
303 exit:
304     OICFree(doxmPayload);
305     return postOxmRes;
306 }
307
308 /**
309  * API to update 'doxm.oxmsel' to resource server.
310  *
311  * @param[in] targetDeviceInfo Selected target device.
312   * @param[in] oxmSelValue Method of multiple ownership transfer (ref. oic.sec.oxm)
313  * @param[in] resultCallback callback provided by API user, callback will be called when
314  *            POST 'oxmsel' request recieves a response from resource server.
315  * @return OC_STACK_OK in case of success and other value otherwise.
316  */
317 OCStackResult MOTSelectMOTMethod(void *ctx, const OCProvisionDev_t *targetDeviceInfo,
318                                  const OicSecOxm_t oxmSelValue, OCProvisionResultCB resultCallback)
319 {
320     OCStackResult postMomRes = OC_STACK_INVALID_CALLBACK;
321     OicSecDoxm_t* doxm = NULL;
322     uint8_t* doxmPayload = NULL;
323     size_t doxmPayloadLen = 0;
324
325     OIC_LOG(DEBUG, TAG, "IN MOTSelectOTMethod");
326
327     VERIFY_NON_NULL(TAG, resultCallback, ERROR);
328     postMomRes = OC_STACK_INVALID_PARAM;
329     VERIFY_NON_NULL(TAG, targetDeviceInfo, ERROR);
330
331     bool isValidOxmsel = false;
332     for(size_t i = 0; i < targetDeviceInfo->doxm->oxmLen; i++)
333     {
334         if(targetDeviceInfo->doxm->oxm[i] == oxmSelValue)
335         {
336             isValidOxmsel = true;
337             break;
338         }
339     }
340     VERIFY_SUCCESS(TAG, isValidOxmsel, ERROR);
341
342     //Dulpicate doxm resource to update the 'oxmsel' property
343     postMomRes = DoxmToCBORPayload(targetDeviceInfo->doxm, &doxmPayload, &doxmPayloadLen, false);
344     VERIFY_SUCCESS(TAG, (OC_STACK_OK == postMomRes), ERROR);
345
346     postMomRes = CBORPayloadToDoxm(doxmPayload, doxmPayloadLen, &doxm);
347     VERIFY_SUCCESS(TAG, (OC_STACK_OK == postMomRes), ERROR);
348     VERIFY_NON_NULL(TAG, doxm, ERROR);
349
350     doxm->oxmSel = oxmSelValue;
351
352     //Send POST reuqest for update doxm
353     postMomRes = MOTSendPostDoxm(ctx, targetDeviceInfo, resultCallback, doxm);
354     VERIFY_SUCCESS(TAG, (OC_STACK_OK == postMomRes), ERROR);
355
356     OIC_LOG(DEBUG, TAG, "OUT MOTSelectOTMethod");
357
358 exit:
359     OICFree(doxmPayload);
360     DeleteDoxmBinData(doxm);
361     return postMomRes;
362 }
363
364 /**
365  * API to provision preconfigured PIN to resource server.
366  *
367  * @param[in] targetDeviceInfo Selected target device.
368  * @param[in] preconfPIN Preconfig PIN which is used while multiple owner authentication
369  * @param[in] preconfPINLen Byte length of preconfig PIN
370  * @param[in] resultCallback callback provided by API user, callback will be called when
371  *            POST credential request recieves a response from resource server.
372  * @return OC_STACK_OK in case of success and other value otherwise.
373  */
374 OCStackResult MOTProvisionPreconfigPIN(void *ctx, const OCProvisionDev_t *targetDeviceInfo,
375                                  const char* preconfPIN, size_t preconfPINLen, OCProvisionResultCB resultCallback)
376 {
377     OCStackResult postCredRes = OC_STACK_INVALID_CALLBACK;
378     bool freeFlag = true;
379     OCSecurityPayload* secPayload = NULL;
380     OTMContext_t *motCtx = NULL;
381     OicSecCred_t* pinCred = NULL;
382
383     OIC_LOG(DEBUG, TAG, "IN MOTProvisionPreconfigPIN");
384
385     VERIFY_NON_NULL(TAG, resultCallback, ERROR);
386     postCredRes = OC_STACK_INVALID_PARAM;
387     VERIFY_NON_NULL(TAG, targetDeviceInfo, ERROR);
388     VERIFY_NON_NULL(TAG, preconfPIN, ERROR);
389     VERIFY_SUCCESS(TAG, (0 != preconfPINLen), ERROR);
390     VERIFY_SUCCESS(TAG, (0 != preconfPINLen && OXM_PRECONFIG_PIN_MAX_SIZE >= preconfPINLen), ERROR);
391
392     postCredRes = OC_STACK_NO_MEMORY;
393     //Generate PIN based credential
394     pinCred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
395     VERIFY_NON_NULL(TAG, pinCred, ERROR);
396
397     pinCred->privateData.data = (uint8_t*)OICMalloc(preconfPINLen + 1);
398     VERIFY_NON_NULL(TAG, pinCred->privateData.data, ERROR);
399
400     memcpy(pinCred->privateData.data, preconfPIN, preconfPINLen);
401     pinCred->privateData.data[preconfPINLen] = '\0';
402     pinCred->privateData.len = preconfPINLen;
403     pinCred->privateData.encoding = OIC_ENCODING_RAW;
404     pinCred->credType = PIN_PASSWORD;
405     OICStrcpy(pinCred->subject.id, sizeof(pinCred->subject.id), WILDCARD_SUBJECT_ID.id);
406
407     //Generate the security payload using updated doxm
408     secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
409     VERIFY_NON_NULL(TAG, secPayload, ERROR);
410     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
411
412     postCredRes = CredToCBORPayload(pinCred, &secPayload->securityData, &secPayload->payloadSize, false);
413     VERIFY_SUCCESS(TAG, (OC_STACK_OK == postCredRes), ERROR);
414
415     OIC_LOG(DEBUG, TAG, "Created Credential payload to register PIN credential:");
416     OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
417
418     char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
419     bool queryGenRes = PMGenerateQuery(true,
420                                        targetDeviceInfo->endpoint.addr,
421                                        targetDeviceInfo->securePort,
422                                        targetDeviceInfo->connType,
423                                        query, sizeof(query), OIC_RSRC_CRED_URI);
424     VERIFY_SUCCESS(TAG, (true == queryGenRes), ERROR);
425     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
426
427     //Create the MOT Context to handle the response message
428     motCtx = (OTMContext_t*)OICCalloc(1, sizeof(OTMContext_t));
429     VERIFY_NON_NULL(TAG, motCtx, ERROR);
430     motCtx->selectedDeviceInfo= targetDeviceInfo;
431     motCtx->ctxResultCallback = resultCallback;
432     motCtx->ctxResultArraySize =1;
433     motCtx->ctxHasError = false;
434     motCtx->userCtx = ctx;
435     motCtx->ctxResultArray = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
436     VERIFY_NON_NULL(TAG, motCtx->ctxResultArray, ERROR);
437
438     //Send POST request
439     OCCallbackData cbData =  {.context=NULL, .cb=NULL, .cd=NULL};
440     cbData.cb = &MOTUpdateSecurityResourceCB;
441     cbData.context = (void *)motCtx;
442     OIC_LOG(DEBUG, TAG, "Sending POST Preconfiged PIN credenatial request to resource server");
443     postCredRes = OCDoResource(NULL, OC_REST_POST, query,
444                               &targetDeviceInfo->endpoint, (OCPayload*)secPayload,
445                               targetDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
446     VERIFY_SUCCESS(TAG, (OC_STACK_OK == postCredRes), ERROR);
447
448     freeFlag = false;
449
450     OIC_LOG(DEBUG, TAG, "OUT MOTProvisionPreconfigPIN");
451
452     return postCredRes;
453
454 exit:
455     //If POST request successfully sent, motCtx will be cleaned from response handler.
456     if(freeFlag && motCtx)
457     {
458         OICFree(motCtx->ctxResultArray);
459         OICFree(motCtx);
460     }
461     if(pinCred)
462     {
463         OICFree(pinCred->privateData.data);
464         OICFree(pinCred);
465     }
466     return postCredRes;
467 }
468
469
470 /**********************************************************************
471  * API for Sub Owner
472  **********************************************************************/
473
474 static OCStackResult StartMultipleOwnershipTransfer(OTMContext_t* motCtx,
475                                                     OCProvisionDev_t* selectedDevice);
476
477 static OTMContext_t* g_MotCtx = NULL;
478
479 static bool IsComplete(OTMContext_t* otmCtx)
480 {
481     for(size_t i = 0; i < otmCtx->ctxResultArraySize; i++)
482     {
483         if(OC_STACK_CONTINUE == otmCtx->ctxResultArray[i].res)
484         {
485             return false;
486         }
487     }
488
489     return true;
490 }
491
492 /**
493  * Function to save the result of multiple ownership transfer.
494  *
495  * @param[in,out] motCtx   Context instance of multiple ownership transfer.
496  * @param[in] res   result of multiple ownership transfer.
497  */
498 static void SetMOTResult(OTMContext_t* motCtx, const OCStackResult res)
499 {
500     OIC_LOG_V(DEBUG, TAG, "IN SetMOTResult : %d ", res);
501     VERIFY_NON_NULL(TAG, motCtx, ERROR);
502
503     if(motCtx->selectedDeviceInfo)
504     {
505         //Revert psk_info callback in case of random PIN OxM
506         if(OIC_RANDOM_DEVICE_PIN == motCtx->selectedDeviceInfo->doxm->oxmSel ||
507            OIC_PRECONFIG_PIN == motCtx->selectedDeviceInfo->doxm->oxmSel)
508         {
509             if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskCredentials))
510             {
511                 OIC_LOG(WARNING, TAG, "Failed to revert the DTLS credential handler.");
512             }
513             OicUuid_t emptyUuid = { .id={0}};
514             SetUuidForPinBasedOxm(&emptyUuid);
515         }
516
517         OCStackResult pdmRetVal = PDMSetDeviceState(&motCtx->selectedDeviceInfo->doxm->deviceID,
518                                                     PDM_DEVICE_ACTIVE);
519         if (OC_STACK_OK != pdmRetVal)
520         {
521             OIC_LOG_V(ERROR, TAG, "Failed to add device information into PDM_DB : %d", res);
522         }
523
524         for(size_t i = 0; i < motCtx->ctxResultArraySize; i++)
525         {
526             if(memcmp(motCtx->selectedDeviceInfo->doxm->deviceID.id,
527                       motCtx->ctxResultArray[i].deviceId.id, UUID_LENGTH) == 0)
528             {
529                 motCtx->ctxResultArray[i].res = res;
530                 if(OC_STACK_OK != res)
531                 {
532                     motCtx->ctxHasError = true;
533                 }
534             }
535         }
536
537         //Remove the current OTM Context from OTM queue
538         RemoveOTMContext(motCtx->selectedDeviceInfo->endpoint.addr,
539                          motCtx->selectedDeviceInfo->securePort);
540
541         //If there is a request being performed, cancel it to prevent retransmission.
542         if(motCtx->ocDoHandle)
543         {
544             if (OC_STACK_OK !=  OCCancel(motCtx->ocDoHandle, OC_HIGH_QOS, NULL, 0))
545             {
546                 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
547             }
548             motCtx->ocDoHandle = NULL;
549         }
550
551         //If all request is completed, invoke the user callback.
552         if(IsComplete(motCtx))
553         {
554             motCtx->ctxResultCallback(motCtx->userCtx, motCtx->ctxResultArraySize,
555                                        motCtx->ctxResultArray, motCtx->ctxHasError);
556
557             OICFree(motCtx->ctxResultArray);
558             OICFree(motCtx);
559         }
560         else
561         {
562             if(OC_STACK_OK != StartMultipleOwnershipTransfer(motCtx,
563                                                      motCtx->selectedDeviceInfo->next))
564             {
565                 OIC_LOG(ERROR, TAG, "Failed to StartMultipleOwnershipTransfer");
566             }
567         }
568     }
569
570 exit:
571     OIC_LOG(DEBUG, TAG, "OUT SetMOTResult");
572 }
573
574 /**
575  * API to add preconfigured PIN to local SVR DB.
576  *
577  * @param[in] targetDeviceInfo Selected target device.
578  * @param[in] preconfPIN Preconfig PIN which is used while multiple owner authentication
579  * @param[in] preconfPINLen Byte length of preconfig PIN
580  * @param[in] resultCallback callback provided by API user, callback will be called when
581  *            POST credential request recieves a response from resource server.
582  * @return OC_STACK_OK in case of success and other value otherwise.
583  */
584 OCStackResult MOTAddPreconfigPIN(const OCProvisionDev_t *targetDeviceInfo,
585                                  const char* preconfPIN, size_t preconfPINLen)
586 {
587     OCStackResult addCredRes = OC_STACK_INVALID_PARAM;
588     OicSecCred_t* pinCred = NULL;
589     bool freeFlag = true;
590
591     OIC_LOG(DEBUG, TAG, "IN MOTAddPreconfigPIN");
592
593     VERIFY_NON_NULL(TAG, targetDeviceInfo, ERROR);
594     VERIFY_NON_NULL(TAG, preconfPIN, ERROR);
595     VERIFY_SUCCESS(TAG, (0 != preconfPINLen), ERROR);
596     VERIFY_SUCCESS(TAG, (0 != preconfPINLen && OXM_PRECONFIG_PIN_MAX_SIZE >= preconfPINLen), ERROR);
597
598     OicSecCred_t* prevCred = GetCredResourceData(&targetDeviceInfo->doxm->deviceID);
599     if(NULL != prevCred)
600     {
601         OIC_LOG(INFO, TAG, "PIN/PW Credential already exist!");
602         return OC_STACK_OK;
603     }
604
605     addCredRes = OC_STACK_NO_MEMORY;
606     //Generate PIN based credential
607     pinCred = (OicSecCred_t*)OICCalloc(1, sizeof(OicSecCred_t));
608     VERIFY_NON_NULL(TAG, pinCred, ERROR);
609
610     pinCred->privateData.data = (uint8_t*)OICMalloc(preconfPINLen + 1);
611     VERIFY_NON_NULL(TAG, pinCred->privateData.data, ERROR);
612
613     memcpy(pinCred->privateData.data, preconfPIN, preconfPINLen);
614     pinCred->privateData.data[preconfPINLen] = '\0';
615     pinCred->privateData.len = preconfPINLen;
616     pinCred->privateData.encoding = OIC_ENCODING_RAW;
617     pinCred->credType = PIN_PASSWORD;
618     memcpy(pinCred->subject.id, targetDeviceInfo->doxm->deviceID.id, sizeof(pinCred->subject.id));
619
620     addCredRes = AddCredential(pinCred);
621     VERIFY_SUCCESS(TAG, (OC_STACK_OK == addCredRes), ERROR);
622
623     OIC_LOG(DEBUG, TAG, "OUT MOTAddPreconfigPIN");
624
625     return addCredRes;
626
627 exit:
628     if(pinCred)
629     {
630         OICFree(pinCred->privateData.data);
631         OICFree(pinCred);
632     }
633     return addCredRes;
634 }
635
636 /**
637  * Function to save the SubOwner PSK.
638  *
639  * @param[in] selectedDeviceInfo   selected device information to performing provisioning.
640  * @return  OC_STACK_OK on success
641  */
642 static OCStackResult SaveSubOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
643 {
644     OIC_LOG(DEBUG, TAG, "IN SaveSubOwnerPSK");
645
646     OCStackResult res = OC_STACK_ERROR;
647
648     CAEndpoint_t endpoint;
649     memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
650     OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
651     endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
652     endpoint.port = selectedDeviceInfo->securePort;
653     endpoint.adapter = selectedDeviceInfo->endpoint.adapter;
654
655     OicUuid_t ownerDeviceID = {.id={0}};
656     if (OC_STACK_OK != GetDoxmDeviceID(&ownerDeviceID))
657     {
658         OIC_LOG(ERROR, TAG, "Error while retrieving SubOwner's device ID");
659         return res;
660     }
661
662     uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
663     OicSecKey_t ownerKey = {ownerPSK, OWNER_PSK_LENGTH_128};
664
665     //Generating SubOwnerPSK
666     CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
667             (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
668             strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)),
669             ownerDeviceID.id, sizeof(ownerDeviceID.id),
670             selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id),
671             ownerPSK, OWNER_PSK_LENGTH_128);
672
673     if (CA_STATUS_OK == pskRet)
674     {
675         OIC_LOG(DEBUG, TAG, "SubOwner PSK dump:");
676         OIC_LOG_BUFFER(DEBUG, TAG, ownerPSK, OWNER_PSK_LENGTH_128);
677         //Generating new credential for provisioning tool
678         OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
679                                       SYMMETRIC_PAIR_WISE_KEY, NULL,
680                                       &ownerKey, &ownerDeviceID, &ownerDeviceID);
681         VERIFY_NON_NULL(TAG, cred, ERROR);
682
683         uint32_t outSize = 0;
684         size_t b64BufSize = B64ENCODE_OUT_SAFESIZE((OWNER_PSK_LENGTH_128 + 1));
685         char* b64Buf = (uint8_t *)OICCalloc(1, b64BufSize);
686         VERIFY_NON_NULL(TAG, b64Buf, ERROR);
687         b64Encode(cred->privateData.data, cred->privateData.len, b64Buf, b64BufSize, &outSize);
688
689         OICFree( cred->privateData.data );
690         cred->privateData.data = (uint8_t *)OICCalloc(1, outSize + 1);
691         VERIFY_NON_NULL(TAG, cred->privateData.data, ERROR);
692
693         strncpy(cred->privateData.data, b64Buf, outSize);
694         cred->privateData.data[outSize] = '\0';
695         cred->privateData.encoding = OIC_ENCODING_BASE64;
696         cred->privateData.len = outSize;
697         OICFree(b64Buf);
698
699         //Add SubOwnerPSK
700         res = AddCredential(cred);
701         if(res != OC_STACK_OK)
702         {
703             DeleteCredList(cred);
704             return res;
705         }
706     }
707     else
708     {
709         OIC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
710     }
711
712     OIC_LOG(DEBUG, TAG, "OUT SaveSubOwnerPSK");
713 exit:
714     return res;
715 }
716
717
718 /**
719  * Response handler for update subowner crendetial request.
720  *
721  * @param[in] ctx             ctx value passed to callback from calling function.
722  * @param[in] UNUSED          handle to an invocation
723  * @param[in] clientResponse  Response from queries to remote servers.
724  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
725  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
726  */
727 static OCStackApplicationResult SubOwnerCredentialHandler(void *ctx, OCDoHandle UNUSED,
728                                 OCClientResponse *clientResponse)
729 {
730     VERIFY_NON_NULL(TAG, clientResponse, WARNING);
731     VERIFY_NON_NULL(TAG, ctx, WARNING);
732
733     OIC_LOG(DEBUG, TAG, "IN SubOwnerCredentialHandler");
734     (void)UNUSED;
735     OTMContext_t* motCtx = (OTMContext_t*)ctx;
736
737     if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
738     {
739         if(motCtx && motCtx->selectedDeviceInfo)
740         {
741             //Close the temporal secure session to verify the owner credential
742             CAEndpoint_t* endpoint = (CAEndpoint_t *)&motCtx->selectedDeviceInfo->endpoint;
743             endpoint->port = motCtx->selectedDeviceInfo->securePort;
744             CAResult_t caResult = CAcloseSslSession(endpoint);
745             if(CA_STATUS_OK != caResult)
746             {
747                 OIC_LOG(ERROR, TAG, "Failed to close DTLS session");
748                 SetMOTResult(motCtx, OC_STACK_ERROR);
749                 return OC_STACK_DELETE_TRANSACTION;
750             }
751
752             // TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 = 0xC037, /**< see RFC 5489 */
753             caResult = CASelectCipherSuite(0xC037, endpoint->adapter);
754             if(CA_STATUS_OK != caResult)
755             {
756                 OIC_LOG(ERROR, TAG, "Failed to select TLS_NULL_WITH_NULL_NULL");
757                 SetMOTResult(motCtx, OC_STACK_ERROR);
758                 return OC_STACK_DELETE_TRANSACTION;
759             }
760
761             SetMOTResult(motCtx, OC_STACK_OK);
762         }
763     }
764     else
765     {
766         OIC_LOG_V(ERROR, TAG, "SubOwnerCredentialHandler : Unexpected result %d",
767                   clientResponse->result);
768         SetMOTResult(motCtx, clientResponse->result);
769     }
770
771     OIC_LOG(DEBUG, TAG, "OUT SubOwnerCredentialHandler");
772
773 exit:
774     return  OC_STACK_DELETE_TRANSACTION;
775 }
776
777
778 static OCStackResult PostSubOwnerCredential(OTMContext_t* motCtx)
779 {
780     OIC_LOG(DEBUG, TAG, "IN PostSubOwnerCredential");
781
782     if(!motCtx || !motCtx->selectedDeviceInfo)
783     {
784         OIC_LOG(ERROR, TAG, "Invalid parameters");
785         return OC_STACK_INVALID_PARAM;
786     }
787
788     OCProvisionDev_t* deviceInfo = motCtx->selectedDeviceInfo;
789     char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
790
791     if(!PMGenerateQuery(true,
792                         deviceInfo->endpoint.addr, deviceInfo->securePort,
793                         deviceInfo->connType,
794                         query, sizeof(query), OIC_RSRC_CRED_URI))
795     {
796         OIC_LOG(ERROR, TAG, "PostSubOwnerCredential : Failed to generate query");
797         return OC_STACK_ERROR;
798     }
799     OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
800     OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
801     if(!secPayload)
802     {
803         OIC_LOG(ERROR, TAG, "Failed to memory allocation");
804         return OC_STACK_NO_MEMORY;
805     }
806
807     //Generate sub-owner credential for new device
808     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
809     const OicSecCred_t* ownerCredential = GetCredResourceData(&(deviceInfo->doxm->deviceID));
810     if(!ownerCredential)
811     {
812         OIC_LOG(ERROR, TAG, "Can not find SubOwnerPSK.");
813         return OC_STACK_NO_RESOURCE;
814     }
815
816     OicUuid_t ownerId = {.id={0}};
817     if(OC_STACK_OK == GetDoxmDeviceID(&ownerId))
818     {
819         OicSecCred_t newCredential;
820         memcpy(&newCredential, ownerCredential, sizeof(OicSecCred_t));
821         newCredential.next = NULL;
822
823         //Set subject ID as SubOwner's ID
824         memcpy(&(newCredential.subject), &ownerId, sizeof(OicUuid_t));
825
826         //Set eowner ID as SubOwner's ID
827         if(NULL == newCredential.eownerID)
828         {
829             newCredential.eownerID = OICCalloc(1, sizeof(OicUuid_t));
830             if(NULL == newCredential.eownerID)
831             {
832                 return OC_STACK_NO_MEMORY;
833             }
834         }
835         memcpy(newCredential.eownerID->id, ownerId.id, sizeof(ownerId.id));
836
837         //Fill private data as empty string
838         newCredential.privateData.data = "";
839         newCredential.privateData.len = 0;
840         newCredential.privateData.encoding = ownerCredential->privateData.encoding;
841 #ifdef __WITH_X509__
842         newCredential.publicData.data = NULL;
843         newCredential.publicData.len = 0;
844 #endif
845         //Send owner credential to new device : POST /oic/sec/cred [ owner credential ]
846         if (OC_STACK_OK != CredToCBORPayload(&newCredential, &secPayload->securityData,
847                                         &secPayload->payloadSize, 0))
848         {
849             OICFree(secPayload);
850             OIC_LOG(ERROR, TAG, "Error while converting bin to cbor.");
851             return OC_STACK_ERROR;
852         }
853         OIC_LOG(DEBUG, TAG, "Cred Payload:");
854         OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
855
856         OCCallbackData cbData;
857         cbData.cb = &SubOwnerCredentialHandler;
858         cbData.context = (void *)motCtx;
859         cbData.cd = NULL;
860         OCStackResult res = OCDoResource(NULL, OC_REST_POST, query,
861                                          &deviceInfo->endpoint, (OCPayload*)secPayload,
862                                          deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
863         if (res != OC_STACK_OK)
864         {
865             OIC_LOG(ERROR, TAG, "OCStack resource error");
866         }
867     }
868     else
869     {
870         OIC_LOG(ERROR, TAG, "Failed to read DOXM device ID.");
871         return OC_STACK_NO_RESOURCE;
872     }
873
874     OIC_LOG(DEBUG, TAG, "OUT PostSubOwnerCredential");
875
876     return OC_STACK_OK;
877 }
878
879
880 /**
881  * Function to handle the handshake result in MOT.
882  * This function will be invoked after DTLS handshake
883  * @param   endPoint  [IN] The remote endpoint.
884  * @param   errorInfo [IN] Error information from the endpoint.
885  * @return  NONE
886  */
887 static void MOTDtlsHandshakeCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
888 {
889     OIC_LOG(INFO, TAG, "IN MOTDtlsHandshakeCB");
890
891     if(NULL != endpoint && NULL != info)
892     {
893         OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
894                  endpoint->addr, endpoint->port, info->result);
895
896         OTMContext_t* motCtx = GetOTMContext(endpoint->addr, endpoint->port);
897         if(motCtx)
898         {
899             OicSecDoxm_t* newDevDoxm = motCtx->selectedDeviceInfo->doxm;
900
901             if(NULL != newDevDoxm)
902             {
903                 OicUuid_t emptyUuid = {.id={0}};
904
905                 //Make sure the address matches.
906                 if(strncmp(motCtx->selectedDeviceInfo->endpoint.addr,
907                    endpoint->addr,
908                    sizeof(endpoint->addr)) == 0 &&
909                    motCtx->selectedDeviceInfo->securePort == endpoint->port)
910                 {
911                     OCStackResult res = OC_STACK_ERROR;
912
913                     //If temporal secure sesstion established successfully
914                     if(CA_STATUS_OK == info->result)
915                     {
916                         //Delete previous credential such as preconfigured-pin
917                         RemoveCredential(&(motCtx->selectedDeviceInfo->doxm->deviceID));
918
919                         res = SaveSubOwnerPSK(motCtx->selectedDeviceInfo);
920                         if(OC_STACK_OK == res)
921                         {
922                             //POST sub owner credential to new device.
923                             res = PostSubOwnerCredential(motCtx);
924                             if(OC_STACK_OK != res)
925                             {
926                                 OIC_LOG(ERROR, TAG,
927                                         "Failed to send POST request for SubOwner Credential");
928                                 SetMOTResult(motCtx, res);
929                             }
930                         }
931                         else
932                         {
933                             OIC_LOG(ERROR, TAG, "Failed to save the SubOwner PSK.");
934                             SetMOTResult(motCtx, res);
935                         }
936                     }
937                     //In case of authentication failure
938                     else if(CA_DTLS_AUTHENTICATION_FAILURE == info->result)
939                     {
940                         //in case of error from wrong PIN, re-start the ownership transfer
941                         if(OIC_RANDOM_DEVICE_PIN == newDevDoxm->oxmSel)
942                         {
943                             OIC_LOG(ERROR, TAG, "The PIN number may incorrect.");
944
945                             motCtx->attemptCnt++;
946
947                             if(WRONG_PIN_MAX_ATTEMP > motCtx->attemptCnt)
948                             {
949                                 res = StartMultipleOwnershipTransfer(motCtx, motCtx->selectedDeviceInfo);
950                                 if(OC_STACK_OK != res)
951                                 {
952                                     SetMOTResult(motCtx, res);
953                                     OIC_LOG(ERROR, TAG, "Failed to Re-StartOwnershipTransfer");
954                                 }
955                             }
956                             else
957                             {
958                                 OIC_LOG(ERROR, TAG, "User has exceeded the number of authentication attempts.");
959                                 SetMOTResult(motCtx, OC_STACK_AUTHENTICATION_FAILURE);
960                             }
961                         }
962                         else
963                         {
964                             OIC_LOG(ERROR, TAG, "Failed to establish DTLS session.");
965                             SetMOTResult(motCtx, OC_STACK_AUTHENTICATION_FAILURE);
966                         }
967                     }
968                 }
969             }
970         }
971         else
972         {
973             OIC_LOG_V(ERROR, TAG, "Can not find the [%s:%d]'s OTMContext for MOT", endpoint->addr, endpoint->port);
974         }
975     }
976
977     OIC_LOG(INFO, TAG, "OUT MOTDtlsHandshakeCB");
978 }
979
980 static OCStackResult StartMultipleOwnershipTransfer(OTMContext_t* motCtx,
981                                                     OCProvisionDev_t* selectedDevice)
982 {
983     OIC_LOG(INFO, TAG, "IN StartMultipleOwnershipTransfer");
984     OCStackResult res = OC_STACK_INVALID_PARAM;
985
986     VERIFY_NON_NULL(TAG, motCtx, ERROR);
987     VERIFY_NON_NULL(TAG, selectedDevice, ERROR);
988     VERIFY_NON_NULL(TAG, selectedDevice->doxm, ERROR);
989     motCtx->selectedDeviceInfo = selectedDevice;
990
991     //Checking duplication of Device ID.
992     char* strUuid = NULL;
993     PdmDeviceState_t deviceState = PDM_DEVICE_UNKNOWN;
994     res = PDMGetDeviceState(&selectedDevice->doxm->deviceID, &deviceState);
995     VERIFY_SUCCESS(TAG, OC_STACK_OK == res, ERROR);
996
997     res = ConvertUuidToStr(&selectedDevice->doxm->deviceID, &strUuid);
998     if(OC_STACK_OK != res)
999     {
1000         OIC_LOG(ERROR, TAG, "Failed to convert UUID to str");
1001         SetMOTResult(motCtx, res);
1002         return res;
1003     }
1004
1005     if(PDM_DEVICE_STALE == deviceState)
1006     {
1007         OIC_LOG_V(WARNING, TAG, "Detected duplicated UUID in stale status, "
1008                            "[%s] will be removed from PDM", strUuid);
1009
1010         res = PDMDeleteDevice(&selectedDevice->doxm->deviceID);
1011         if(OC_STACK_OK != res)
1012         {
1013             OIC_LOG(ERROR, TAG, "Internal error in PDMDeleteDevice");
1014             OICFree(strUuid);
1015             SetMOTResult(motCtx, res);
1016         }
1017     }
1018     else if(PDM_DEVICE_INIT == deviceState)
1019     {
1020         OIC_LOG_V(ERROR, TAG, "[%s]'s multiple owner transfer process is already started.", strUuid);
1021         OICFree(strUuid);
1022         SetMOTResult(motCtx, OC_STACK_DUPLICATE_REQUEST);
1023         return OC_STACK_OK;
1024     }
1025
1026     res = PDMAddDevice(&selectedDevice->doxm->deviceID);
1027     if (OC_STACK_OK != res)
1028     {
1029         OIC_LOG_V(INFO, TAG, "Error in PDMAddDevice for [%s]", strUuid);
1030         OICFree(strUuid);
1031         SetMOTResult(motCtx, res);
1032         return res;
1033     }
1034     OICFree(strUuid);
1035
1036     //Register DTLS event handler to catch the dtls event while handshake
1037     if(CA_STATUS_OK != CAregisterSslHandshakeCallback(MOTDtlsHandshakeCB))
1038     {
1039         OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register DTLS handshake callback.");
1040     }
1041
1042     OicSecOxm_t oxmSel = selectedDevice->doxm->oxmSel;
1043     OIC_LOG_V(DEBUG, TAG, "Multiple Ownership Transfer method = %d", (int)oxmSel);
1044
1045     if(OIC_PRECONFIG_PIN != oxmSel && OIC_RANDOM_DEVICE_PIN != oxmSel)
1046     {
1047         OIC_LOG(ERROR, TAG, "Unsupported OxM");
1048         return OC_STACK_ERROR;
1049     }
1050
1051     res = OTMSetOTCallback(selectedDevice->doxm->oxmSel, &motCtx->otmCallback);
1052     if(OC_STACK_OK != res)
1053     {
1054         OIC_LOG_V(ERROR, TAG, "Error in OTMSetOTCallback : %d", res);
1055         return res;
1056     }
1057     //Only two functions required for MOT
1058     VERIFY_NON_NULL(TAG, motCtx->otmCallback.loadSecretCB, ERROR);
1059     VERIFY_NON_NULL(TAG, motCtx->otmCallback.createSecureSessionCB, ERROR);
1060
1061     if(OIC_RANDOM_DEVICE_PIN == oxmSel)
1062     {
1063         if(CA_STATUS_OK != CAregisterPskCredentialsHandler(GetDtlsPskForRandomPinOxm))
1064         {
1065             OIC_LOG(ERROR, TAG, "Failed to register DTLS credential handler for Random PIN OxM.");
1066         }
1067     }
1068
1069     //Save the current context instance to use on the dtls handshake callback
1070     res = AddOTMContext(motCtx, selectedDevice->endpoint.addr, selectedDevice->securePort);
1071     VERIFY_SUCCESS(TAG, OC_STACK_OK == res, ERROR);
1072
1073     res = motCtx->otmCallback.loadSecretCB(motCtx);
1074     VERIFY_SUCCESS(TAG, OC_STACK_OK == res, ERROR);
1075
1076     res = motCtx->otmCallback.createSecureSessionCB(motCtx);
1077     VERIFY_SUCCESS(TAG, OC_STACK_OK == res, ERROR);
1078
1079     OIC_LOG(INFO, TAG, "OUT StartMultipleOwnershipTransfer");
1080
1081 exit:
1082     return res;
1083 }
1084
1085 OCStackResult MOTDoOwnershipTransfer(void* ctx,
1086                                      OCProvisionDev_t *selectedDevicelist,
1087                                      OCProvisionResultCB resultCallback)
1088 {
1089     OIC_LOG(DEBUG, TAG, "IN MOTDoOwnershipTransfer");
1090     OCStackResult res = OC_STACK_INVALID_PARAM;
1091     OTMContext_t* motCtx = NULL;
1092     OCProvisionDev_t* pCurDev = NULL;
1093
1094     VERIFY_NON_NULL(TAG, selectedDevicelist, ERROR);
1095     VERIFY_NON_NULL(TAG, resultCallback, ERROR);
1096
1097     res = OC_STACK_NO_MEMORY;
1098     motCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
1099     VERIFY_NON_NULL(TAG, motCtx, ERROR);
1100
1101     motCtx->ctxResultCallback = resultCallback;
1102     motCtx->ctxHasError = false;
1103     motCtx->userCtx = ctx;
1104     motCtx->ctxResultArraySize = 0;
1105     LL_FOREACH(selectedDevicelist, pCurDev)
1106     {
1107         motCtx->ctxResultArraySize++;
1108     }
1109
1110     motCtx->ctxResultArray =
1111         (OCProvisionResult_t*)OICCalloc(motCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
1112     VERIFY_NON_NULL(TAG, motCtx->ctxResultArray, ERROR);
1113
1114     //Fill the device UUID for result array.
1115     size_t devIdx = 0;
1116     pCurDev = NULL;
1117     LL_FOREACH(selectedDevicelist, pCurDev)
1118     {
1119         memcpy(motCtx->ctxResultArray[devIdx].deviceId.id,
1120                pCurDev->doxm->deviceID.id,
1121                UUID_LENGTH);
1122         motCtx->ctxResultArray[devIdx].res = OC_STACK_CONTINUE;
1123         devIdx++;
1124     }
1125
1126     motCtx->selectedDeviceInfo = selectedDevicelist;
1127     res = StartMultipleOwnershipTransfer(motCtx, selectedDevicelist);
1128     VERIFY_SUCCESS(TAG, OC_STACK_OK == res, ERROR);
1129
1130     OIC_LOG(DEBUG, TAG, "OUT MOTDoOwnershipTransfer");
1131
1132 exit:
1133     if(OC_STACK_OK != res)
1134     {
1135         if(motCtx)
1136         {
1137             OICFree(motCtx->ctxResultArray);
1138             OICFree(motCtx);
1139         }
1140     }
1141     return res;
1142 }