Master branch merge for VS build success
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / src / EnrolleeSecurity.cpp
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 #include "base64.h"
22
23 #include "EnrolleeSecurity.h"
24 #include "oxmjustworks.h"
25 #include "oxmrandompin.h"
26 #include "EnrolleeResource.h"
27 #include "logger.h"
28 #include "ESException.h"
29 #include "oic_malloc.h"
30 #include "oic_string.h"
31
32 namespace OIC
33 {
34     namespace Service
35     {
36         #define MAX_PERMISSION_LENGTH (5)
37         #define CREATE (1)
38         #define READ (2)
39         #define UPDATE (4)
40         #define DELETE (8)
41         #define NOTIFY (16)
42         #define DASH '-'
43
44         //TODO : Currently discovery timeout for owned and unowned devices is fixed as 5
45         // The value should be accepted from the application as a parameter during ocplatform
46         // config call
47 #define ES_SEC_DISCOVERY_TIMEOUT 5
48
49         EnrolleeSecurity::EnrolleeSecurity(
50         std::shared_ptr< EnrolleeResource > EnrolleeResource,
51         std::string secDbPath)
52         {
53             m_enrolleeSecState = EnrolleeSecState::ES_SEC_UNKNOWN;
54             m_EnrolleeResource = EnrolleeResource;
55         }
56
57         void EnrolleeSecurity::registerCallbackHandler(SecurityProvStatusCb securityProvStatusCb,
58                 SecurityPinCb securityPinCb, SecProvisioningDbPathCb secProvisioningDbPathCb)
59         {
60             m_securityProvStatusCb = securityProvStatusCb;
61             m_securityPinCb = securityPinCb;
62             m_secProvisioningDbPathCb = secProvisioningDbPathCb;
63         }
64
65         std::shared_ptr< OC::OCSecureResource > EnrolleeSecurity::getEnrollee(DeviceList_t &list)
66         {
67             for (unsigned int i = 0; i < list.size(); i++)
68             {
69                 if(m_deviceId == list[i]->getDeviceID().c_str())
70                 {
71                     OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Device %d ID %s ", i + 1,
72                             list[i]->getDeviceID().c_str());
73                     OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "From IP :%s", list[i]->getDevAddr().c_str());
74                     return list[i];
75                 }
76                 //Always return the first element of the unOwned devices. This is considering that Mediator is
77                 // always connected with only one Enrollee for which ownership transfer is being performed.
78                 // Incase of multiple Enrollee devices connected to the Mediator via any OnBoarding method (SoftAp
79                 // for example), the Enrollee devices will be provisioned in the first come first serve basis in the order
80                 // returned by the security layer.
81
82             }
83             OIC_LOG(ERROR, ENROLEE_SECURITY_TAG,"Error!!! DeviceList_t is NULL");
84             return NULL;
85         }
86
87         void EnrolleeSecurity::convertUUIDToString(OicUuid_t uuid, std::string& uuidString)
88         {
89             char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*) 0)->id)) + 1] =
90             { 0, };
91             uint32_t outLen = 0;
92             B64Result b64Ret = B64_OK;
93             std::ostringstream deviceId("");
94
95             b64Ret = b64Encode(uuid.id, sizeof(uuid.id),
96                     base64Buff, sizeof(base64Buff), &outLen);
97
98             if (B64_OK == b64Ret)
99             {
100                 deviceId << base64Buff;
101             }
102             uuidString =  deviceId.str();
103         }
104
105         void EnrolleeSecurity::ownershipTransferCb(OC::PMResultList_t *result, int hasError)
106         {
107             if (hasError)
108             {
109                 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG,"Error!!! in OwnershipTransfer");
110
111                 std::string uuid;
112                 convertUUIDToString(result->at(0).deviceId, uuid);
113                 std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
114                         std::make_shared< SecProvisioningStatus >(uuid, ES_ERROR);
115                 m_securityProvStatusCb(securityProvisioningStatus);
116                 return;
117             }
118             else
119             {
120                 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "ownershipTransferCb : Received provisioning results: ");
121                 for (unsigned int i = 0; i < result->size(); i++)
122                 {
123                     OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Result is = %d for device",result->at(i).res);
124                     std::string uuid;
125                     convertUUIDToString(result->at(0).deviceId, uuid);
126
127                     OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "UUID : %s",uuid.c_str());
128                     std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
129                             std::make_shared< SecProvisioningStatus >(uuid, ES_OK);
130                     m_securityProvStatusCb(securityProvisioningStatus);
131                     return;
132                 }
133
134                 delete result;
135             }
136         }
137
138         void EnrolleeSecurity::setTargetDevID(const std::string devID)
139         {
140             m_deviceId = devID;
141         }
142
143         void EnrolleeSecurity::performOwnershipTransfer()
144         {
145             OC::DeviceList_t pUnownedDevList, pOwnedDevList;
146
147             pOwnedDevList.clear();
148             pUnownedDevList.clear();
149
150             OCStackResult result;
151             /*
152             result = OCSecure::discoverOwnedDevices(ES_SEC_DISCOVERY_TIMEOUT,
153                     pOwnedDevList);
154             if (result != OC_STACK_OK)
155             {
156                 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Owned Discovery failed.");
157                 //Throw exception
158                 throw ESPlatformException(result);
159             }
160             else if (pOwnedDevList.size())
161             {
162                 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Found owned devices. Count =%d",
163                         pOwnedDevList.size());
164                 std::shared_ptr< OC::OCSecureResource > ownedDevice = getEnrollee(pOwnedDevList);
165
166                 if (ownedDevice)
167                 {
168                     std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
169                             std::make_shared< SecProvisioningStatus >(ownedDevice->getDeviceID(), ES_OK);
170                     m_securityProvStatusCb(securityProvisioningStatus);
171                     return;
172                 }
173             }
174             */
175             result = OCSecure::discoverUnownedDevices(ES_SEC_DISCOVERY_TIMEOUT, pUnownedDevList);
176             if (result != OC_STACK_OK)
177             {
178                 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "UnOwned Discovery failed.");
179                 //Throw exception
180                 throw ESPlatformException(result);
181             }
182             else if (pUnownedDevList.size())
183             {
184                 OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Found Unowned devices. Count =%d",
185                         pUnownedDevList.size());
186
187                 m_unownedDevice = getEnrollee(pUnownedDevList);
188                 if (m_unownedDevice)
189                 {
190                     OTMCallbackData_t justWorksCBData;
191                     justWorksCBData.loadSecretCB = LoadSecretJustWorksCallback;
192                     justWorksCBData.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
193                     justWorksCBData.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
194                     justWorksCBData.createOwnerTransferPayloadCB =
195                             CreateJustWorksOwnerTransferPayload;
196                     OCSecure::setOwnerTransferCallbackData(OIC_JUST_WORKS, &justWorksCBData, NULL);
197
198                     OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Transfering ownership for : %s ",
199                             m_unownedDevice->getDeviceID().c_str());
200
201                     OC::ResultCallBack ownershipTransferCb = std::bind(
202                             &EnrolleeSecurity::ownershipTransferCb, this, std::placeholders::_1,
203                             std::placeholders::_2);
204
205                     result = m_unownedDevice->doOwnershipTransfer(ownershipTransferCb);
206                     if (result != OC_STACK_OK)
207                     {
208                         OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "OwnershipTransferCallback is failed");
209                         throw ESPlatformException(result);
210                     }
211                 }
212             }
213             else
214             {
215                 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "No unOwned devices found.");
216                 throw ESException("No unOwned devices found.");
217             }
218         }
219     }
220 }