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 * *****************************************************************/
25 #include "ocprovisioningmanager.h"
26 #include "secureresourceprovider.h"
28 #include "oic_malloc.h"
29 #include "aclresource.h"
30 #include "pstatresource.h"
31 #include "srmresourcestrings.h"
32 #include "credresource.h"
33 #include "doxmresource.h"
34 #include "pconfresource.h"
35 #include "credentialgenerator.h"
36 #include "cainterface.h"
39 #include "pmutility.h"
40 #include "provisioningdatabasemanager.h"
43 #include "ocpayload.h"
46 #include "crlresource.h"
52 * Macro to verify argument is not equal to NULL.
53 * eg: VERIFY_NON_NULL(TAG, ptrData, ERROR,OC_STACK_ERROR);
55 #define VERIFY_NON_NULL(tag, arg, logLevel, retValue) { if (NULL == (arg)) \
56 { OIC_LOG((logLevel), tag, #arg " is NULL"); return retValue; } }
59 * Macro to verify success of operation.
60 * eg: VERIFY_SUCCESS(TAG, OC_STACK_OK == foo(), ERROR, OC_STACK_ERROR);
62 #define VERIFY_SUCCESS(tag, op, logLevel, retValue) { if (!(op)) \
63 {OIC_LOG((logLevel), tag, #op " failed!!"); return retValue;} }
66 * Structure to carry credential data to callback.
68 typedef struct CredentialData CredentialData_t;
71 void *ctx; /**< Pointer to user context.**/
72 const OCProvisionDev_t *deviceInfo1; /**< Pointer to OCProvisionDev_t.**/
73 const OCProvisionDev_t *deviceInfo2; /**< Pointer to OCProvisionDev_t.**/
74 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
75 OicSecCred_t *credInfoFirst; /**< Pointer to OicSecCred_t.**/
76 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
77 OCProvisionResult_t *resArr; /**< Result array.**/
78 int numOfResults; /**< Number of results in result array.**/
82 * Structure to carry ACL provision API data to callback.
84 typedef struct ACLData ACLData_t;
87 void *ctx; /**< Pointer to user context.**/
88 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
89 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
90 OCProvisionResult_t *resArr; /**< Result array.**/
91 int numOfResults; /**< Number of results in result array.**/
95 * Structure to carry PCONF provision API data to callback.
97 typedef struct PconfData PconfData_t;
100 void *ctx; /**< Pointer to user context.**/
101 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
102 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
103 OCProvisionResult_t *resArr; /**< Result array.**/
104 int numOfResults; /**< Number of results in result array.**/
107 // Enum type index for unlink callback.
109 IDX_FIRST_DEVICE_RES = 0, // index for resulf of the first device
110 IDX_SECOND_DEVICE_RES, // index for result of the second device
111 IDX_DB_UPDATE_RES // index for result of updating provisioning database.
114 // Structure to carry unlink APIs data to callback.
115 typedef struct UnlinkData UnlinkData_t;
118 OCProvisionDev_t* unlinkDev; /**< Pointer to OCProvisionDev_t to be unlinked.**/
119 OCProvisionResult_t* unlinkRes; /**< Result array.**/
120 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
121 int numOfResults; /**< Number of results in result array.**/
124 //Example of DELETE cred request -> coaps://0.0.0.0:5684/oic/sec/cred?sub=(BASE64 ENCODED UUID)
125 const char * SRP_FORM_DELETE_CREDENTIAL = "coaps://[%s]:%d%s?%s=%s";
127 // Structure to carry remove APIs data to callback.
128 typedef struct RemoveData RemoveData_t;
131 OCProvisionDev_t* revokeTargetDev; /**< Device which is going to be revoked..**/
132 OCProvisionDev_t* linkedDevList; /**< A list of devices which have invalid credential.**/
133 OCProvisionResult_t* removeRes; /**< Result array.**/
134 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
135 size_t numOfResults; /**< Number of results in result array.**/
136 size_t sizeOfResArray;
143 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
144 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
145 OCClientResponseHandler responseHandler);
149 * Internal function to update result in result array.
151 static void registerResultForCredProvisioning(CredentialData_t *credData,
152 OCStackResult stackresult, int cause)
155 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",credData->numOfResults);
158 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
159 credData->deviceInfo1->doxm->deviceID.id,UUID_LENGTH);
163 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
164 credData->deviceInfo2->doxm->deviceID.id,UUID_LENGTH);
166 credData->resArr[(credData->numOfResults)].res = stackresult;
167 ++(credData->numOfResults);
171 * Callback handler for handling callback of provisioning device 2.
173 * @param[in] ctx ctx value passed to callback from calling function.
174 * @param[in] UNUSED handle to an invocation
175 * @param[in] clientResponse Response from queries to remote servers.
176 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
177 * and OC_STACK_KEEP_TRANSACTION to keep it.
179 static OCStackApplicationResult provisionCredentialCB2(void *ctx, OCDoHandle UNUSED,
180 OCClientResponse *clientResponse)
182 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
183 CredentialData_t *credData = (CredentialData_t *) ctx;
186 OCProvisionResultCB resultCallback = credData->resultCallback;
187 OIC_LOG(INFO, TAG, "provisionCredentialCB2 called");
190 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
192 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CREATED, 2);
193 OCStackResult res = PDMLinkDevices(&credData->deviceInfo1->doxm->deviceID,
194 &credData->deviceInfo2->doxm->deviceID);
195 if (OC_STACK_OK != res)
197 OIC_LOG(ERROR, TAG, "Error occured on PDMLinkDevices");
198 return OC_STACK_DELETE_TRANSACTION;
200 OIC_LOG(INFO, TAG, "Link created successfully");
202 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
205 OICFree(credData->resArr);
207 return OC_STACK_DELETE_TRANSACTION;
211 OIC_LOG(INFO, TAG, "provisionCredentialCB2 received Null clientResponse");
212 registerResultForCredProvisioning(credData, OC_STACK_ERROR, 2);
213 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
216 OICFree(credData->resArr);
218 return OC_STACK_DELETE_TRANSACTION;
222 * Callback handler for handling callback of provisioning device 1.
224 * @param[in] ctx ctx value passed to callback from calling function.
225 * @param[in] UNUSED handle to an invocation
226 * @param[in] clientResponse Response from queries to remote servers.
227 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
228 * and OC_STACK_KEEP_TRANSACTION to keep it.
230 static OCStackApplicationResult provisionCredentialCB1(void *ctx, OCDoHandle UNUSED,
231 OCClientResponse *clientResponse)
233 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
235 CredentialData_t* credData = (CredentialData_t*) ctx;
236 OICFree(credData->credInfoFirst);
237 const OCProvisionDev_t *deviceInfo = credData->deviceInfo2;
238 OicSecCred_t *credInfo = credData->credInfo;
239 const OCProvisionResultCB resultCallback = credData->resultCallback;
242 if (OC_STACK_RESOURCE_CREATED == clientResponse->result)
244 // send credentials to second device
245 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CREATED,1);
246 OCStackResult res = provisionCredentials(credInfo, deviceInfo, credData,
247 provisionCredentialCB2);
248 DeleteCredList(credInfo);
249 if (OC_STACK_OK != res)
251 registerResultForCredProvisioning(credData, res,2);
252 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
255 OICFree(credData->resArr);
262 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
263 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
266 OICFree(credData->resArr);
273 OIC_LOG(INFO, TAG, "provisionCredentialCB received Null clientResponse for first device");
274 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
275 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
278 DeleteCredList(credInfo);
279 OICFree(credData->resArr);
283 return OC_STACK_DELETE_TRANSACTION;
289 * Internal function for handling credential generation and sending credential to resource server.
291 * @param[in] cred Instance of cred resource.
292 * @param[in] deviceInfo information about device to which credential is to be provisioned.
293 * @param[in] responseHandler callbak called by OC stack when request API receives response.
294 * @return OC_STACK_OK in case of success and other value otherwise.
296 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
297 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
298 OCClientResponseHandler responseHandler)
300 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
303 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
304 return OC_STACK_NO_MEMORY;
306 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
307 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData1, &secPayload->payloadSize);
308 if((OC_STACK_OK != res) && (NULL == secPayload->securityData1))
310 OCPayloadDestroy((OCPayload *)secPayload);
311 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
312 return OC_STACK_NO_MEMORY;
315 OIC_LOG_V(INFO, TAG, "Credential for provisioning : %s",secPayload->securityData1);
316 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
317 if(!PMGenerateQuery(true,
318 deviceInfo->endpoint.addr,
319 deviceInfo->securePort,
320 deviceInfo->connType,
321 query, sizeof(query), OIC_RSRC_CRED_URI))
323 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
324 return OC_STACK_ERROR;
326 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
328 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
329 cbData.cb = responseHandler;
330 cbData.context = (void *) credData;
333 OCDoHandle handle = NULL;
334 OCMethod method = OC_REST_POST;
335 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
336 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
337 OIC_LOG_V(INFO, TAG, "OCDoResource::Credential provisioning returned : %d",ret);
338 if (ret != OC_STACK_OK)
340 OIC_LOG(ERROR, TAG, "OCStack resource error");
348 * Structure to carry certificate data to callback.
350 typedef struct CertificateData CertData_t;
351 struct CertificateData
353 void *ctx; /**< Pointer to user context.**/
354 const OCProvisionDev_t *deviceInfo; /**< Pointer to OCProvisionDev_t.**/
355 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
356 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
357 OCProvisionResult_t *resArr; /**< Result array.**/
358 int numOfResults; /**< Number of results in result array.**/
362 * Structure to carry CRL provision API data to callback.
364 typedef struct CRLData CRLData_t;
367 void *ctx; /**< Pointer to user context.**/
368 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
369 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
370 OCProvisionResult_t *resArr; /**< Result array.**/
371 int numOfResults; /**< Number of results in result array.**/
375 * Internal function to update result in result array.
377 static void registerResultForCertProvisioning(CertData_t *certData,
378 OCStackResult stackresult)
381 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",certData->numOfResults);
382 memcpy(certData->resArr[(certData->numOfResults)].deviceId.id,
383 certData->deviceInfo->doxm->deviceID.id,UUID_LENGTH);
384 certData->resArr[(certData->numOfResults)].res = stackresult;
385 ++(certData->numOfResults);
389 * Internal Function to store results in result array during ACL provisioning.
391 static void registerResultForCRLProvisioning(CRLData_t *crlData,
392 OCStackResult stackresult)
394 OIC_LOG_V(INFO, TAG, "Inside registerResultForCRLProvisioning crlData->numOfResults is %d\n",
395 crlData->numOfResults);
396 memcpy(crlData->resArr[(crlData->numOfResults)].deviceId.id,
397 crlData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
398 crlData->resArr[(crlData->numOfResults)].res = stackresult;
399 ++(crlData->numOfResults);
404 * Callback handler of SRPProvisionCRL.
406 * @param[in] ctx ctx value passed to callback from calling function.
407 * @param[in] UNUSED handle to an invocation
408 * @param[in] clientResponse Response from queries to remote servers.
409 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
410 * and OC_STACK_KEEP_TRANSACTION to keep it.
412 static OCStackApplicationResult SRPProvisionCRLCB(void *ctx, OCDoHandle UNUSED,
413 OCClientResponse *clientResponse)
415 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionCRLCB.");
417 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
418 CRLData_t *crlData = (CRLData_t*)ctx;
419 OCProvisionResultCB resultCallback = crlData->resultCallback;
423 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
425 registerResultForCRLProvisioning(crlData, OC_STACK_RESOURCE_CREATED);
426 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
429 OICFree(crlData->resArr);
431 return OC_STACK_DELETE_TRANSACTION;
434 registerResultForCRLProvisioning(crlData, OC_STACK_ERROR);
435 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
438 OIC_LOG_V(ERROR, TAG, "SRPProvisionCRLCB received Null clientResponse");
439 OICFree(crlData->resArr);
441 return OC_STACK_DELETE_TRANSACTION;
444 OCStackResult SRPProvisionCRL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
445 OicSecCrl_t *crl, OCProvisionResultCB resultCallback)
447 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
448 VERIFY_NON_NULL(TAG, crl, ERROR, OC_STACK_INVALID_PARAM);
449 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
451 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
454 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
455 return OC_STACK_NO_MEMORY;
458 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
459 OCStackResult res = CrlToCBORPayload(crl, &secPayload->securityData1, &secPayload->payloadSize);
460 if((OC_STACK_OK != res) && (NULL == secPayload->securityData1))
463 OIC_LOG(ERROR, TAG, "Failed to BinToCrlJSON");
464 return OC_STACK_NO_MEMORY;
466 OIC_LOG_V(INFO, TAG, "CRL : %s", secPayload->securityData1);
468 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
469 if(!PMGenerateQuery(true,
470 selectedDeviceInfo->endpoint.addr,
471 selectedDeviceInfo->securePort,
472 selectedDeviceInfo->connType,
473 query, sizeof(query), OIC_RSRC_CRL_URI))
475 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
476 OCPayloadDestroy((OCPayload *)secPayload);
477 return OC_STACK_ERROR;
479 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
481 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
482 cbData.cb = &SRPProvisionCRLCB;
483 CRLData_t *crlData = (CRLData_t *) OICCalloc(1, sizeof(CRLData_t));
486 OCPayloadDestroy((OCPayload *)secPayload);
487 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
488 return OC_STACK_NO_MEMORY;
491 crlData->deviceInfo = selectedDeviceInfo;
492 crlData->resultCallback = resultCallback;
493 crlData->numOfResults=0;
496 crlData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
497 if (crlData->resArr == NULL)
499 OCPayloadDestroy((OCPayload *)secPayload);
500 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
501 return OC_STACK_NO_MEMORY;
504 cbData.context = (void *)crlData;
506 OCMethod method = OC_REST_POST;
507 OCDoHandle handle = NULL;
508 OIC_LOG(DEBUG, TAG, "Sending CRL info to resource server");
510 OCStackResult ret = OCDoResource(&handle, method, query,
511 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
512 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
514 if (ret != OC_STACK_OK)
516 OICFree(crlData->resArr);
524 * Internal function for handling credential generation and sending cretificate credential.
526 * @param[in] cred Instance of cred resource.
527 * @param[in] deviceInfo information about device to which credential is to be provisioned.
528 * @param[in] responseHandler callbak called by OC stack when request API receives response.
529 * @return OC_STACK_OK in case of success and other value otherwise.
531 static OCStackResult provisionCertCred(const OicSecCred_t *cred,
532 const OCProvisionDev_t *deviceInfo, CertData_t *certData,
533 OCClientResponseHandler responseHandler)
535 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
538 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
539 return OC_STACK_NO_MEMORY;
541 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
542 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData1,
543 &secPayload->payloadSize);
545 if ((OC_STACK_OK != res) || (NULL == secPayload->securityData1))
548 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
549 return OC_STACK_NO_MEMORY;
552 OIC_LOG_V(INFO, TAG, "Credential for provisioning : %s",secPayload->securityData1);
553 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
554 if(!PMGenerateQuery(true,
555 deviceInfo->endpoint.addr,
556 deviceInfo->securePort,
557 deviceInfo->connType,
558 query, sizeof(query), OIC_RSRC_CRED_URI))
560 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
561 OCPayloadDestroy((OCPayload *)secPayload);
562 return OC_STACK_ERROR;
564 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
566 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
567 cbData.cb = responseHandler;
568 cbData.context = (void *) certData;
571 OCDoHandle handle = NULL;
572 OCMethod method = OC_REST_POST;
573 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
574 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
575 OIC_LOG_V(INFO, TAG, "OCDoResource::Certificate provisioning returned : %d",ret);
576 if (ret != OC_STACK_OK)
578 OIC_LOG(ERROR, TAG, "OCStack resource error");
585 * Callback handler for handling callback of certificate provisioning device.
587 * @param[in] ctx ctx value passed to callback from calling function.
588 * @param[in] UNUSED handle to an invocation
589 * @param[in] clientResponse Response from queries to remote servers.
590 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
591 * and OC_STACK_KEEP_TRANSACTION to keep it.
593 static OCStackApplicationResult provisionCertCB(void *ctx, OCDoHandle UNUSED,
594 OCClientResponse *clientResponse)
596 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
597 CertData_t *certData = (CertData_t *) ctx;
600 OCProvisionResultCB resultCallback = certData->resultCallback;
601 OIC_LOG(INFO, TAG, "provisionCertCred called");
604 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
606 registerResultForCertProvisioning(certData, OC_STACK_RESOURCE_CREATED);
607 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
610 OICFree(certData->resArr);
612 return OC_STACK_DELETE_TRANSACTION;
616 OIC_LOG(INFO, TAG, "provisionCertCredCB received Null clientResponse");
617 registerResultForCertProvisioning(certData, OC_STACK_ERROR);
618 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
621 OICFree(certData->resArr);
623 return OC_STACK_DELETE_TRANSACTION;
625 #endif // __WITH_X509__
627 OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
628 const OCProvisionDev_t *pDev1,
629 const OCProvisionDev_t *pDev2,
630 OCProvisionResultCB resultCallback)
632 VERIFY_NON_NULL(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM);
633 if (SYMMETRIC_PAIR_WISE_KEY == type)
635 VERIFY_NON_NULL(TAG, pDev2, ERROR, OC_STACK_INVALID_PARAM);
639 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
640 return OC_STACK_INVALID_CALLBACK;
642 if (SYMMETRIC_PAIR_WISE_KEY == type &&
643 0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
645 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
646 return OC_STACK_INVALID_PARAM;
649 if (SYMMETRIC_PAIR_WISE_KEY == type &&
650 !(OWNER_PSK_LENGTH_128 == keySize || OWNER_PSK_LENGTH_256 == keySize))
652 OIC_LOG(INFO, TAG, "Invalid key size");
653 return OC_STACK_INVALID_PARAM;
656 OIC_LOG(INFO, TAG, "In SRPProvisionCredentials");
658 if (SYMMETRIC_PAIR_WISE_KEY == type)
660 bool linkExisits = true;
661 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExisits);
663 if (res != OC_STACK_OK)
665 OIC_LOG(ERROR, TAG, "Internal error occured");
670 OIC_LOG(ERROR, TAG, "Link already exists");
671 return OC_STACK_INVALID_PARAM;
675 OicUuid_t provTooldeviceID = {{0,}};
676 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
678 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
679 return OC_STACK_ERROR;
681 OIC_LOG(INFO, TAG, "retrieved deviceid");
684 case SYMMETRIC_PAIR_WISE_KEY:
686 const OCProvisionDev_t *firstDevice = pDev1;
687 const OCProvisionDev_t *secondDevice = pDev2;
689 OicSecCred_t *firstCred = NULL;
690 OicSecCred_t *secondCred = NULL;
691 OCStackResult res = PMGeneratePairWiseCredentials(type, keySize, &provTooldeviceID,
692 &firstDevice->doxm->deviceID, &secondDevice->doxm->deviceID,
693 &firstCred, &secondCred);
694 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
695 OIC_LOG(INFO, TAG, "Credentials generated successfully");
696 CredentialData_t *credData =
697 (CredentialData_t *) OICCalloc(1, sizeof(CredentialData_t));
698 if (NULL == credData)
702 OIC_LOG(ERROR, TAG, "Memory allocation problem");
703 return OC_STACK_NO_MEMORY;
705 credData->deviceInfo1 = firstDevice;
706 credData->deviceInfo2 = secondDevice;
707 credData->credInfo = secondCred;
709 credData->credInfoFirst = firstCred;
710 credData->numOfResults = 0;
711 credData->resultCallback = resultCallback;
712 // first call to provision creds to device1.
713 // second call to provision creds to device2.
716 (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
717 if (NULL == credData->resArr)
722 OIC_LOG(ERROR, TAG, "Memory allocation problem");
723 return OC_STACK_NO_MEMORY;
725 res = provisionCredentials(firstCred, firstDevice, credData, &provisionCredentialCB1);
726 if (OC_STACK_OK != res)
728 DeleteCredList(firstCred);
729 DeleteCredList(secondCred);
730 OICFree(credData->resArr);
733 OIC_LOG_V(INFO, TAG, "provisionCredentials returned: %d",res);
734 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
738 case SIGNED_ASYMMETRIC_KEY:
740 const OCProvisionDev_t *firstDevice = pDev1;
741 OicSecCred_t *cred = NULL;
742 OCStackResult res = PMGenerateCertificateCredentials(&provTooldeviceID,
743 &firstDevice->doxm->deviceID,&cred);
744 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
745 OIC_LOG(INFO, TAG, "Certificate credentials generated successfully");
746 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
747 if (NULL == certData)
750 OIC_LOG(ERROR, TAG, "Memory allocation problem");
751 return OC_STACK_NO_MEMORY;
754 certData->deviceInfo = firstDevice;
756 certData->credInfo = cred;
757 certData->numOfResults = 0;
758 certData->resultCallback = resultCallback;
760 certData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
761 if (NULL == certData->resArr)
763 DeleteCredList(cred);
765 OIC_LOG(ERROR, TAG, "Memory allocation problem");
766 return OC_STACK_NO_MEMORY;
769 res = provisionCertCred(cred, firstDevice, certData, &provisionCertCB);
770 if (OC_STACK_OK != res)
772 OICFree(certData->resArr);
775 DeleteCredList(cred);
776 OIC_LOG_V(INFO, TAG, "provisionCertCredentials returned: %d",res);
783 OIC_LOG(ERROR, TAG, "Invalid option.");
784 return OC_STACK_INVALID_PARAM;
787 return OC_STACK_ERROR;
791 * Internal Function to store results in result array during ACL provisioning.
793 static void registerResultForACLProvisioning(ACLData_t *aclData,
794 OCStackResult stackresult)
796 OIC_LOG_V(INFO, TAG, "Inside registerResultForACLProvisioning aclData->numOfResults is %d\n",
797 aclData->numOfResults);
798 memcpy(aclData->resArr[(aclData->numOfResults)].deviceId.id,
799 aclData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
800 aclData->resArr[(aclData->numOfResults)].res = stackresult;
801 ++(aclData->numOfResults);
805 * Callback handler of SRPProvisionACL.
807 * @param[in] ctx ctx value passed to callback from calling function.
808 * @param[in] UNUSED handle to an invocation
809 * @param[in] clientResponse Response from queries to remote servers.
810 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
811 * and OC_STACK_KEEP_TRANSACTION to keep it.
813 static OCStackApplicationResult SRPProvisionACLCB(void *ctx, OCDoHandle UNUSED,
814 OCClientResponse *clientResponse)
816 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionACLCB.");
818 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
819 ACLData_t *aclData = (ACLData_t*)ctx;
820 OCProvisionResultCB resultCallback = aclData->resultCallback;
824 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
826 registerResultForACLProvisioning(aclData, OC_STACK_RESOURCE_CREATED);
827 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
830 OICFree(aclData->resArr);
832 return OC_STACK_DELETE_TRANSACTION;
835 registerResultForACLProvisioning(aclData, OC_STACK_ERROR);
836 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
839 OIC_LOG_V(ERROR, TAG, "SRPProvisionACLCB received Null clientResponse");
840 OICFree(aclData->resArr);
842 return OC_STACK_DELETE_TRANSACTION;
845 OCStackResult SRPProvisionACL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
846 OicSecAcl_t *acl, OCProvisionResultCB resultCallback)
848 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
849 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
850 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
852 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
855 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
856 return OC_STACK_NO_MEMORY;
858 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
859 if(OC_STACK_OK != AclToCBORPayload(acl, &secPayload->securityData1, &secPayload->payloadSize))
861 OCPayloadDestroy((OCPayload *)secPayload);
862 OIC_LOG(ERROR, TAG, "Failed to AclToCBORPayload");
863 return OC_STACK_NO_MEMORY;
865 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
866 if(!PMGenerateQuery(true,
867 selectedDeviceInfo->endpoint.addr,
868 selectedDeviceInfo->securePort,
869 selectedDeviceInfo->connType,
870 query, sizeof(query), OIC_RSRC_ACL_URI))
872 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
873 return OC_STACK_ERROR;
875 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
877 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
878 cbData.cb = &SRPProvisionACLCB;
879 ACLData_t *aclData = (ACLData_t *) OICCalloc(1, sizeof(ACLData_t));
882 OCPayloadDestroy((OCPayload *)secPayload);
883 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
884 return OC_STACK_NO_MEMORY;
886 aclData->deviceInfo = selectedDeviceInfo;
887 aclData->resultCallback = resultCallback;
888 aclData->numOfResults=0;
890 // call to provision ACL to device1.
892 aclData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
893 if (aclData->resArr == NULL)
896 OCPayloadDestroy((OCPayload *)secPayload);
897 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
898 return OC_STACK_NO_MEMORY;
900 cbData.context = (void *)aclData;
902 OCMethod method = OC_REST_POST;
903 OCDoHandle handle = NULL;
904 OIC_LOG(DEBUG, TAG, "Sending ACL info to resource server");
905 OCStackResult ret = OCDoResource(&handle, method, query,
906 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
907 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
908 if (ret != OC_STACK_OK)
910 OICFree(aclData->resArr);
913 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
918 * Internal Function to store results in result array during Direct-Pairing provisioning.
920 static void registerResultForDirectPairingProvisioning(PconfData_t *pconfData,
921 OCStackResult stackresult)
923 OIC_LOG_V(INFO, TAG, "Inside registerResultForDirectPairingProvisioning "
924 "pconfData->numOfResults is %d\n", pconfData->numOfResults);
925 memcpy(pconfData->resArr[(pconfData->numOfResults)].deviceId.id,
926 pconfData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
927 pconfData->resArr[(pconfData->numOfResults)].res = stackresult;
928 ++(pconfData->numOfResults);
932 * Callback handler of SRPProvisionDirectPairing.
934 * @param[in] ctx ctx value passed to callback from calling function.
935 * @param[in] UNUSED handle to an invocation
936 * @param[in] clientResponse Response from queries to remote servers.
937 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
938 * and OC_STACK_KEEP_TRANSACTION to keep it.
940 static OCStackApplicationResult SRPProvisionDirectPairingCB(void *ctx, OCDoHandle UNUSED,
941 OCClientResponse *clientResponse)
943 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionDirectPairingCB.");
945 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
946 PconfData_t *pconfData = (PconfData_t*)ctx;
947 OCProvisionResultCB resultCallback = pconfData->resultCallback;
951 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
953 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_OK);
954 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
957 OICFree(pconfData->resArr);
959 return OC_STACK_DELETE_TRANSACTION;
962 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_ERROR);
963 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
966 OIC_LOG_V(ERROR, TAG, "SRPProvisionDirectPairingCB received Null clientResponse");
967 OICFree(pconfData->resArr);
969 return OC_STACK_DELETE_TRANSACTION;
972 OCStackResult SRPProvisionDirectPairing(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
973 OicSecPconf_t *pconf, OCProvisionResultCB resultCallback)
975 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
976 VERIFY_NON_NULL(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
977 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
979 // check direct-pairing capability
980 if (true != selectedDeviceInfo->doxm->dpc)
982 OIC_LOG(ERROR, TAG, "Resouce server does not have Direct-Pairing Capability ");
983 return OC_STACK_UNAUTHORIZED_REQ;
986 OicUuid_t provTooldeviceID = {.id={0}};
987 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
989 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
990 return OC_STACK_ERROR;
992 memcpy(&pconf->rownerID, &provTooldeviceID, sizeof(OicUuid_t));
994 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
997 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
998 return OC_STACK_NO_MEMORY;
1000 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1002 if (OC_STACK_OK != PconfToCBORPayload(pconf, &(secPayload->securityData1),
1003 &(secPayload->payloadSize)))
1005 OCPayloadDestroy((OCPayload*)secPayload);
1006 OIC_LOG(ERROR, TAG, "Failed to PconfToCborPayload");
1007 return OC_STACK_NO_MEMORY;
1009 OIC_LOG_V(INFO, TAG, "PCONF : %s", secPayload->securityData1);
1011 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1012 if(!PMGenerateQuery(true,
1013 selectedDeviceInfo->endpoint.addr,
1014 selectedDeviceInfo->securePort,
1015 selectedDeviceInfo->connType,
1016 query, sizeof(query), OIC_RSRC_PCONF_URI))
1018 OIC_LOG(ERROR, TAG, "SRPProvisionDirectPairing : Failed to generate query");
1019 return OC_STACK_ERROR;
1021 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1023 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1024 cbData.cb = &SRPProvisionDirectPairingCB;
1025 PconfData_t *pconfData = (PconfData_t *) OICCalloc(1, sizeof(PconfData_t));
1026 if (NULL == pconfData)
1028 OCPayloadDestroy((OCPayload*)secPayload);
1029 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1030 return OC_STACK_NO_MEMORY;
1032 pconfData->deviceInfo = selectedDeviceInfo;
1033 pconfData->resultCallback = resultCallback;
1034 pconfData->numOfResults=0;
1035 pconfData->ctx = ctx;
1036 // call to provision PCONF to device1.
1037 int noOfRiCalls = 1;
1038 pconfData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1039 if (NULL == pconfData->resArr)
1042 OCPayloadDestroy((OCPayload*)secPayload);
1043 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1044 return OC_STACK_NO_MEMORY;
1046 cbData.context = (void *)pconfData;
1048 OCMethod method = OC_REST_POST;
1049 OCDoHandle handle = NULL;
1050 OIC_LOG(DEBUG, TAG, "Sending PCONF info to resource server");
1051 OCStackResult ret = OCDoResource(&handle, method, query,
1052 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1053 selectedDeviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1054 if (OC_STACK_OK != ret)
1056 OICFree(pconfData->resArr);
1059 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1063 static void DeleteUnlinkData_t(UnlinkData_t *unlinkData)
1067 OICFree(unlinkData->unlinkDev);
1068 OICFree(unlinkData->unlinkRes);
1069 OICFree(unlinkData);
1073 static void registerResultForUnlinkDevices(UnlinkData_t *unlinkData, OCStackResult stackresult,
1076 if (NULL != unlinkData)
1078 OIC_LOG_V(INFO, TAG, "Inside registerResultForUnlinkDevices unlinkData->numOfResults is %d\n",
1079 unlinkData->numOfResults);
1080 OIC_LOG_V(INFO, TAG, "Stack result :: %d", stackresult);
1082 OicUuid_t *pUuid = &unlinkData->unlinkRes[(unlinkData->numOfResults)].deviceId;
1084 // Set result in the result array according to the position (devNum).
1085 if (idx != IDX_DB_UPDATE_RES)
1087 memcpy(pUuid->id, unlinkData->unlinkDev[idx].doxm->deviceID.id, sizeof(pUuid->id));
1090 { // When deivce ID is 000... this means it's the result of database update.
1091 memset(pUuid->id, 0, sizeof(pUuid->id));
1093 unlinkData->unlinkRes[(unlinkData->numOfResults)].res = stackresult;
1094 ++(unlinkData->numOfResults);
1095 OIC_LOG (INFO, TAG, "Out registerResultForUnlinkDevices");
1099 static OCStackResult SendDeleteCredentialRequest(void* ctx,
1100 OCClientResponseHandler respHandler,
1101 const OCProvisionDev_t* revokedDev,
1102 const OCProvisionDev_t* destDev)
1104 OIC_LOG(DEBUG, TAG, "IN SendDeleteCredentialRequest");
1106 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1108 return OC_STACK_INVALID_PARAM;
1112 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1113 if(OC_STACK_OK != ret)
1115 OIC_LOG(ERROR, TAG, "SendDeleteCredentialRequest : Failed to canonical UUID encoding");
1116 return OC_STACK_ERROR;
1119 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1121 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1122 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, destDev->endpoint.addr,
1123 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1127 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1128 return OC_STACK_ERROR;
1130 else if ((size_t)snRet >= sizeof(reqBuf))
1132 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1133 return OC_STACK_ERROR;
1136 OCCallbackData cbData;
1137 memset(&cbData, 0, sizeof(cbData));
1138 cbData.context = ctx;
1139 cbData.cb = respHandler;
1141 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1143 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1145 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1146 &destDev->endpoint, NULL,
1147 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1148 if (OC_STACK_OK != ret)
1150 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1152 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1158 * Callback handler of unlink second device.
1160 * @param[in] ctx ctx value passed to callback from calling function.
1161 * @param[in] handle handle to an invocation
1162 * @param[in] clientResponse Response from queries to remote servers.
1163 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1164 * OC_STACK_KEEP_TRANSACTION to keep it.
1166 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1167 OCClientResponse *clientResponse)
1170 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1171 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1172 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1176 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1177 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1179 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1181 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1185 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1186 unlinkData->resultCallback(unlinkData->ctx,
1187 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1193 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1194 IDX_SECOND_DEVICE_RES);
1195 unlinkData->resultCallback(unlinkData->ctx,
1196 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1197 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1201 //Update provisioning DB when succes case.
1202 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1203 &unlinkData->unlinkDev[1].doxm->deviceID))
1205 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1206 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1207 unlinkData->resultCallback(unlinkData->ctx,
1208 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1211 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1215 DeleteUnlinkData_t(unlinkData);
1216 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1217 return OC_STACK_DELETE_TRANSACTION;
1222 * Callback handler of unlink first device.
1224 * @param[in] ctx ctx value passed to callback from calling function.
1225 * @param[in] handle handle to an invocation
1226 * @param[in] clientResponse Response from queries to remote servers.
1227 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1228 * OC_STACK_KEEP_TRANSACTION to keep it.
1230 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1231 OCClientResponse *clientResponse)
1233 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1234 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1235 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1240 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1241 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1243 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1245 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1247 // Second revocation request to second device.
1248 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1249 &unlinkData->unlinkDev[0],
1250 &unlinkData->unlinkDev[1] /*Dest*/);
1251 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1252 if (OC_STACK_OK != res)
1254 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1255 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1256 IDX_SECOND_DEVICE_RES);
1257 unlinkData->resultCallback(unlinkData->ctx,
1258 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1263 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1264 return OC_STACK_DELETE_TRANSACTION;
1269 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1271 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1272 unlinkData->unlinkRes, true);
1278 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1279 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1280 IDX_FIRST_DEVICE_RES );
1281 unlinkData->resultCallback(unlinkData->ctx,
1282 unlinkData->numOfResults, unlinkData->unlinkRes,
1284 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1288 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1289 DeleteUnlinkData_t(unlinkData);
1290 return OC_STACK_DELETE_TRANSACTION;
1294 * Function to unlink devices.
1295 * This function will remove the credential & relationship between the two devices.
1297 * @param[in] ctx Application context would be returned in result callback
1298 * @param[in] pTargetDev1 first device information to be unlinked.
1299 * @param[in] pTargetDev2 second device information to be unlinked.
1300 * @param[in] resultCallback callback provided by API user, callback will be called when
1301 * device unlink is finished.
1302 * @return OC_STACK_OK in case of success and other value otherwise.
1304 OCStackResult SRPUnlinkDevices(void* ctx,
1305 const OCProvisionDev_t* pTargetDev1,
1306 const OCProvisionDev_t* pTargetDev2,
1307 OCProvisionResultCB resultCallback)
1309 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1311 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1313 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1314 return OC_STACK_INVALID_PARAM;
1316 if (!resultCallback)
1318 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1319 return OC_STACK_INVALID_CALLBACK;
1321 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1323 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1324 return OC_STACK_INVALID_PARAM;
1327 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1328 PMPrintOCProvisionDev(pTargetDev1);
1329 PMPrintOCProvisionDev(pTargetDev2);
1331 // Mark the link status stale
1332 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1333 if (OC_STACK_OK != res)
1335 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1339 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1340 VERIFY_NON_NULL(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1342 //Initialize unlink data
1343 unlinkData->ctx = ctx;
1344 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1345 if (NULL == unlinkData->unlinkDev)
1347 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1348 res = OC_STACK_NO_MEMORY;
1352 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1353 if (NULL == unlinkData->unlinkRes)
1355 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1356 res = OC_STACK_NO_MEMORY;
1360 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1361 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1363 unlinkData->numOfResults = 0;
1364 unlinkData->resultCallback = resultCallback;
1366 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1367 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1368 if (OC_STACK_OK != res)
1370 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1377 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1378 DeleteUnlinkData_t(unlinkData);
1382 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1386 OICFree(pRemoveData->revokeTargetDev);
1387 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1388 OICFree(pRemoveData->removeRes);
1389 OICFree(pRemoveData);
1393 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1394 OCStackResult stackresult, bool hasError)
1396 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1397 removeData->numOfResults + 1);
1400 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1401 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1405 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1406 0, sizeof(pLinkedDevId->id) );
1408 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1409 removeData->hasError = hasError;
1410 ++(removeData->numOfResults);
1412 // If we get suffcient result from linked devices, we have to call user callback and do free
1413 if (removeData->sizeOfResArray == removeData->numOfResults)
1415 if(!removeData->hasError)
1417 // Remove device info from prvisioning database
1418 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1420 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1421 removeData->hasError = true;
1424 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1425 removeData->hasError);
1426 DeleteRemoveData_t(removeData);
1431 * Callback handler of unlink first device.
1433 * @param[in] ctx ctx value passed to callback from calling function.
1434 * @param[in] handle handle to an invocation
1435 * @param[in] clientResponse Response from queries to remote servers.
1436 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1437 * and OC_STACK_KEEP_TRANSACTION to keep it.
1439 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1440 OCClientResponse *clientResponse)
1442 //Update the delete credential into delete device context
1443 //Save the deleted status in delDevCtx
1445 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1446 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1447 OCStackResult res = OC_STACK_ERROR;
1449 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1453 OicUuid_t revDevUuid = {.id={0}};
1454 if(UUID_LENGTH == clientResponse->identity.id_length)
1456 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1457 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1459 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1460 if (OC_STACK_OK != res)
1462 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1463 registerResultForRemoveDevice(removeData, &revDevUuid,
1464 OC_STACK_INCONSISTENT_DB, true);
1466 return OC_STACK_DELETE_TRANSACTION;
1469 registerResultForRemoveDevice(removeData, &revDevUuid,
1470 OC_STACK_RESOURCE_DELETED, false);
1474 registerResultForRemoveDevice(removeData, &revDevUuid,
1475 clientResponse->result, true);
1476 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1481 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1482 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1484 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1487 * Since server's credential was deleted,
1488 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1490 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1491 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1495 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1501 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1502 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1506 return OC_STACK_DELETE_TRANSACTION;
1509 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1510 OCProvisionDev_t* pOwnedDevList,
1511 OCUuidList_t* pLinkedUuidList,
1512 OCProvisionDev_t** ppLinkedDevList,
1513 size_t *numOfLinkedDev)
1515 // pOwnedDevList could be NULL. It means no alived and owned device now.
1516 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1517 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1519 return OC_STACK_INVALID_PARAM;
1523 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1524 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1526 // Mark the link status stale.
1527 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1528 if (OC_STACK_OK != res)
1530 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1531 return OC_STACK_INCONSISTENT_DB;
1536 // If this linked device is alive (power-on), add the deivce to the list.
1537 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1538 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1540 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1542 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1543 if (NULL == targetDev)
1545 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1546 return OC_STACK_NO_MEMORY;
1549 LL_PREPEND(*ppLinkedDevList, targetDev);
1556 *numOfLinkedDev = cnt;
1561 * Function to device revocation
1562 * This function will remove credential of target device from all devices in subnet.
1564 * @param[in] ctx Application context would be returned in result callback
1565 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1566 * @param[in] pTargetDev Device information to be revoked.
1567 * @param[in] resultCallback callback provided by API user, callback will be called when
1568 * credential revocation is finished.
1569 * @return OC_STACK_OK in case of success and other value otherwise.
1570 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1571 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1573 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1574 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1576 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1578 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1580 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1581 return OC_STACK_INVALID_PARAM;
1583 if (!resultCallback)
1585 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
1586 return OC_STACK_INVALID_CALLBACK;
1589 // Declare variables in here to handle error cases with goto statement.
1590 OCProvisionDev_t* pOwnedDevList = NULL;
1591 OCProvisionDev_t* pLinkedDevList = NULL;
1592 RemoveData_t* removeData = NULL;
1594 //1. Find all devices that has a credential of the revoked device
1595 OCUuidList_t* pLinkedUuidList = NULL;
1596 size_t numOfDevices = 0;
1597 OCStackResult res = OC_STACK_ERROR;
1598 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
1599 if (OC_STACK_OK != res)
1601 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
1604 // if there is no related device, we can skip further process.
1605 if (0 == numOfDevices)
1607 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
1608 res = OC_STACK_CONTINUE;
1612 //2. Find owned device from the network
1613 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
1614 if (OC_STACK_OK != res)
1616 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
1620 //3. Make a list of devices to send DELETE credential request
1621 // by comparing owned devices from provisioning database with mutlicast discovery result.
1622 size_t numOfLinkedDev = 0;
1623 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
1624 &pLinkedDevList, &numOfLinkedDev);
1625 if (OC_STACK_OK != res)
1627 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
1630 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
1631 { // So we don't have to send request message.
1632 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
1633 res = OC_STACK_CONTINUE;
1637 // 4. Prepare RemoveData Context data.
1638 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
1641 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1642 res = OC_STACK_NO_MEMORY;
1646 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
1647 if (!removeData->revokeTargetDev)
1649 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
1650 res = OC_STACK_NO_MEMORY;
1654 removeData->removeRes =
1655 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
1656 if (!removeData->removeRes)
1658 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1659 res = OC_STACK_NO_MEMORY;
1663 removeData->ctx = ctx;
1664 removeData->linkedDevList = pLinkedDevList;
1665 removeData->resultCallback = resultCallback;
1666 removeData->numOfResults = 0;
1667 removeData->sizeOfResArray = numOfLinkedDev;
1668 removeData->hasError = false;
1670 // 5. Send DELETE credential request to linked devices.
1671 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1672 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
1673 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
1675 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
1676 removeData->revokeTargetDev, curDev);
1677 if (OC_STACK_OK != res)
1679 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
1680 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
1684 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
1688 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
1689 PMDeleteDeviceList(pOwnedDevList);
1690 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
1692 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
1695 PDMDestoryOicUuidLinkList(pLinkedUuidList);
1696 PMDeleteDeviceList(pOwnedDevList);
1697 PMDeleteDeviceList(pLinkedDevList);
1700 OICFree(removeData->revokeTargetDev);
1701 OICFree(removeData->removeRes);
1702 OICFree(removeData);
1704 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");