Merge branch 'master' into easysetup & CBOR changes
[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 <time.h>
34 #include <unistd.h>
35 #include <sys/time.h>
36 #include <stdbool.h>
37 #include <string.h>
38
39 #include "logger.h"
40 #include "oic_malloc.h"
41 #include "oic_string.h"
42 #include "cacommon.h"
43 #include "cainterface.h"
44 #include "base64.h"
45 #include "cJSON.h"
46
47 #include "srmresourcestrings.h"
48 #include "doxmresource.h"
49 #include "pstatresource.h"
50 #include "credresource.h"
51 #include "aclresource.h"
52 #include "ownershiptransfermanager.h"
53 #include "securevirtualresourcetypes.h"
54 #include "oxmjustworks.h"
55 #include "pmtypes.h"
56 #include "pmutility.h"
57 #include "srmutility.h"
58
59 // TODO: Not yet supported.
60 //#include "oxmrandompin.h"
61
62 #define DEFAULT_CONTEXT_VALUE 0x99
63 #define DEFAULT_SECURE_PORT 5684
64
65 #define TAG "OTM"
66
67 OCProvisionResultCB g_resultCallback;
68 OCProvisionResult_t* g_resultArray = NULL;
69 size_t g_resultArraySize = 0;
70 bool g_hasError = false;
71
72 /**
73  * Possible states of ownership transfer manager module.
74  */
75 typedef enum
76 {
77     SP_NO_MASK                = (0       ),
78     SP_DISCOVERY_STARTED      = (0x1 << 1),
79     SP_DISCOVERY_ERROR        = (0x1 << 2),
80     SP_DISCOVERY_DONE         = (0x1 << 3),
81     SP_UP_OWN_TR_METH_STARTED = (0x1 << 4),
82     SP_UP_OWN_TR_METH_ERROR   = (0x1 << 5),
83     SP_UP_OWN_TR_METH_DONE    = (0x1 << 6),
84     SP_LIST_METHODS_STARTED   = (0x1 << 7),
85     SP_LIST_METHODS_ERROR     = (0x1 << 8),
86     SP_LIST_METHODS_DONE      = (0x1 << 9),
87     SP_UPDATE_OP_MODE_STARTED = (0x1 << 10),
88     SP_UPDATE_OP_MODE_ERROR   = (0x1 << 11),
89     SP_UPDATE_OP_MODE_DONE    = (0x1 << 12),
90     SP_UPDATE_OWNER_STARTED   = (0x1 << 13),
91     SP_UPDATE_OWNER_ERROR     = (0x1 << 14),
92     SP_UPDATE_OWNER_DONE      = (0x1 << 15)
93 } OTMStates;
94
95
96
97 /**
98  * Array to store the callbacks for each owner transfer method.
99  */
100 OTMCallbackData_t g_OTMDatas[OIC_OXM_COUNT];
101
102 /**
103  * Variable for storing provisioning tool's provisioning capabilities
104  * Must be in decreasing order of preference. More prefered method should
105  * have lower array index.
106  */
107 static OicSecDpom_t gProvisioningToolCapability[] = { SINGLE_SERVICE_CLIENT_DRIVEN };
108
109 /**
110  * Number of supported provisioning methods
111  * current version supports only one.
112  */
113 static size_t gNumOfProvisioningMethodsPT = 1;
114
115 /**
116  * Function to getting string of ownership transfer method
117  */
118 static const char* GetOxmString(OicSecOxm_t oxmType)
119 {
120     switch(oxmType)
121     {
122         case OIC_JUST_WORKS:
123             return OXM_JUST_WORKS;
124         case OIC_MODE_SWITCH:
125             return OXM_MODE_SWITCH;
126         case OIC_RANDOM_DEVICE_PIN:
127             return OXM_RANDOM_DEVICE_PIN;
128         case OIC_PRE_PROVISIONED_DEVICE_PIN:
129             return OXM_PRE_PROVISIONED_DEVICE_PIN;
130         case OIC_PRE_PROVISION_STRONG_CREDENTIAL:
131             return OXM_PRE_PROVISIONED_STRONG_CREDENTIAL;
132         default:
133             return NULL;
134     }
135 }
136
137 /**
138  * Function to select appropriate  provisioning method.
139  *
140  * @param[in] supportedMethods   Array of supported methods
141  * @param[in] numberOfMethods   number of supported methods
142  * @param[out]  selectedMethod         Selected methods
143  * @return  SP_SUCCESS on success
144  */
145 static OCStackResult SelectProvisioningMethod(const OicSecOxm_t *supportedMethods,
146                                                             size_t numberOfMethods,
147                                                             OicSecOxm_t *selectedMethod)
148 {
149     OC_LOG(DEBUG, TAG, "IN SelectProvisioningMethod");
150
151     if(numberOfMethods == 0 || !supportedMethods)
152     {
153         OC_LOG(WARNING, TAG, "Could not find a supported OxM.");
154         return OC_STACK_ERROR;
155     }
156
157     *selectedMethod  = supportedMethods[0];
158     for(size_t i = 0; i < numberOfMethods; i++)
159     {
160         if(*selectedMethod < supportedMethods[i])
161         {
162             *selectedMethod =  supportedMethods[i];
163         }
164     }
165
166     return OC_STACK_OK;
167 }
168
169 /**
170  * Function to select operation mode.This function will return most secure common operation mode.
171  *
172  * @param[in] selectedDeviceInfo   selected device information to performing provisioning.
173  * @param[out]   selectedMode   selected operation mode
174  * @return  SP_SUCCESS on success
175  */
176 static void SelectOperationMode(const OCProvisionDev_t *selectedDeviceInfo,
177                                 OicSecDpom_t *selectedMode)
178 {
179     OC_LOG(DEBUG, TAG, "IN SelectOperationMode");
180
181     size_t i = 0;
182     size_t j = 0;
183
184     while (i < gNumOfProvisioningMethodsPT && j < selectedDeviceInfo->pstat->smLen)
185     {
186         if (gProvisioningToolCapability[i] < selectedDeviceInfo->pstat->sm[j])
187         {
188             i++;
189         }
190         else if (selectedDeviceInfo->pstat->sm[j] < gProvisioningToolCapability[i])
191         {
192             j++;
193         }
194         else /* if gProvisioningToolCapability[i] == deviceSupportedMethods[j] */
195         {
196             *selectedMode = gProvisioningToolCapability[j];
197             break;
198         }
199     }
200     OC_LOG(DEBUG, TAG, "OUT SelectOperationMode");
201 }
202
203 /**
204  * Function to update owner transfer mode
205  *
206  * @param[in]  otmCtx  Context value of ownership transfer.
207  * @return  OC_STACK_OK on success
208  */
209 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx);
210
211 /**
212  * Function to send request to resource to get its pstat resource information.
213  *
214  * @param[in]  otmCtx  Context value of ownership transfer.
215  * @return  OC_STACK_OK on success
216  */
217 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx);
218
219
220 /**
221  * Function to send ownerShip info. This function would update Owned as true and
222  * owner as UUID for provisioning tool
223  *
224  * @param[in]  otmCtx  Context value of ownership transfer.
225  * @return  OC_STACK_OK on success
226  */
227 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx);
228
229 /**
230  * Function to update the operation mode. As per the spec. Operation mode in client driven
231  * single service provisioning it will be updated to 0x3
232  *
233  * @param[in]  otmCtx  Context value of ownership transfer.
234  * @param[in] selectedOperationMode selected operation mode
235  * @return  OC_STACK_OK on success
236  */
237 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx,
238                                     OicSecDpom_t selectedOperationMode);
239
240 /**
241  * Function to start ownership transfer.
242  * This function will send the first request for provisioning,
243  * The next request message is sent from the response handler for this request.
244  *
245  * @param[in] ctx   context value passed to callback from calling function.
246  * @param[in] selectedDevice   selected device information to performing provisioning.
247  * @return  OC_STACK_OK on success
248  */
249 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice);
250
251 /*
252  * Function to finalize provisioning.
253  * This function will send default ACL and commit hash.
254  *
255  * @param[in] otmCtx   Context value of ownership transfer.
256  * @return  OC_STACK_OK on success
257  */
258 static OCStackResult FinalizeProvisioning(OTMContext_t* otmCtx);
259
260
261 static bool IsComplete()
262 {
263     for(size_t i = 0; i < g_resultArraySize; i++)
264     {
265         if(OC_STACK_CONTINUE == g_resultArray[i].res)
266         {
267             return false;
268         }
269     }
270
271     return true;
272 }
273
274 /**
275  * Function to save the result of provisioning.
276  *
277  * @param[in,out] otmCtx   Context value of ownership transfer.
278  * @param[in] res   result of provisioning
279  */
280 static void SetResult(OTMContext_t* otmCtx, const OCStackResult res)
281 {
282     OC_LOG(DEBUG, TAG, "IN SetResult");
283
284     if(!otmCtx)
285     {
286         OC_LOG(WARNING, TAG, "OTMContext is NULL");
287         return;
288     }
289
290     if(otmCtx->selectedDeviceInfo)
291     {
292         for(size_t i = 0; i < g_resultArraySize; i++)
293         {
294             if(memcmp(otmCtx->selectedDeviceInfo->doxm->deviceID.id,
295                       g_resultArray[i].deviceId.id, UUID_LENGTH) == 0)
296             {
297                 g_resultArray[i].res = res;
298                 if(OC_STACK_OK != res)
299                 {
300                     g_hasError = true;
301                 }
302             }
303         }
304
305         //If all request is completed, invoke the user callback.
306         if(IsComplete())
307         {
308             g_resultCallback(otmCtx->userCtx, g_resultArraySize, g_resultArray, g_hasError);
309         }
310         else
311         {
312             if(OC_STACK_OK != StartOwnershipTransfer(otmCtx->userCtx,
313                                                      otmCtx->selectedDeviceInfo->next))
314             {
315                 OC_LOG(ERROR, TAG, "Failed to StartOwnershipTransfer");
316             }
317         }
318     }
319
320     OC_LOG(DEBUG, TAG, "OUT SetResult");
321
322     OICFree(otmCtx);
323 }
324
325
326 /**
327  * Function to save ownerPSK at provisioning tool end.
328  *
329  * @param[in] selectedDeviceInfo   selected device information to performing provisioning.
330  * @return  OC_STACK_OK on success
331  */
332 static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo)
333 {
334     OC_LOG(DEBUG, TAG, "IN SaveOwnerPSK");
335
336     OCStackResult res = OC_STACK_ERROR;
337
338     CAEndpoint_t endpoint;
339     memset(&endpoint, 0x00, sizeof(CAEndpoint_t));
340     OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
341     endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0';
342     endpoint.port = selectedDeviceInfo->securePort;
343
344     OicUuid_t ptDeviceID = {.id={0}};
345     if (OC_STACK_OK != GetDoxmDeviceID(&ptDeviceID))
346     {
347         OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
348         return res;
349     }
350
351     uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0};
352
353     //Generating OwnerPSK
354     CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
355             (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel),
356             strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)), ptDeviceID.id,
357             sizeof(ptDeviceID.id), selectedDeviceInfo->doxm->deviceID.id,
358             sizeof(selectedDeviceInfo->doxm->deviceID.id), ownerPSK,
359             OWNER_PSK_LENGTH_128);
360
361     if (CA_STATUS_OK == pskRet)
362     {
363         OC_LOG(INFO, TAG,"ownerPSK dump:\n");
364         OC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
365         //Generating new credential for provisioning tool
366         size_t ownLen = 1;
367         uint32_t outLen = 0;
368
369         char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(ownerPSK)) + 1] = {};
370         B64Result b64Ret = b64Encode(ownerPSK, sizeof(ownerPSK), base64Buff, sizeof(base64Buff),
371                 &outLen);
372         VERIFY_SUCCESS(TAG, B64_OK == b64Ret, ERROR);
373
374         OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
375                 SYMMETRIC_PAIR_WISE_KEY, NULL,
376                 base64Buff, ownLen, &ptDeviceID);
377         VERIFY_NON_NULL(TAG, cred, ERROR);
378
379         res = AddCredential(cred);
380         if(res != OC_STACK_OK)
381         {
382             DeleteCredList(cred);
383             return res;
384         }
385     }
386     else
387     {
388         OC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
389     }
390
391     OC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK");
392 exit:
393     return res;
394 }
395
396 /**
397  * Callback handler for OwnerShipTransferModeHandler API.
398  *
399  * @param[in] ctx             ctx value passed to callback from calling function.
400  * @param[in] UNUSED          handle to an invocation
401  * @param[in] clientResponse  Response from queries to remote servers.
402  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
403  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
404  */
405 static OCStackApplicationResult OwnerTransferModeHandler(void *ctx, OCDoHandle UNUSED,
406                                                          OCClientResponse *clientResponse)
407 {
408     OC_LOG(DEBUG, TAG, "IN OwnerTransferModeHandler");
409
410     VERIFY_NON_NULL(TAG, clientResponse, WARNING);
411     VERIFY_NON_NULL(TAG, ctx, WARNING);
412
413     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
414     (void)UNUSED;
415     if(clientResponse->result == OC_STACK_OK)
416     {
417         OC_LOG(INFO, TAG, "OwnerTransferModeHandler : response result = OC_STACK_OK");
418         //Send request : GET /oic/sec/pstat
419         OCStackResult res = GetProvisioningStatusResource(otmCtx);
420         if(OC_STACK_OK != res)
421         {
422             OC_LOG(WARNING, TAG, "Failed to get pstat information");
423             SetResult(otmCtx, res);
424         }
425     }
426     else
427     {
428         OC_LOG_V(WARNING, TAG, "OwnerTransferModeHandler : Client response is incorrect : %d",
429         clientResponse->result);
430         SetResult(otmCtx, clientResponse->result);
431     }
432
433     OC_LOG(DEBUG, TAG, "OUT OwnerTransferModeHandler");
434
435 exit:
436     return  OC_STACK_DELETE_TRANSACTION;
437 }
438
439 /**
440  * Callback handler for ProvisioningStatusResouceHandler API.
441  *
442  * @param[in] ctx             ctx value passed to callback from calling function.
443  * @param[in] UNUSED          handle to an invocation
444  * @param[in] clientResponse  Response from queries to remote servers.
445  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
446  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
447  */
448 static OCStackApplicationResult ListMethodsHandler(void *ctx, OCDoHandle UNUSED,
449                                                     OCClientResponse *clientResponse)
450 {
451     OC_LOG(DEBUG, TAG, "IN ListMethodsHandler");
452
453     VERIFY_NON_NULL(TAG, clientResponse, WARNING);
454     VERIFY_NON_NULL(TAG, ctx, WARNING);
455
456     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
457     (void)UNUSED;
458     if  (OC_STACK_OK == clientResponse->result)
459     {
460         if  (NULL == clientResponse->payload)
461         {
462             OC_LOG(INFO, TAG, "Skiping Null payload");
463             SetResult(otmCtx, OC_STACK_ERROR);
464             return OC_STACK_DELETE_TRANSACTION;
465         }
466
467         if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
468         {
469             OC_LOG(INFO, TAG, "Unknown payload type");
470             SetResult(otmCtx, OC_STACK_ERROR);
471             return OC_STACK_DELETE_TRANSACTION;
472         }
473
474         OicSecPstat_t* pstat = JSONToPstatBin(
475                 ((OCSecurityPayload*)clientResponse->payload)->securityData);
476         if(NULL == pstat)
477         {
478             OC_LOG(ERROR, TAG, "Error while converting json to pstat bin");
479             SetResult(otmCtx, OC_STACK_ERROR);
480             return OC_STACK_DELETE_TRANSACTION;
481         }
482         otmCtx->selectedDeviceInfo->pstat = pstat;
483
484         //Select operation mode (Currently supported SINGLE_SERVICE_CLIENT_DRIVEN only)
485         OicSecDpom_t selectedOperationMode;
486         SelectOperationMode(otmCtx->selectedDeviceInfo, &selectedOperationMode);
487
488         //Send request : PUT /oic/sec/pstat [{"OM":"0x11", .. }]
489         OCStackResult res = PutUpdateOperationMode(otmCtx, selectedOperationMode);
490         if (OC_STACK_OK != res)
491         {
492             OC_LOG(ERROR, TAG, "Error while updating operation mode.");
493             SetResult(otmCtx, res);
494         }
495     }
496     else
497     {
498         OC_LOG_V(WARNING, TAG, "ListMethodsHandler : Client response is incorrect : %d",
499             clientResponse->result);
500         SetResult(otmCtx, clientResponse->result);
501     }
502
503     OC_LOG(DEBUG, TAG, "OUT ListMethodsHandler");
504 exit:
505     return  OC_STACK_DELETE_TRANSACTION;
506 }
507
508 /**
509  * Callback handler for OwnershipInformationHandler API.
510  *
511  * @param[in] ctx             ctx value passed to callback from calling function.
512  * @param[in] UNUSED          handle to an invocation
513  * @param[in] clientResponse  Response from queries to remote servers.
514  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
515  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
516  */
517 static OCStackApplicationResult OwnershipInformationHandler(void *ctx, OCDoHandle UNUSED,
518                                 OCClientResponse *clientResponse)
519 {
520     VERIFY_NON_NULL(TAG, clientResponse, WARNING);
521     VERIFY_NON_NULL(TAG, ctx, WARNING);
522
523     OC_LOG(DEBUG, TAG, "IN OwnershipInformationHandler");
524     (void)UNUSED;
525     OCStackResult res = OC_STACK_OK;
526     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
527     if  (OC_STACK_OK == clientResponse->result)
528     {
529         if(OIC_RANDOM_DEVICE_PIN == otmCtx->selectedDeviceInfo->doxm->oxmSel)
530         {
531             res = RemoveCredential(&otmCtx->tempCredId);
532             if(OC_STACK_RESOURCE_DELETED != res)
533             {
534                 OC_LOG_V(ERROR, TAG, "Failed to remove temporal PSK : %d", res);
535                 return OC_STACK_DELETE_TRANSACTION;
536             }
537         }
538
539         res = SaveOwnerPSK(otmCtx->selectedDeviceInfo);
540         if(OC_STACK_OK != res)
541         {
542             OC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to owner PSK generation");
543             SetResult(otmCtx, res);
544             return OC_STACK_DELETE_TRANSACTION;
545         }
546
547         CAEndpoint_t* endpoint = (CAEndpoint_t *)&otmCtx->selectedDeviceInfo->endpoint;
548         endpoint->port = otmCtx->selectedDeviceInfo->securePort;
549         CAResult_t closeRes = CACloseDtlsSession(endpoint);
550         if(CA_STATUS_OK != closeRes)
551         {
552             OC_LOG(ERROR, TAG, "Failed to close DTLS session");
553             SetResult(otmCtx, closeRes);
554             return OC_STACK_DELETE_TRANSACTION;
555         }
556
557         OC_LOG(INFO, TAG, "Ownership transfer was successfully completed.");
558         OC_LOG(INFO, TAG, "Start defualt ACL & commit-hash provisioning.");
559
560         res = FinalizeProvisioning(otmCtx);
561         if(OC_STACK_OK != res)
562         {
563             SetResult(otmCtx, res);
564         }
565     }
566     else
567     {
568         res = clientResponse->result;
569     }
570
571     OC_LOG(DEBUG, TAG, "OUT OwnershipInformationHandler");
572
573 exit:
574     return  OC_STACK_DELETE_TRANSACTION;
575 }
576
577 /**
578  * Response handler for update operation mode.
579  *
580  * @param[in] ctx             ctx value passed to callback from calling function.
581  * @param[in] UNUSED          handle to an invocation
582  * @param[in] clientResponse  Response from queries to remote servers.
583  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
584  *          and  OC_STACK_KEEP_TRANSACTION to keep it.
585  */
586 static OCStackApplicationResult OperationModeUpdateHandler(void *ctx, OCDoHandle UNUSED,
587                                 OCClientResponse *clientResponse)
588 {
589     OC_LOG(DEBUG, TAG, "IN OperationModeUpdateHandler");
590
591     VERIFY_NON_NULL(TAG, clientResponse, WARNING);
592     VERIFY_NON_NULL(TAG, ctx, WARNING);
593
594     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
595     (void) UNUSED;
596     if  (OC_STACK_OK == clientResponse->result)
597     {
598         OCStackResult res = OC_STACK_ERROR;
599         OicSecOxm_t selOxm = otmCtx->selectedDeviceInfo->doxm->oxmSel;
600         //DTLS Handshake
601         //Load secret for temporal secure session.
602         if(g_OTMDatas[selOxm].loadSecretCB)
603         {
604             res = g_OTMDatas[selOxm].loadSecretCB(otmCtx);
605             if(OC_STACK_OK != res)
606             {
607                 OC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to load secret");
608                 SetResult(otmCtx, res);
609                 return  OC_STACK_DELETE_TRANSACTION;
610             }
611         }
612
613         //Try DTLS handshake to generate secure session
614         if(g_OTMDatas[selOxm].createSecureSessionCB)
615         {
616             res = g_OTMDatas[selOxm].createSecureSessionCB(otmCtx);
617             if(OC_STACK_OK != res)
618             {
619                 OC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to create DTLS session");
620                 SetResult(otmCtx, res);
621                 return OC_STACK_DELETE_TRANSACTION;
622             }
623         }
624
625         //Send request : PUT /oic/sec/doxm [{"Owned":"True", .. , "Owner":"PT's UUID"}]
626         res = PutOwnershipInformation(otmCtx);
627         if(OC_STACK_OK != res)
628         {
629             OC_LOG(ERROR, TAG, "OperationModeUpdate : Failed to send owner information");
630             SetResult(otmCtx, res);
631         }
632     }
633     else
634     {
635         OC_LOG(ERROR, TAG, "Error while update operation mode");
636         SetResult(otmCtx, clientResponse->result);
637     }
638
639     OC_LOG(DEBUG, TAG, "OUT OperationModeUpdateHandler");
640
641 exit:
642     return  OC_STACK_DELETE_TRANSACTION;
643 }
644
645
646 static OCStackResult PutOwnerTransferModeToResource(OTMContext_t* otmCtx)
647 {
648     OC_LOG(DEBUG, TAG, "IN PutOwnerTransferModeToResource");
649
650     if(!otmCtx || !otmCtx->selectedDeviceInfo)
651     {
652         OC_LOG(ERROR, TAG, "Invailed parameters");
653         return OC_STACK_INVALID_PARAM;
654     }
655
656     OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
657     OicSecOxm_t selectedOxm = deviceInfo->doxm->oxmSel;
658     char query[MAX_QUERY_LENGTH] = {};
659     sprintf(query, "%s%s:%d%s", COAP_PREFIX,
660                                 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
661                                 OIC_RSRC_DOXM_URI);
662     OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
663     if(!secPayload)
664     {
665         OC_LOG(ERROR, TAG, "Failed to memory allocation");
666         return OC_STACK_NO_MEMORY;
667     }
668     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
669     secPayload->securityData = g_OTMDatas[selectedOxm].createSelectOxmPayloadCB(otmCtx);
670     if (NULL == secPayload->securityData)
671     {
672         OICFree(secPayload);
673         OC_LOG(ERROR, TAG, "Error while converting bin to json");
674         return OC_STACK_ERROR;
675     }
676     OC_LOG_V(DEBUG, TAG, "Payload : %s", secPayload->securityData);
677
678     OCCallbackData cbData;
679     cbData.cb = &OwnerTransferModeHandler;
680     cbData.context = (void *)otmCtx;
681     cbData.cd = NULL;
682
683     // TODO: 6th argument need to be changed, if we have to use CT_FLAG_SECURE
684     OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query,
685                                      &deviceInfo->endpoint, (OCPayload*)secPayload,
686                                      CT_ADAPTER_IP, OC_LOW_QOS, &cbData, NULL, 0);
687     if (res != OC_STACK_OK)
688     {
689         OC_LOG(ERROR, TAG, "OCStack resource error");
690     }
691
692     OC_LOG(DEBUG, TAG, "OUT PutOwnerTransferModeToResource");
693
694     return res;
695 }
696
697 static OCStackResult GetProvisioningStatusResource(OTMContext_t* otmCtx)
698 {
699     OC_LOG(DEBUG, TAG, "IN GetProvisioningStatusResource");
700
701     if(!otmCtx || !otmCtx->selectedDeviceInfo)
702     {
703         OC_LOG(ERROR, TAG, "Invailed parameters");
704         return OC_STACK_INVALID_PARAM;
705     }
706
707     OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
708     char query[MAX_QUERY_LENGTH] = {};
709     sprintf(query, "%s%s:%d%s", COAP_PREFIX,
710                                 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
711                                 OIC_RSRC_PSTAT_URI);
712     OCCallbackData cbData;
713     cbData.cb = &ListMethodsHandler;
714     cbData.context = (void *)otmCtx;
715     cbData.cd = NULL;
716
717     // TODO: 6th argument need to be changed, if we have to use CT_FLAG_SECURE
718     OCStackResult res = OCDoResource(NULL, OC_REST_GET, query, NULL, NULL,
719                                      CT_ADAPTER_IP, OC_LOW_QOS, &cbData, NULL, 0);
720     if (res != OC_STACK_OK)
721     {
722         OC_LOG(ERROR, TAG, "OCStack resource error");
723     }
724
725     OC_LOG(DEBUG, TAG, "OUT GetProvisioningStatusResource");
726
727     return res;
728 }
729
730
731 static OCStackResult PutOwnershipInformation(OTMContext_t* otmCtx)
732 {
733     OC_LOG(DEBUG, TAG, "IN PutOwnershipInformation");
734
735     if(!otmCtx || !otmCtx->selectedDeviceInfo)
736     {
737         OC_LOG(ERROR, TAG, "Invailed parameters");
738         return OC_STACK_INVALID_PARAM;
739     }
740
741     OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
742     char query[MAX_QUERY_LENGTH] = {};
743     sprintf(query, "%s%s:%d%s", COAPS_PREFIX,
744                                 deviceInfo->endpoint.addr, deviceInfo->securePort,
745                                 OIC_RSRC_DOXM_URI);
746     //OwnershipInformationHandler
747     OicSecOxm_t selOxm = deviceInfo->doxm->oxmSel;
748     OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
749     if(!secPayload)
750     {
751         OC_LOG(ERROR, TAG, "Failed to memory allocation");
752         return OC_STACK_NO_MEMORY;
753     }
754     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
755     secPayload->securityData = g_OTMDatas[selOxm].createOwnerTransferPayloadCB(otmCtx);
756     if (NULL == secPayload->securityData)
757     {
758         OICFree(secPayload);
759         OC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
760         return OC_STACK_INVALID_PARAM;
761     }
762
763     OCCallbackData cbData;
764     cbData.cb = &OwnershipInformationHandler;
765     cbData.context = (void *)otmCtx;
766     cbData.cd = NULL;
767     // TODO: 6th argument need to be changed, if we have to use CT_FLAG_SECURE
768     OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
769                                      CT_ADAPTER_IP, OC_LOW_QOS, &cbData, NULL, 0);
770     if (res != OC_STACK_OK)
771     {
772         OC_LOG(ERROR, TAG, "OCStack resource error");
773     }
774
775     OC_LOG(DEBUG, TAG, "OUT PutOwnershipInformation");
776
777     return res;
778 }
779
780 static OCStackResult PutUpdateOperationMode(OTMContext_t* otmCtx,
781                                     OicSecDpom_t selectedOperationMode)
782 {
783     OC_LOG(DEBUG, TAG, "IN PutUpdateOperationMode");
784
785     if(!otmCtx || !otmCtx->selectedDeviceInfo)
786     {
787         return OC_STACK_INVALID_PARAM;
788     }
789
790     OCProvisionDev_t* deviceInfo = otmCtx->selectedDeviceInfo;
791     char query[MAX_QUERY_LENGTH] = {};
792     sprintf(query, "%s%s:%d%s", COAP_PREFIX,
793                                 deviceInfo->endpoint.addr, deviceInfo->endpoint.port,
794                                 OIC_RSRC_PSTAT_URI);
795     deviceInfo->pstat->om = selectedOperationMode;
796
797     OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
798     if(!secPayload)
799     {
800         OC_LOG(ERROR, TAG, "Failed to memory allocation");
801         return OC_STACK_NO_MEMORY;
802     }
803     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
804     secPayload->securityData = BinToPstatJSON(deviceInfo->pstat);
805     if (NULL == secPayload->securityData)
806     {
807         OICFree(secPayload);
808         OC_LOG(ERROR, TAG, "Error while converting pstat bin to json");
809         return OC_STACK_INVALID_PARAM;
810     }
811
812     OCCallbackData cbData;
813     cbData.cb = &OperationModeUpdateHandler;
814     cbData.context = (void *)otmCtx;
815     cbData.cd = NULL;
816
817     OCStackResult res = OCDoResource(NULL, OC_REST_PUT, query, 0, (OCPayload*)secPayload,
818                                      CT_ADAPTER_IP, OC_LOW_QOS, &cbData, NULL, 0);
819     if (res != OC_STACK_OK)
820     {
821         OC_LOG(ERROR, TAG, "OCStack resource error");
822     }
823
824     OC_LOG(DEBUG, TAG, "OUT PutUpdateOperationMode");
825
826     return res;
827 }
828
829 static OCStackResult StartOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice)
830 {
831     OC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
832     OTMContext_t* otmCtx = (OTMContext_t*)OICMalloc(sizeof(OTMContext_t));
833     if(!otmCtx)
834     {
835         OC_LOG(ERROR, TAG, "Failed to create OTM Context");
836         return OC_STACK_NO_MEMORY;
837     }
838     otmCtx->userCtx = ctx;
839     otmCtx->selectedDeviceInfo = selectedDevice;
840
841     //Set to the lowest level OxM, and then find more higher level OxM.
842     OCStackResult res = SelectProvisioningMethod(selectedDevice->doxm->oxm,
843                                                  selectedDevice->doxm->oxmLen,
844                                                  &selectedDevice->doxm->oxmSel);
845     if(OC_STACK_OK != res)
846     {
847         OC_LOG(ERROR, TAG, "Failed to select the provisioning method");
848         SetResult(otmCtx, res);
849         return res;
850     }
851     OC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
852
853     //Send Req: PUT /oic/sec/doxm [{..."OxmSel" :g_OTMDatas[Index of Selected OxM].OXMString,...}]
854     res = PutOwnerTransferModeToResource(otmCtx);
855     if(OC_STACK_OK != res)
856     {
857         OC_LOG(WARNING, TAG, "Failed to select the provisioning method");
858         SetResult(otmCtx, res);
859         return res;
860     }
861
862     OC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
863
864     return res;
865
866 }
867
868 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
869 {
870     OC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
871
872     if(!data)
873     {
874         OC_LOG(ERROR, TAG, "OTMSetOwnershipTransferCallbackData : Invalid parameters");
875         return OC_STACK_INVALID_PARAM;
876     }
877     if(oxmType >= OIC_OXM_COUNT)
878     {
879         OC_LOG(INFO, TAG, "Unknow ownership transfer method");
880         return OC_STACK_INVALID_PARAM;
881     }
882
883     g_OTMDatas[oxmType].loadSecretCB= data->loadSecretCB;
884     g_OTMDatas[oxmType].createSecureSessionCB = data->createSecureSessionCB;
885     g_OTMDatas[oxmType].createSelectOxmPayloadCB = data->createSelectOxmPayloadCB;
886     g_OTMDatas[oxmType].createOwnerTransferPayloadCB = data->createOwnerTransferPayloadCB;
887
888     OC_LOG(DEBUG, TAG, "OUT OTMSetOwnerTransferCallbackData");
889
890     return OC_STACK_OK;
891 }
892
893 /**
894  * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
895  */
896 OCStackResult OTMDoOwnershipTransfer(void* ctx,
897                                      OCProvisionDev_t *selectedDevicelist,
898                                      OCProvisionResultCB resultCallback)
899 {
900     OC_LOG(DEBUG, TAG, "IN OTMDoOwnershipTransfer");
901
902     if (NULL == selectedDevicelist || NULL == resultCallback )
903     {
904         return OC_STACK_INVALID_PARAM;
905     }
906
907     g_resultCallback = resultCallback;
908     g_hasError = false;
909
910     OCProvisionDev_t* pCurDev = selectedDevicelist;
911
912     //Counting number of selected devices.
913     g_resultArraySize = 0;
914     while(NULL != pCurDev)
915     {
916         g_resultArraySize++;
917         pCurDev = pCurDev->next;
918     }
919
920     if(g_resultArray)
921     {
922         OICFree(g_resultArray);
923     }
924     g_resultArray =
925         (OCProvisionResult_t*)OICMalloc(sizeof(OCProvisionResult_t) * g_resultArraySize);
926     if(NULL == g_resultArray)
927     {
928         OC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
929         return OC_STACK_NO_MEMORY;
930     }
931
932     pCurDev = selectedDevicelist;
933     //Fill the device UUID for result array.
934     for(size_t devIdx = 0; devIdx < g_resultArraySize; devIdx++)
935     {
936         memcpy(g_resultArray[devIdx].deviceId.id,
937                pCurDev->doxm->deviceID.id,
938                UUID_LENGTH);
939         g_resultArray[devIdx].res = OC_STACK_CONTINUE;
940         pCurDev = pCurDev->next;
941     }
942
943     StartOwnershipTransfer(ctx, selectedDevicelist);
944
945     OC_LOG(DEBUG, TAG, "OUT OTMDoOwnershipTransfer");
946
947     return (g_hasError ? OC_STACK_ERROR : OC_STACK_OK);
948 }
949
950 /**
951  * Callback handler of SRPFinalizeProvisioning.
952  *
953  * @param[in] ctx             ctx value passed to callback from calling function.
954  * @param[in] UNUSED          handle to an invocation
955  * @param[in] clientResponse  Response from queries to remote servers.
956  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
957  *          and OC_STACK_KEEP_TRANSACTION to keep it.
958  */
959 static OCStackApplicationResult FinalizeProvisioningCB(void *ctx, OCDoHandle UNUSED,
960                                                        OCClientResponse *clientResponse)
961 {
962     OC_LOG_V(INFO, TAG, "IN FinalizeProvisioningCB.");
963
964     VERIFY_NON_NULL(TAG, clientResponse, ERROR);
965     VERIFY_NON_NULL(TAG, ctx, ERROR);
966
967     OTMContext_t* otmCtx = (OTMContext_t*)ctx;
968     (void)UNUSED;
969     if(OC_STACK_OK == clientResponse->result)
970     {
971         SetResult(otmCtx, OC_STACK_OK);
972     }
973 exit:
974     return OC_STACK_DELETE_TRANSACTION;
975 }
976
977 /**
978  * Callback handler of default ACL provisioning.
979  *
980  * @param[in] ctx             ctx value passed to callback from calling function.
981  * @param[in] UNUSED          handle to an invocation
982  * @param[in] clientResponse  Response from queries to remote servers.
983  * @return  OC_STACK_DELETE_TRANSACTION to delete the transaction
984  *          and OC_STACK_KEEP_TRANSACTION to keep it.
985  */
986 static OCStackApplicationResult ProvisionDefaultACLCB(void *ctx, OCDoHandle UNUSED,
987                                                        OCClientResponse *clientResponse)
988 {
989     OC_LOG_V(INFO, TAG, "IN ProvisionDefaultACLCB.");
990
991     VERIFY_NON_NULL(TAG, clientResponse, ERROR);
992     VERIFY_NON_NULL(TAG, ctx, ERROR);
993
994     OTMContext_t* otmCtx = (OTMContext_t*) ctx;
995     (void)UNUSED;
996
997     if (OC_STACK_RESOURCE_CREATED == clientResponse->result)
998     {
999         OC_LOG_V(INFO, TAG, "Staring commit hash task.");
1000         // TODO hash currently have fixed value 0.
1001         uint16_t aclHash = 0;
1002         otmCtx->selectedDeviceInfo->pstat->commitHash = aclHash;
1003         otmCtx->selectedDeviceInfo->pstat->tm = NORMAL;
1004         OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1005         if(!secPayload)
1006         {
1007             OC_LOG(ERROR, TAG, "Failed to memory allocation");
1008             return OC_STACK_NO_MEMORY;
1009         }
1010         secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1011         secPayload->securityData = BinToPstatJSON(otmCtx->selectedDeviceInfo->pstat);
1012         if (NULL == secPayload->securityData)
1013         {
1014             OICFree(secPayload);
1015             SetResult(otmCtx, OC_STACK_INVALID_JSON);
1016             return OC_STACK_DELETE_TRANSACTION;
1017         }
1018         OC_LOG_V(INFO, TAG, "Created payload for commit hash: %s",secPayload->securityData);
1019
1020         char uri[MAX_QUERY_LENGTH] = { 0 };
1021         size_t uriLen = sizeof(uri);
1022
1023         snprintf(uri, uriLen - 1, COAPS_QUERY, otmCtx->selectedDeviceInfo->endpoint.addr,
1024                 otmCtx->selectedDeviceInfo->securePort, OIC_RSRC_PSTAT_URI);
1025         uri[uriLen - 1] = '\0';
1026         OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1027         cbData.cb = &FinalizeProvisioningCB;
1028         cbData.context = (void*)otmCtx; // forward context to SRPFinalizeProvisioningCB
1029         cbData.cd = NULL;
1030
1031         // TODO change value of CT_ADAPTER_IP with val from discovery
1032         OCStackResult ret = OCDoResource(NULL, OC_REST_PUT, uri, 0, (OCPayload*)secPayload,
1033                 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1034         OC_LOG_V(INFO, TAG, "OCDoResource returned: %d",ret);
1035         if (ret != OC_STACK_OK)
1036         {
1037             OC_LOG(ERROR, TAG, "OCStack resource error");
1038             SetResult(otmCtx, ret);
1039         }
1040     }
1041     else
1042     {
1043         OC_LOG_V(INFO, TAG, "Error occured in provisionDefaultACLCB :: %d\n",
1044                             clientResponse->result);
1045         SetResult(otmCtx, clientResponse->result);
1046     }
1047 exit:
1048     return OC_STACK_DELETE_TRANSACTION;
1049 }
1050
1051
1052 OCStackResult FinalizeProvisioning(OTMContext_t* otmCtx)
1053 {
1054     OC_LOG(INFO, TAG, "IN FinalizeProvisioning");
1055
1056     if(!otmCtx)
1057     {
1058         OC_LOG(ERROR, TAG, "OTMContext is NULL");
1059         return OC_STACK_INVALID_PARAM;
1060     }
1061     if(!otmCtx->selectedDeviceInfo)
1062     {
1063         OC_LOG(ERROR, TAG, "Can't find device information in OTMContext");
1064         OICFree(otmCtx);
1065         return OC_STACK_INVALID_PARAM;
1066     }
1067     // Provision Default ACL to device
1068     OicSecAcl_t defaultAcl =
1069     { {.id={0}},
1070         1,
1071         NULL,
1072         0x001F,
1073         0,
1074         NULL,
1075         NULL,
1076         1,
1077         NULL,
1078         NULL,
1079     };
1080
1081     OicUuid_t provTooldeviceID = {.id={0}};
1082     if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1083     {
1084         OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1085         SetResult(otmCtx, OC_STACK_ERROR);
1086         return OC_STACK_ERROR;
1087     }
1088     OC_LOG(INFO, TAG, "Retieved deviceID");
1089     memcpy(defaultAcl.subject.id, provTooldeviceID.id, sizeof(defaultAcl.subject.id));
1090     char *wildCardResource = "*";
1091     defaultAcl.resources = &wildCardResource;
1092
1093     defaultAcl.owners = (OicUuid_t *) OICCalloc(1, UUID_LENGTH);
1094     if(!defaultAcl.owners)
1095     {
1096         OC_LOG(ERROR, TAG, "Failed to memory allocation for default ACL");
1097         SetResult(otmCtx, OC_STACK_NO_MEMORY);
1098         return OC_STACK_NO_MEMORY;
1099     }
1100     memcpy(defaultAcl.owners->id, provTooldeviceID.id, UUID_LENGTH);
1101     OC_LOG(INFO, TAG, "Provisioning default ACL");
1102
1103     OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1104     if(!secPayload)
1105     {
1106         OC_LOG(ERROR, TAG, "Failed to memory allocation");
1107         return OC_STACK_NO_MEMORY;
1108     }
1109     secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1110     secPayload->securityData = BinToAclJSON(&defaultAcl);
1111     OICFree(defaultAcl.owners);
1112     if(!secPayload->securityData)
1113     {
1114         OICFree(secPayload);
1115         OC_LOG(INFO, TAG, "FinalizeProvisioning : Failed to BinToAclJSON");
1116         SetResult(otmCtx, OC_STACK_ERROR);
1117         return OC_STACK_ERROR;
1118     }
1119     OC_LOG_V(INFO, TAG, "Provisioning default ACL : %s",secPayload->securityData);
1120
1121     char uri[MAX_QUERY_LENGTH] = { 0 };
1122
1123     size_t uriLen = sizeof(uri);
1124     snprintf(uri, uriLen - 1, COAPS_QUERY, otmCtx->selectedDeviceInfo->endpoint.addr,
1125             otmCtx->selectedDeviceInfo->securePort, OIC_RSRC_ACL_URI);
1126     uri[uriLen - 1] = '\0';
1127     OC_LOG_V(INFO, TAG, "Request URI for Provisioning default ACL : %s",uri);
1128
1129     OCCallbackData cbData =  {.context=NULL, .cb=NULL, .cd=NULL};
1130     cbData.cb = &ProvisionDefaultACLCB;
1131     cbData.context = (void *)otmCtx;
1132     cbData.cd = NULL;
1133
1134     OCStackResult ret = OCDoResource(NULL, OC_REST_POST, uri,
1135             &otmCtx->selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1136             CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1137     if (OC_STACK_OK != ret)
1138     {
1139         SetResult(otmCtx, ret);
1140         return ret;
1141     }
1142
1143     OC_LOG(INFO, TAG, "OUT FinalizeProvisioning");
1144
1145     return ret;
1146
1147 }
1148