1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * *****************************************************************/
23 #include "ocprovisioningmanager.h"
24 #include "pmutility.h"
25 #include "ownershiptransfermanager.h"
26 #include "oic_malloc.h"
28 #include "secureresourceprovider.h"
29 #include "provisioningdatabasemanager.h"
30 #include "credresource.h"
34 typedef struct Linkdata Linkdata_t;
38 const OCProvisionDev_t *pDev1;
39 OicSecAcl_t *pDev1Acl;
40 const OCProvisionDev_t *pDev2;
41 OicSecAcl_t *pDev2Acl;
42 OCProvisionResult_t *resArr;
44 int currentCountResults;
45 OCProvisionResultCB resultCallback;
50 * The function is responsible for initializaton of the provisioning manager. It will load
51 * provisioning database which have owned device's list and their linked status.
52 * TODO: In addition, if there is a device(s) which has not up-to-date credentials, this function will
53 * automatically try to update the deivce(s).
55 * @param[in] dbPath file path of the sqlite3 db
57 * @return OC_STACK_OK in case of success and other value otherwise.
59 OCStackResult OCInitPM(const char* dbPath)
61 return PDMInit(dbPath);
65 * The function is responsible for discovery of device is current subnet. It will list
66 * all the device in subnet which are not yet owned. Please call OCInit with OC_CLIENT_SERVER as
69 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
70 * client before returning the list of devices.
71 * @param[out] ppList List of candidate devices to be provisioned
72 * @return OTM_SUCCESS in case of success and other value otherwise.
74 OCStackResult OCDiscoverUnownedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
76 if( ppList == NULL || *ppList != NULL)
78 return OC_STACK_INVALID_PARAM;
81 return PMDeviceDiscovery(timeout, false, ppList);
85 * The function is responsible for discovery of owned device is current subnet. It will list
86 * all the device in subnet which are owned by calling provisioning client.
88 * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
89 * client before returning the list of devices.
90 * @param[out] ppList List of device owned by provisioning tool.
91 * @return OTM_SUCCESS in case of success and other value otherwise.
93 OCStackResult OCDiscoverOwnedDevices(unsigned short timeout, OCProvisionDev_t **ppList)
95 if( ppList == NULL || *ppList != NULL)
97 return OC_STACK_INVALID_PARAM;
100 return PMDeviceDiscovery(timeout, true, ppList);
104 * API to register for particular OxM.
106 * @param[in] Ownership transfer method.
107 * @param[in] Implementation of callback functions for owership transfer.
108 * @return OC_STACK_OK in case of success and other value otherwise.
110 OCStackResult OCSetOwnerTransferCallbackData(OicSecOxm_t oxm, OTMCallbackData_t* callbackData)
112 if(NULL == callbackData)
114 return OC_STACK_INVALID_PARAM;
117 return OTMSetOwnershipTransferCallbackData(oxm, callbackData);
120 OCStackResult OCDoOwnershipTransfer(void* ctx,
121 OCProvisionDev_t *targetDevices,
122 OCProvisionResultCB resultCallback)
124 if( NULL == targetDevices )
126 return OC_STACK_INVALID_PARAM;
129 return OTMDoOwnershipTransfer(ctx, targetDevices, resultCallback);
133 * This function deletes memory allocated to linked list created by OCDiscover_XXX_Devices API.
135 * @param[in] pList Pointer to OCProvisionDev_t which should be deleted.
137 void OCDeleteDiscoveredDevices(OCProvisionDev_t *pList)
139 PMDeleteDeviceList(pList);
143 * this function sends ACL information to resource.
145 * @param[in] ctx Application context would be returned in result callback.
146 * @param[in] selectedDeviceInfo Selected target device.
147 * @param[in] acl ACL to provision.
148 * @param[in] resultCallback callback provided by API user, callback will be called when provisioning
149 request recieves a response from resource server.
150 * @return OC_STACK_OK in case of success and other value otherwise.
152 OCStackResult OCProvisionACL(void* ctx, const OCProvisionDev_t *selectedDeviceInfo, OicSecAcl_t *acl,
153 OCProvisionResultCB resultCallback)
155 return SRPProvisionACL(ctx, selectedDeviceInfo, acl, resultCallback);
159 * function to provision credential to devices.
161 * @param[in] ctx Application context would be returned in result callback.
162 * @param[in] type Type of credentials to be provisioned to the device.
163 * @param[in] pDev1 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
164 @param[in] pDev2 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
165 * @param[in] resultCallback callback provided by API user, callback will be called when
166 * provisioning request recieves a response from first resource server.
167 * @return OC_STACK_OK in case of success and other value otherwise.
169 OCStackResult OCProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
170 const OCProvisionDev_t *pDev1,
171 const OCProvisionDev_t *pDev2,
172 OCProvisionResultCB resultCallback)
174 return SRPProvisionCredentials(ctx, type, keySize,
175 pDev1, pDev2, resultCallback);
180 * Function to unlink devices.
181 * This function will remove the credential & relationship between the two devices.
183 * @param[in] ctx Application context would be returned in result callback
184 * @param[in] pTargetDev1 first device information to be unlinked.
185 * @param[in] pTargetDev2 second device information to be unlinked.
186 * @param[in] resultCallback callback provided by API user, callback will be called when
187 * device unlink is finished.
188 * @return OC_STACK_OK in case of success and other value otherwise.
190 OCStackResult OCUnlinkDevices(void* ctx,
191 const OCProvisionDev_t* pTargetDev1,
192 const OCProvisionDev_t* pTargetDev2,
193 OCProvisionResultCB resultCallback)
195 OC_LOG(INFO, TAG, "IN OCUnlinkDevices");
196 OCUuidList_t* idList = NULL;
199 if (!pTargetDev1 || !pTargetDev2 || !resultCallback)
201 OC_LOG(ERROR, TAG, "OCUnlinkDevices : NULL parameters");
202 return OC_STACK_INVALID_PARAM;
205 // Get linked devices with the first device.
206 OCStackResult res = PDMGetLinkedDevices(&(pTargetDev1->doxm->deviceID), &idList, &numOfDev);
207 if (OC_STACK_OK != res)
209 OC_LOG(ERROR, TAG, "OCUnlinkDevices : PDMgetOwnedDevices failed");
214 OC_LOG(DEBUG, TAG, "OCUnlinkDevices : Can not find linked devices");
215 res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
219 // Check the linked devices contains the second device. If yes send credential DELETE request.
220 OCUuidList_t* curDev = idList;
221 while (NULL != curDev)
223 if (memcmp(pTargetDev2->doxm->deviceID.id, curDev->dev.id, sizeof(curDev->dev.id)) == 0)
225 res = SRPUnlinkDevices(ctx, pTargetDev1, pTargetDev2, resultCallback);
226 if (OC_STACK_OK != res)
228 OC_LOG(ERROR, TAG, "OCUnlinkDevices : Failed to unlink devices.");
232 curDev = curDev->next;
234 OC_LOG(DEBUG, TAG, "No matched pair found from provisioning database");
235 res = OC_STACK_INVALID_PARAM; // Input devices are not linked, No request is made
238 OC_LOG(INFO, TAG, "OUT OCUnlinkDevices");
240 PDMDestoryOicUuidLinkList(idList);
245 * Function to device revocation
246 * This function will remove credential of target device from all devices in subnet.
248 * @param[in] ctx Application context would be returned in result callback
249 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
250 * @param[in] pTargetDev Device information to be revoked.
251 * @param[in] resultCallback callback provided by API user, callback will be called when
252 * credential revocation is finished.
253 * @return OC_STACK_OK in case of success and other value otherwise.
255 OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
256 const OCProvisionDev_t* pTargetDev,
257 OCProvisionResultCB resultCallback)
259 OC_LOG(INFO, TAG, "IN OCRemoveDevice");
260 OCStackResult res = OC_STACK_ERROR;
261 if (!pTargetDev || !resultCallback || 0 == waitTimeForOwnedDeviceDiscovery)
263 OC_LOG(INFO, TAG, "OCRemoveDevice : Invalied parameters");
264 return OC_STACK_INVALID_PARAM;
267 // Send DELETE requests to linked devices
268 OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not.
269 resReq = SRPRemoveDevice(ctx, waitTimeForOwnedDeviceDiscovery, pTargetDev, resultCallback);
270 if (OC_STACK_OK != resReq)
272 if (OC_STACK_CONTINUE == resReq)
274 OC_LOG(DEBUG, TAG, "OCRemoveDevice : Revoked device has no linked device except PT.");
278 OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to invoke SRPRemoveDevice");
284 // Remove credential of revoked device from SVR database
285 const OicSecCred_t *cred = NULL;
286 cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
289 OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to get credential of remove device.");
293 res = RemoveCredential(&cred->subject);
294 if (res != OC_STACK_RESOURCE_DELETED)
296 OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to remove credential.");
300 // Remove device info from prvisioning database.
301 res = PDMDeleteDevice(&pTargetDev->doxm->deviceID);
302 if (res != OC_STACK_OK)
304 OC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to delete device in PDM.");
308 // Check that we have to wait callback for DELETE request or not
312 OC_LOG(INFO, TAG, "OUT OCRemoveDevice");
318 * Internal Function to update result in link result array.
320 static void UpdateLinkResults(Linkdata_t *link, int device, OCStackResult stackresult)
323 OC_LOG_V(INFO,TAG,"value of link->currentCountResults is %d",link->currentCountResults);
326 memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev1->doxm->deviceID.id,UUID_LENGTH);
330 memcpy(link->resArr[(link->currentCountResults)].deviceId.id, link->pDev2->doxm->deviceID.id,UUID_LENGTH);
332 link->resArr[(link->currentCountResults)].res = stackresult;
333 ++(link->currentCountResults);
338 * Callback to handle ACL provisioning for device 2.
340 static void AclProv2CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
345 OC_LOG(ERROR,TAG,"Context is Null in ACLProv 2");
349 Linkdata_t *link = (Linkdata_t*)ctx;
350 OCProvisionResultCB resultCallback = link->resultCallback;
355 UpdateLinkResults(link, 2,arr[0].res);
356 OC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
357 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
360 OICFree(link->resArr);
364 UpdateLinkResults(link, 2, arr[0].res);
365 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
368 OICFree(link->resArr);
374 * Callback to handle ACL provisioning for device 1
376 static void AclProv1CB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
381 OC_LOG(ERROR,TAG,"Context is Null in ACLProv1");
385 Linkdata_t *link = (Linkdata_t*)ctx;
386 OCProvisionResultCB resultCallback = link->resultCallback;
390 OC_LOG(ERROR,TAG,"Error occured while ACL provisioning device 1");
391 UpdateLinkResults(link, 1, arr[0].res);
392 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
395 OICFree(link->resArr);
399 UpdateLinkResults(link, 1, arr[0].res);
400 if (NULL != link->pDev2Acl)
402 OCStackResult res = SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
403 if (OC_STACK_OK!=res)
405 UpdateLinkResults(link, 2, res);
406 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
414 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
417 OICFree(link->resArr);
425 * Callback to handle credential provisioning.
427 static void ProvisionCredsCB(void* ctx, int nOfRes, OCProvisionResult_t *arr, bool hasError)
431 OC_LOG(ERROR,TAG,"Error occured while credential provisioning");
434 Linkdata_t *link = (Linkdata_t*)ctx;
435 OCProvisionResultCB resultCallback = link->resultCallback;
436 OC_LOG_V(INFO, TAG, "has error returned %d",hasError);
437 UpdateLinkResults(link, 1, arr[0].res);
438 UpdateLinkResults(link, 2, arr[1].res);
441 OC_LOG(ERROR,TAG,"Error occured while credential provisioning");
442 ((OCProvisionResultCB)(resultCallback))(link->ctx, nOfRes,
445 OICFree(link->resArr);
449 if (NULL != link->pDev1Acl)
452 OCStackResult res = SRPProvisionACL(ctx, link->pDev1, link->pDev1Acl, &AclProv1CB);
453 if (OC_STACK_OK!=res)
455 OC_LOG(ERROR, TAG, "Error while provisioning ACL for device 1");
456 UpdateLinkResults(link, 1, res);
457 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
460 OICFree(link->resArr);
464 else if (NULL!=link->pDev2Acl)
466 OC_LOG(ERROR, TAG, "ACL for device 1 is NULL");
467 OCStackResult res = SRPProvisionACL(ctx, link->pDev2, link->pDev2Acl, &AclProv2CB);
468 if (OC_STACK_OK!=res)
470 OC_LOG(ERROR, TAG, "Error while provisioning ACL for device 2");
471 UpdateLinkResults(link, 2, res);
472 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
475 OICFree(link->resArr);
481 OC_LOG(INFO, TAG, "ACLs of both devices are NULL");
482 ((OCProvisionResultCB)(resultCallback))(link->ctx, link->currentCountResults,
485 OICFree(link->resArr);
491 * function to provision credentials between two devices and ACLs for the devices who act as a server.
493 * @param[in] ctx Application context would be returned in result callback.
494 * @param[in] type Type of credentials to be provisioned to the device.
495 * @param[in] pDev1 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
496 * @param[in] acl ACL for device 1. If this is not required set NULL.
497 * @param[in] pDev2 Pointer to OCProvisionDev_t instance,respresenting resource to be provsioned.
498 * @param[in] acl ACL for device 2. If this is not required set NULL.
499 * @param[in] resultCallback callback provided by API user, callback will be called when
500 * provisioning request recieves a response from first resource server.
501 * @return OC_STACK_OK in case of success and other value otherwise.
503 OCStackResult OCProvisionPairwiseDevices(void* ctx, OicSecCredType_t type, size_t keySize,
504 const OCProvisionDev_t *pDev1, OicSecAcl_t *pDev1Acl,
505 const OCProvisionDev_t *pDev2, OicSecAcl_t *pDev2Acl,
506 OCProvisionResultCB resultCallback)
509 if (!pDev1 || !pDev2 || !resultCallback)
511 OC_LOG(ERROR, TAG, "OCProvisionPairwiseDevices : Invalid parameters");
512 return OC_STACK_INVALID_PARAM;
514 if (!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256))
516 OC_LOG(INFO, TAG, "OCProvisionPairwiseDevices : Invalid key size");
517 return OC_STACK_INVALID_PARAM;
519 int noOfResults = 2; // Initial Value
528 Linkdata_t *link = (Linkdata_t*) OICMalloc(sizeof(Linkdata_t));
531 OC_LOG(ERROR, TAG, "Failed to memory allocation");
532 return OC_STACK_NO_MEMORY;
534 OC_LOG_V(INFO,TAG, "Maximum no od results %d",noOfResults);
537 link->pDev1Acl = pDev1Acl;
539 link->pDev2Acl = pDev2Acl;
541 // 1 call for each device for credential provisioning. implict call by SRPProvisioning credential
542 // 1 call for ACL provisioning for device 1 and 1 call for ACL provisioning for device 2.
543 link->numOfResults = noOfResults;
544 link->resultCallback = resultCallback;
545 link->currentCountResults = 0;
546 link->resArr = (OCProvisionResult_t*) OICMalloc(sizeof(OCProvisionResult_t)*noOfResults);
547 OCStackResult res = SRPProvisionCredentials(link, type, keySize,
548 pDev1, pDev2, &ProvisionCredsCB);
549 if (res != OC_STACK_OK)
551 OICFree(link->resArr);