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 * *****************************************************************/
27 #include "ocprovisioningmanager.h"
28 #include "secureresourceprovider.h"
30 #include "oic_malloc.h"
31 #include "aclresource.h"
32 #include "pstatresource.h"
33 #include "srmresourcestrings.h"
34 #include "credresource.h"
35 #include "doxmresource.h"
36 #include "pconfresource.h"
37 #include "credentialgenerator.h"
38 #include "cainterface.h"
41 #include "pmutility.h"
42 #include "provisioningdatabasemanager.h"
45 #include "ocpayload.h"
48 #include "crlresource.h"
54 * Macro to verify argument is not equal to NULL.
55 * eg: VERIFY_NON_NULL(TAG, ptrData, ERROR,OC_STACK_ERROR);
57 #define VERIFY_NON_NULL(tag, arg, logLevel, retValue) { if (NULL == (arg)) \
58 { OIC_LOG((logLevel), tag, #arg " is NULL"); return retValue; } }
61 * Macro to verify success of operation.
62 * eg: VERIFY_SUCCESS(TAG, OC_STACK_OK == foo(), ERROR, OC_STACK_ERROR);
64 #define VERIFY_SUCCESS(tag, op, logLevel, retValue) { if (!(op)) \
65 {OIC_LOG((logLevel), tag, #op " failed!!"); return retValue;} }
68 * Structure to carry credential data to callback.
70 typedef struct CredentialData CredentialData_t;
73 void *ctx; /**< Pointer to user context.**/
74 const OCProvisionDev_t *deviceInfo1; /**< Pointer to OCProvisionDev_t.**/
75 const OCProvisionDev_t *deviceInfo2; /**< Pointer to OCProvisionDev_t.**/
76 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
77 OicSecCred_t *credInfoFirst; /**< Pointer to OicSecCred_t.**/
78 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
79 OCProvisionResult_t *resArr; /**< Result array.**/
80 int numOfResults; /**< Number of results in result array.**/
84 * Structure to carry ACL provision API data to callback.
86 typedef struct ACLData ACLData_t;
89 void *ctx; /**< Pointer to user context.**/
90 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
91 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
92 OCProvisionResult_t *resArr; /**< Result array.**/
93 int numOfResults; /**< Number of results in result array.**/
96 // Structure to carry get security resource APIs data to callback.
97 typedef struct GetSecData GetSecData_t;
100 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
101 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
102 OCProvisionResult_t *resArr; /**< Result array.**/
103 int numOfResults; /**< Number of results in result array.**/
107 * Structure to carry PCONF provision API data to callback.
109 typedef struct PconfData PconfData_t;
112 void *ctx; /**< Pointer to user context.**/
113 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
114 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
115 OCProvisionResult_t *resArr; /**< Result array.**/
116 int numOfResults; /**< Number of results in result array.**/
119 // Enum type index for unlink callback.
121 IDX_FIRST_DEVICE_RES = 0, // index for resulf of the first device
122 IDX_SECOND_DEVICE_RES, // index for result of the second device
123 IDX_DB_UPDATE_RES // index for result of updating provisioning database.
126 // Structure to carry unlink APIs data to callback.
127 typedef struct UnlinkData UnlinkData_t;
130 OCProvisionDev_t* unlinkDev; /**< Pointer to OCProvisionDev_t to be unlinked.**/
131 OCProvisionResult_t* unlinkRes; /**< Result array.**/
132 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
133 int numOfResults; /**< Number of results in result array.**/
136 //Example of DELETE cred request -> coaps://0.0.0.0:5684/oic/sec/cred?sub=(BASE64 ENCODED UUID)
137 const char * SRP_FORM_DELETE_CREDENTIAL = "coaps://[%s]:%d%s?%s=%s";
139 // Structure to carry remove APIs data to callback.
140 typedef struct RemoveData RemoveData_t;
143 OCProvisionDev_t* revokeTargetDev; /**< Device which is going to be revoked..**/
144 OCProvisionDev_t* linkedDevList; /**< A list of devices which have invalid credential.**/
145 OCProvisionResult_t* removeRes; /**< Result array.**/
146 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
147 size_t numOfResults; /**< Number of results in result array.**/
148 size_t sizeOfResArray;
155 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
156 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
157 OCClientResponseHandler responseHandler);
161 * Internal function to update result in result array.
163 static void registerResultForCredProvisioning(CredentialData_t *credData,
164 OCStackResult stackresult, int cause)
167 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",credData->numOfResults);
170 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
171 credData->deviceInfo1->doxm->deviceID.id,UUID_LENGTH);
175 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
176 credData->deviceInfo2->doxm->deviceID.id,UUID_LENGTH);
178 credData->resArr[(credData->numOfResults)].res = stackresult;
179 ++(credData->numOfResults);
183 * Callback handler for handling callback of provisioning device 2.
185 * @param[in] ctx ctx value passed to callback from calling function.
186 * @param[in] UNUSED handle to an invocation
187 * @param[in] clientResponse Response from queries to remote servers.
188 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
189 * and OC_STACK_KEEP_TRANSACTION to keep it.
191 static OCStackApplicationResult provisionCredentialCB2(void *ctx, OCDoHandle UNUSED,
192 OCClientResponse *clientResponse)
194 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
195 CredentialData_t *credData = (CredentialData_t *) ctx;
198 OCProvisionResultCB resultCallback = credData->resultCallback;
199 OIC_LOG(INFO, TAG, "provisionCredentialCB2 called");
202 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
204 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CREATED, 2);
205 OCStackResult res = PDMLinkDevices(&credData->deviceInfo1->doxm->deviceID,
206 &credData->deviceInfo2->doxm->deviceID);
207 if (OC_STACK_OK != res)
209 OIC_LOG(ERROR, TAG, "Error occured on PDMLinkDevices");
210 return OC_STACK_DELETE_TRANSACTION;
212 OIC_LOG(INFO, TAG, "Link created successfully");
214 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
217 OICFree(credData->resArr);
219 return OC_STACK_DELETE_TRANSACTION;
223 OIC_LOG(INFO, TAG, "provisionCredentialCB2 received Null clientResponse");
224 registerResultForCredProvisioning(credData, OC_STACK_ERROR, 2);
225 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
228 OICFree(credData->resArr);
230 return OC_STACK_DELETE_TRANSACTION;
234 * Callback handler for handling callback of provisioning device 1.
236 * @param[in] ctx ctx value passed to callback from calling function.
237 * @param[in] UNUSED handle to an invocation
238 * @param[in] clientResponse Response from queries to remote servers.
239 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
240 * and OC_STACK_KEEP_TRANSACTION to keep it.
242 static OCStackApplicationResult provisionCredentialCB1(void *ctx, OCDoHandle UNUSED,
243 OCClientResponse *clientResponse)
245 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
247 CredentialData_t* credData = (CredentialData_t*) ctx;
248 OICFree(credData->credInfoFirst);
249 const OCProvisionDev_t *deviceInfo = credData->deviceInfo2;
250 OicSecCred_t *credInfo = credData->credInfo;
251 const OCProvisionResultCB resultCallback = credData->resultCallback;
254 if (OC_STACK_RESOURCE_CREATED == clientResponse->result)
256 // send credentials to second device
257 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CREATED,1);
258 OCStackResult res = provisionCredentials(credInfo, deviceInfo, credData,
259 provisionCredentialCB2);
260 DeleteCredList(credInfo);
261 if (OC_STACK_OK != res)
263 registerResultForCredProvisioning(credData, res,2);
264 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
267 OICFree(credData->resArr);
274 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
275 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
278 OICFree(credData->resArr);
285 OIC_LOG(INFO, TAG, "provisionCredentialCB received Null clientResponse for first device");
286 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
287 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
290 DeleteCredList(credInfo);
291 OICFree(credData->resArr);
295 return OC_STACK_DELETE_TRANSACTION;
301 * Internal function for handling credential generation and sending credential to resource server.
303 * @param[in] cred Instance of cred resource.
304 * @param[in] deviceInfo information about device to which credential is to be provisioned.
305 * @param[in] responseHandler callbak called by OC stack when request API receives response.
306 * @return OC_STACK_OK in case of success and other value otherwise.
308 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
309 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
310 OCClientResponseHandler responseHandler)
312 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
315 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
316 return OC_STACK_NO_MEMORY;
318 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
320 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
321 &secPayload->payloadSize, secureFlag);
322 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
324 OCPayloadDestroy((OCPayload *)secPayload);
325 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
326 return OC_STACK_NO_MEMORY;
329 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
330 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
331 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
332 if(!PMGenerateQuery(true,
333 deviceInfo->endpoint.addr,
334 deviceInfo->securePort,
335 deviceInfo->connType,
336 query, sizeof(query), OIC_RSRC_CRED_URI))
338 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
339 return OC_STACK_ERROR;
341 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
343 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
344 cbData.cb = responseHandler;
345 cbData.context = (void *) credData;
348 OCDoHandle handle = NULL;
349 OCMethod method = OC_REST_POST;
350 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
351 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
352 OIC_LOG_V(INFO, TAG, "OCDoResource::Credential provisioning returned : %d",ret);
353 if (ret != OC_STACK_OK)
355 OIC_LOG(ERROR, TAG, "OCStack resource error");
363 * Structure to carry certificate data to callback.
365 typedef struct CertificateData CertData_t;
366 struct CertificateData
368 void *ctx; /**< Pointer to user context.**/
369 const OCProvisionDev_t *deviceInfo; /**< Pointer to OCProvisionDev_t.**/
370 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
371 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
372 OCProvisionResult_t *resArr; /**< Result array.**/
373 int numOfResults; /**< Number of results in result array.**/
377 * Structure to carry CRL provision API data to callback.
379 typedef struct CRLData CRLData_t;
382 void *ctx; /**< Pointer to user context.**/
383 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
384 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
385 OCProvisionResult_t *resArr; /**< Result array.**/
386 int numOfResults; /**< Number of results in result array.**/
390 * Internal function to update result in result array.
392 static void registerResultForCertProvisioning(CertData_t *certData,
393 OCStackResult stackresult)
396 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",certData->numOfResults);
397 memcpy(certData->resArr[(certData->numOfResults)].deviceId.id,
398 certData->deviceInfo->doxm->deviceID.id,UUID_LENGTH);
399 certData->resArr[(certData->numOfResults)].res = stackresult;
400 ++(certData->numOfResults);
404 * Internal Function to store results in result array during ACL provisioning.
406 static void registerResultForCRLProvisioning(CRLData_t *crlData,
407 OCStackResult stackresult)
409 OIC_LOG_V(INFO, TAG, "Inside registerResultForCRLProvisioning crlData->numOfResults is %d\n",
410 crlData->numOfResults);
411 memcpy(crlData->resArr[(crlData->numOfResults)].deviceId.id,
412 crlData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
413 crlData->resArr[(crlData->numOfResults)].res = stackresult;
414 ++(crlData->numOfResults);
419 * Callback handler of SRPProvisionCRL.
421 * @param[in] ctx ctx value passed to callback from calling function.
422 * @param[in] UNUSED handle to an invocation
423 * @param[in] clientResponse Response from queries to remote servers.
424 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
425 * and OC_STACK_KEEP_TRANSACTION to keep it.
427 static OCStackApplicationResult SRPProvisionCRLCB(void *ctx, OCDoHandle UNUSED,
428 OCClientResponse *clientResponse)
430 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionCRLCB.");
432 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
433 CRLData_t *crlData = (CRLData_t*)ctx;
434 OCProvisionResultCB resultCallback = crlData->resultCallback;
438 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
440 registerResultForCRLProvisioning(crlData, OC_STACK_RESOURCE_CREATED);
441 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
444 OICFree(crlData->resArr);
446 return OC_STACK_DELETE_TRANSACTION;
449 registerResultForCRLProvisioning(crlData, OC_STACK_ERROR);
450 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
453 OIC_LOG_V(ERROR, TAG, "SRPProvisionCRLCB received Null clientResponse");
454 OICFree(crlData->resArr);
456 return OC_STACK_DELETE_TRANSACTION;
459 OCStackResult SRPProvisionCRL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
460 OicSecCrl_t *crl, OCProvisionResultCB resultCallback)
462 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
463 VERIFY_NON_NULL(TAG, crl, ERROR, OC_STACK_INVALID_PARAM);
464 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
466 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
469 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
470 return OC_STACK_NO_MEMORY;
473 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
474 OCStackResult res = CrlToCBORPayload(crl, &secPayload->securityData, &secPayload->payloadSize);
475 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
478 OIC_LOG(ERROR, TAG, "Failed to BinToCrlJSON");
479 return OC_STACK_NO_MEMORY;
481 OIC_LOG(DEBUG, TAG, "Created payload for CRL:");
482 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
484 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
485 if(!PMGenerateQuery(true,
486 selectedDeviceInfo->endpoint.addr,
487 selectedDeviceInfo->securePort,
488 selectedDeviceInfo->connType,
489 query, sizeof(query), OIC_RSRC_CRL_URI))
491 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
492 OCPayloadDestroy((OCPayload *)secPayload);
493 return OC_STACK_ERROR;
495 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
497 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
498 cbData.cb = &SRPProvisionCRLCB;
499 CRLData_t *crlData = (CRLData_t *) OICCalloc(1, sizeof(CRLData_t));
502 OCPayloadDestroy((OCPayload *)secPayload);
503 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
504 return OC_STACK_NO_MEMORY;
507 crlData->deviceInfo = selectedDeviceInfo;
508 crlData->resultCallback = resultCallback;
509 crlData->numOfResults=0;
512 crlData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
513 if (crlData->resArr == NULL)
515 OCPayloadDestroy((OCPayload *)secPayload);
516 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
517 return OC_STACK_NO_MEMORY;
520 cbData.context = (void *)crlData;
522 OCMethod method = OC_REST_POST;
523 OCDoHandle handle = NULL;
524 OIC_LOG(DEBUG, TAG, "Sending CRL info to resource server");
526 OCStackResult ret = OCDoResource(&handle, method, query,
527 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
528 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
530 if (ret != OC_STACK_OK)
532 OICFree(crlData->resArr);
540 * Internal function for handling credential generation and sending cretificate credential.
542 * @param[in] cred Instance of cred resource.
543 * @param[in] deviceInfo information about device to which credential is to be provisioned.
544 * @param[in] responseHandler callbak called by OC stack when request API receives response.
545 * @return OC_STACK_OK in case of success and other value otherwise.
547 static OCStackResult provisionCertCred(const OicSecCred_t *cred,
548 const OCProvisionDev_t *deviceInfo, CertData_t *certData,
549 OCClientResponseHandler responseHandler)
551 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
554 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
555 return OC_STACK_NO_MEMORY;
557 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
559 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
560 &secPayload->payloadSize, secureFlag);
562 if ((OC_STACK_OK != res) || (NULL == secPayload->securityData))
565 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
566 return OC_STACK_NO_MEMORY;
569 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
570 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
571 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
572 if(!PMGenerateQuery(true,
573 deviceInfo->endpoint.addr,
574 deviceInfo->securePort,
575 deviceInfo->connType,
576 query, sizeof(query), OIC_RSRC_CRED_URI))
578 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
579 OCPayloadDestroy((OCPayload *)secPayload);
580 return OC_STACK_ERROR;
582 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
584 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
585 cbData.cb = responseHandler;
586 cbData.context = (void *) certData;
589 OCDoHandle handle = NULL;
590 OCMethod method = OC_REST_POST;
591 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
592 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
593 OIC_LOG_V(INFO, TAG, "OCDoResource::Certificate provisioning returned : %d",ret);
594 if (ret != OC_STACK_OK)
596 OIC_LOG(ERROR, TAG, "OCStack resource error");
603 * Callback handler for handling callback of certificate provisioning device.
605 * @param[in] ctx ctx value passed to callback from calling function.
606 * @param[in] UNUSED handle to an invocation
607 * @param[in] clientResponse Response from queries to remote servers.
608 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
609 * and OC_STACK_KEEP_TRANSACTION to keep it.
611 static OCStackApplicationResult provisionCertCB(void *ctx, OCDoHandle UNUSED,
612 OCClientResponse *clientResponse)
614 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
615 CertData_t *certData = (CertData_t *) ctx;
618 OCProvisionResultCB resultCallback = certData->resultCallback;
619 OIC_LOG(INFO, TAG, "provisionCertCred called");
622 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
624 registerResultForCertProvisioning(certData, OC_STACK_RESOURCE_CREATED);
625 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
628 OICFree(certData->resArr);
630 return OC_STACK_DELETE_TRANSACTION;
634 OIC_LOG(INFO, TAG, "provisionCertCredCB received Null clientResponse");
635 registerResultForCertProvisioning(certData, OC_STACK_ERROR);
636 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
639 OICFree(certData->resArr);
641 return OC_STACK_DELETE_TRANSACTION;
643 #endif // __WITH_X509__
645 OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
646 const OCProvisionDev_t *pDev1,
647 const OCProvisionDev_t *pDev2,
648 OCProvisionResultCB resultCallback)
650 VERIFY_NON_NULL(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM);
651 if (SYMMETRIC_PAIR_WISE_KEY == type)
653 VERIFY_NON_NULL(TAG, pDev2, ERROR, OC_STACK_INVALID_PARAM);
657 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
658 return OC_STACK_INVALID_CALLBACK;
660 if (SYMMETRIC_PAIR_WISE_KEY == type &&
661 0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
663 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
664 return OC_STACK_INVALID_PARAM;
667 if (SYMMETRIC_PAIR_WISE_KEY == type &&
668 !(OWNER_PSK_LENGTH_128 == keySize || OWNER_PSK_LENGTH_256 == keySize))
670 OIC_LOG(INFO, TAG, "Invalid key size");
671 return OC_STACK_INVALID_PARAM;
674 OIC_LOG(INFO, TAG, "In SRPProvisionCredentials");
676 if (SYMMETRIC_PAIR_WISE_KEY == type)
678 bool linkExisits = true;
679 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExisits);
681 if (res != OC_STACK_OK)
683 OIC_LOG(ERROR, TAG, "Internal error occured");
688 OIC_LOG(ERROR, TAG, "Link already exists");
689 return OC_STACK_INVALID_PARAM;
693 OicUuid_t provTooldeviceID = {{0,}};
694 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
696 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
697 return OC_STACK_ERROR;
699 OIC_LOG(INFO, TAG, "retrieved deviceid");
702 case SYMMETRIC_PAIR_WISE_KEY:
704 const OCProvisionDev_t *firstDevice = pDev1;
705 const OCProvisionDev_t *secondDevice = pDev2;
707 OicSecCred_t *firstCred = NULL;
708 OicSecCred_t *secondCred = NULL;
709 OCStackResult res = PMGeneratePairWiseCredentials(type, keySize, &provTooldeviceID,
710 &firstDevice->doxm->deviceID, &secondDevice->doxm->deviceID,
711 &firstCred, &secondCred);
712 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
713 OIC_LOG(INFO, TAG, "Credentials generated successfully");
714 CredentialData_t *credData =
715 (CredentialData_t *) OICCalloc(1, sizeof(CredentialData_t));
716 if (NULL == credData)
720 OIC_LOG(ERROR, TAG, "Memory allocation problem");
721 return OC_STACK_NO_MEMORY;
723 credData->deviceInfo1 = firstDevice;
724 credData->deviceInfo2 = secondDevice;
725 credData->credInfo = secondCred;
727 credData->credInfoFirst = firstCred;
728 credData->numOfResults = 0;
729 credData->resultCallback = resultCallback;
730 // first call to provision creds to device1.
731 // second call to provision creds to device2.
734 (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
735 if (NULL == credData->resArr)
740 OIC_LOG(ERROR, TAG, "Memory allocation problem");
741 return OC_STACK_NO_MEMORY;
743 res = provisionCredentials(firstCred, firstDevice, credData, &provisionCredentialCB1);
744 if (OC_STACK_OK != res)
746 DeleteCredList(firstCred);
747 DeleteCredList(secondCred);
748 OICFree(credData->resArr);
751 OIC_LOG_V(INFO, TAG, "provisionCredentials returned: %d",res);
752 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
756 case SIGNED_ASYMMETRIC_KEY:
758 const OCProvisionDev_t *firstDevice = pDev1;
759 OicSecCred_t *cred = NULL;
760 OCStackResult res = PMGenerateCertificateCredentials(&provTooldeviceID,
761 &firstDevice->doxm->deviceID,&cred);
762 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
763 OIC_LOG(INFO, TAG, "Certificate credentials generated successfully");
764 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
765 if (NULL == certData)
768 OIC_LOG(ERROR, TAG, "Memory allocation problem");
769 return OC_STACK_NO_MEMORY;
772 certData->deviceInfo = firstDevice;
774 certData->credInfo = cred;
775 certData->numOfResults = 0;
776 certData->resultCallback = resultCallback;
778 certData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
779 if (NULL == certData->resArr)
781 DeleteCredList(cred);
783 OIC_LOG(ERROR, TAG, "Memory allocation problem");
784 return OC_STACK_NO_MEMORY;
787 res = provisionCertCred(cred, firstDevice, certData, &provisionCertCB);
788 if (OC_STACK_OK != res)
790 OICFree(certData->resArr);
793 DeleteCredList(cred);
794 OIC_LOG_V(INFO, TAG, "provisionCertCredentials returned: %d",res);
801 OIC_LOG(ERROR, TAG, "Invalid option.");
802 return OC_STACK_INVALID_PARAM;
805 return OC_STACK_ERROR;
809 * Internal Function to store results in result array during ACL provisioning.
811 static void registerResultForACLProvisioning(ACLData_t *aclData,
812 OCStackResult stackresult)
814 OIC_LOG_V(INFO, TAG, "Inside registerResultForACLProvisioning aclData->numOfResults is %d\n",
815 aclData->numOfResults);
816 memcpy(aclData->resArr[(aclData->numOfResults)].deviceId.id,
817 aclData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
818 aclData->resArr[(aclData->numOfResults)].res = stackresult;
819 ++(aclData->numOfResults);
823 * Callback handler of SRPProvisionACL.
825 * @param[in] ctx ctx value passed to callback from calling function.
826 * @param[in] UNUSED handle to an invocation
827 * @param[in] clientResponse Response from queries to remote servers.
828 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
829 * and OC_STACK_KEEP_TRANSACTION to keep it.
831 static OCStackApplicationResult SRPProvisionACLCB(void *ctx, OCDoHandle UNUSED,
832 OCClientResponse *clientResponse)
834 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionACLCB.");
836 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
837 ACLData_t *aclData = (ACLData_t*)ctx;
838 OCProvisionResultCB resultCallback = aclData->resultCallback;
842 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
844 registerResultForACLProvisioning(aclData, OC_STACK_RESOURCE_CREATED);
845 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
848 OICFree(aclData->resArr);
850 return OC_STACK_DELETE_TRANSACTION;
853 registerResultForACLProvisioning(aclData, OC_STACK_ERROR);
854 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
857 OIC_LOG_V(ERROR, TAG, "SRPProvisionACLCB received Null clientResponse");
858 OICFree(aclData->resArr);
860 return OC_STACK_DELETE_TRANSACTION;
863 OCStackResult SRPProvisionACL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
864 OicSecAcl_t *acl, OCProvisionResultCB resultCallback)
866 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
867 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
868 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
870 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
873 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
874 return OC_STACK_NO_MEMORY;
876 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
877 if(OC_STACK_OK != AclToCBORPayload(acl, &secPayload->securityData, &secPayload->payloadSize))
879 OCPayloadDestroy((OCPayload *)secPayload);
880 OIC_LOG(ERROR, TAG, "Failed to AclToCBORPayload");
881 return OC_STACK_NO_MEMORY;
883 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
884 if(!PMGenerateQuery(true,
885 selectedDeviceInfo->endpoint.addr,
886 selectedDeviceInfo->securePort,
887 selectedDeviceInfo->connType,
888 query, sizeof(query), OIC_RSRC_ACL_URI))
890 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
891 return OC_STACK_ERROR;
893 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
895 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
896 cbData.cb = &SRPProvisionACLCB;
897 ACLData_t *aclData = (ACLData_t *) OICCalloc(1, sizeof(ACLData_t));
900 OCPayloadDestroy((OCPayload *)secPayload);
901 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
902 return OC_STACK_NO_MEMORY;
904 aclData->deviceInfo = selectedDeviceInfo;
905 aclData->resultCallback = resultCallback;
906 aclData->numOfResults=0;
908 // call to provision ACL to device1.
910 aclData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
911 if (aclData->resArr == NULL)
914 OCPayloadDestroy((OCPayload *)secPayload);
915 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
916 return OC_STACK_NO_MEMORY;
918 cbData.context = (void *)aclData;
920 OCMethod method = OC_REST_POST;
921 OCDoHandle handle = NULL;
922 OIC_LOG(DEBUG, TAG, "Sending ACL info to resource server");
923 OCStackResult ret = OCDoResource(&handle, method, query,
924 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
925 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
926 if (ret != OC_STACK_OK)
928 OICFree(aclData->resArr);
931 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
936 * Internal Function to store results in result array during Direct-Pairing provisioning.
938 static void registerResultForDirectPairingProvisioning(PconfData_t *pconfData,
939 OCStackResult stackresult)
941 OIC_LOG_V(INFO, TAG, "Inside registerResultForDirectPairingProvisioning "
942 "pconfData->numOfResults is %d\n", pconfData->numOfResults);
943 memcpy(pconfData->resArr[(pconfData->numOfResults)].deviceId.id,
944 pconfData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
945 pconfData->resArr[(pconfData->numOfResults)].res = stackresult;
946 ++(pconfData->numOfResults);
950 * Callback handler of SRPProvisionDirectPairing.
952 * @param[in] ctx ctx value passed to callback from calling function.
953 * @param[in] UNUSED handle to an invocation
954 * @param[in] clientResponse Response from queries to remote servers.
955 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
956 * and OC_STACK_KEEP_TRANSACTION to keep it.
958 static OCStackApplicationResult SRPProvisionDirectPairingCB(void *ctx, OCDoHandle UNUSED,
959 OCClientResponse *clientResponse)
961 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionDirectPairingCB.");
963 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
964 PconfData_t *pconfData = (PconfData_t*)ctx;
965 OCProvisionResultCB resultCallback = pconfData->resultCallback;
969 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
971 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_OK);
972 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
975 OICFree(pconfData->resArr);
977 return OC_STACK_DELETE_TRANSACTION;
980 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_ERROR);
981 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
984 OIC_LOG_V(ERROR, TAG, "SRPProvisionDirectPairingCB received Null clientResponse");
985 OICFree(pconfData->resArr);
987 return OC_STACK_DELETE_TRANSACTION;
990 OCStackResult SRPProvisionDirectPairing(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
991 OicSecPconf_t *pconf, OCProvisionResultCB resultCallback)
993 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
994 VERIFY_NON_NULL(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
995 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
997 // check direct-pairing capability
998 if (true != selectedDeviceInfo->doxm->dpc)
1000 OIC_LOG(ERROR, TAG, "Resouce server does not have Direct-Pairing Capability ");
1001 return OC_STACK_UNAUTHORIZED_REQ;
1004 OicUuid_t provTooldeviceID = {.id={0}};
1005 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1007 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1008 return OC_STACK_ERROR;
1010 memcpy(&pconf->rownerID, &provTooldeviceID, sizeof(OicUuid_t));
1012 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1015 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1016 return OC_STACK_NO_MEMORY;
1018 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1020 if (OC_STACK_OK != PconfToCBORPayload(pconf, &(secPayload->securityData),
1021 &(secPayload->payloadSize)))
1023 OCPayloadDestroy((OCPayload*)secPayload);
1024 OIC_LOG(ERROR, TAG, "Failed to PconfToCborPayload");
1025 return OC_STACK_NO_MEMORY;
1027 OIC_LOG(DEBUG, TAG, "Created payload for pconf set");
1028 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1031 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1032 if(!PMGenerateQuery(true,
1033 selectedDeviceInfo->endpoint.addr,
1034 selectedDeviceInfo->securePort,
1035 selectedDeviceInfo->connType,
1036 query, sizeof(query), OIC_RSRC_PCONF_URI))
1038 OIC_LOG(ERROR, TAG, "SRPProvisionDirectPairing : Failed to generate query");
1039 return OC_STACK_ERROR;
1041 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1043 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1044 cbData.cb = &SRPProvisionDirectPairingCB;
1045 PconfData_t *pconfData = (PconfData_t *) OICCalloc(1, sizeof(PconfData_t));
1046 if (NULL == pconfData)
1048 OCPayloadDestroy((OCPayload*)secPayload);
1049 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1050 return OC_STACK_NO_MEMORY;
1052 pconfData->deviceInfo = selectedDeviceInfo;
1053 pconfData->resultCallback = resultCallback;
1054 pconfData->numOfResults=0;
1055 pconfData->ctx = ctx;
1056 // call to provision PCONF to device1.
1057 int noOfRiCalls = 1;
1058 pconfData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1059 if (NULL == pconfData->resArr)
1062 OCPayloadDestroy((OCPayload*)secPayload);
1063 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1064 return OC_STACK_NO_MEMORY;
1066 cbData.context = (void *)pconfData;
1068 OCMethod method = OC_REST_POST;
1069 OCDoHandle handle = NULL;
1070 OIC_LOG(DEBUG, TAG, "Sending PCONF info to resource server");
1071 OCStackResult ret = OCDoResource(&handle, method, query,
1072 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1073 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1074 if (OC_STACK_OK != ret)
1076 OICFree(pconfData->resArr);
1079 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1083 static void DeleteUnlinkData_t(UnlinkData_t *unlinkData)
1087 OICFree(unlinkData->unlinkDev);
1088 OICFree(unlinkData->unlinkRes);
1089 OICFree(unlinkData);
1093 static void registerResultForUnlinkDevices(UnlinkData_t *unlinkData, OCStackResult stackresult,
1096 if (NULL != unlinkData)
1098 OIC_LOG_V(INFO, TAG, "Inside registerResultForUnlinkDevices unlinkData->numOfResults is %d\n",
1099 unlinkData->numOfResults);
1100 OIC_LOG_V(INFO, TAG, "Stack result :: %d", stackresult);
1102 OicUuid_t *pUuid = &unlinkData->unlinkRes[(unlinkData->numOfResults)].deviceId;
1104 // Set result in the result array according to the position (devNum).
1105 if (idx != IDX_DB_UPDATE_RES)
1107 memcpy(pUuid->id, unlinkData->unlinkDev[idx].doxm->deviceID.id, sizeof(pUuid->id));
1110 { // When deivce ID is 000... this means it's the result of database update.
1111 memset(pUuid->id, 0, sizeof(pUuid->id));
1113 unlinkData->unlinkRes[(unlinkData->numOfResults)].res = stackresult;
1114 ++(unlinkData->numOfResults);
1115 OIC_LOG (INFO, TAG, "Out registerResultForUnlinkDevices");
1119 static OCStackResult SendDeleteCredentialRequest(void* ctx,
1120 OCClientResponseHandler respHandler,
1121 const OCProvisionDev_t* revokedDev,
1122 const OCProvisionDev_t* destDev)
1124 OIC_LOG(DEBUG, TAG, "IN SendDeleteCredentialRequest");
1126 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1128 return OC_STACK_INVALID_PARAM;
1132 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1133 if(OC_STACK_OK != ret)
1135 OIC_LOG(ERROR, TAG, "SendDeleteCredentialRequest : Failed to canonical UUID encoding");
1136 return OC_STACK_ERROR;
1139 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1141 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1142 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, destDev->endpoint.addr,
1143 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1147 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1148 return OC_STACK_ERROR;
1150 else if ((size_t)snRet >= sizeof(reqBuf))
1152 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1153 return OC_STACK_ERROR;
1156 OCCallbackData cbData;
1157 memset(&cbData, 0, sizeof(cbData));
1158 cbData.context = ctx;
1159 cbData.cb = respHandler;
1161 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1163 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1165 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1166 &destDev->endpoint, NULL,
1167 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1168 if (OC_STACK_OK != ret)
1170 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1172 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1178 * Callback handler of unlink second device.
1180 * @param[in] ctx ctx value passed to callback from calling function.
1181 * @param[in] handle handle to an invocation
1182 * @param[in] clientResponse Response from queries to remote servers.
1183 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1184 * OC_STACK_KEEP_TRANSACTION to keep it.
1186 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1187 OCClientResponse *clientResponse)
1190 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1191 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1192 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1196 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1197 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1199 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1201 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1205 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1206 unlinkData->resultCallback(unlinkData->ctx,
1207 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1213 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1214 IDX_SECOND_DEVICE_RES);
1215 unlinkData->resultCallback(unlinkData->ctx,
1216 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1217 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1221 //Update provisioning DB when succes case.
1222 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1223 &unlinkData->unlinkDev[1].doxm->deviceID))
1225 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1226 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1227 unlinkData->resultCallback(unlinkData->ctx,
1228 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1231 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1235 DeleteUnlinkData_t(unlinkData);
1236 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1237 return OC_STACK_DELETE_TRANSACTION;
1242 * Callback handler of unlink first device.
1244 * @param[in] ctx ctx value passed to callback from calling function.
1245 * @param[in] handle handle to an invocation
1246 * @param[in] clientResponse Response from queries to remote servers.
1247 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1248 * OC_STACK_KEEP_TRANSACTION to keep it.
1250 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1251 OCClientResponse *clientResponse)
1253 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1254 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1255 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1260 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1261 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1263 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1265 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1267 // Second revocation request to second device.
1268 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1269 &unlinkData->unlinkDev[0],
1270 &unlinkData->unlinkDev[1] /*Dest*/);
1271 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1272 if (OC_STACK_OK != res)
1274 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1275 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1276 IDX_SECOND_DEVICE_RES);
1277 unlinkData->resultCallback(unlinkData->ctx,
1278 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1283 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1284 return OC_STACK_DELETE_TRANSACTION;
1289 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1291 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1292 unlinkData->unlinkRes, true);
1298 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1299 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1300 IDX_FIRST_DEVICE_RES );
1301 unlinkData->resultCallback(unlinkData->ctx,
1302 unlinkData->numOfResults, unlinkData->unlinkRes,
1304 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1308 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1309 DeleteUnlinkData_t(unlinkData);
1310 return OC_STACK_DELETE_TRANSACTION;
1314 * Function to unlink devices.
1315 * This function will remove the credential & relationship between the two devices.
1317 * @param[in] ctx Application context would be returned in result callback
1318 * @param[in] pTargetDev1 first device information to be unlinked.
1319 * @param[in] pTargetDev2 second device information to be unlinked.
1320 * @param[in] resultCallback callback provided by API user, callback will be called when
1321 * device unlink is finished.
1322 * @return OC_STACK_OK in case of success and other value otherwise.
1324 OCStackResult SRPUnlinkDevices(void* ctx,
1325 const OCProvisionDev_t* pTargetDev1,
1326 const OCProvisionDev_t* pTargetDev2,
1327 OCProvisionResultCB resultCallback)
1329 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1331 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1333 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1334 return OC_STACK_INVALID_PARAM;
1336 if (!resultCallback)
1338 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1339 return OC_STACK_INVALID_CALLBACK;
1341 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1343 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1344 return OC_STACK_INVALID_PARAM;
1347 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1348 PMPrintOCProvisionDev(pTargetDev1);
1349 PMPrintOCProvisionDev(pTargetDev2);
1351 // Mark the link status stale
1352 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1353 if (OC_STACK_OK != res)
1355 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1359 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1360 VERIFY_NON_NULL(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1362 //Initialize unlink data
1363 unlinkData->ctx = ctx;
1364 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1365 if (NULL == unlinkData->unlinkDev)
1367 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1368 res = OC_STACK_NO_MEMORY;
1372 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1373 if (NULL == unlinkData->unlinkRes)
1375 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1376 res = OC_STACK_NO_MEMORY;
1380 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1381 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1383 unlinkData->numOfResults = 0;
1384 unlinkData->resultCallback = resultCallback;
1386 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1387 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1388 if (OC_STACK_OK != res)
1390 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1397 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1398 DeleteUnlinkData_t(unlinkData);
1402 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1406 OICFree(pRemoveData->revokeTargetDev);
1407 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1408 OICFree(pRemoveData->removeRes);
1409 OICFree(pRemoveData);
1413 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1414 OCStackResult stackresult, bool hasError)
1416 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1417 removeData->numOfResults + 1);
1420 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1421 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1425 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1426 0, sizeof(pLinkedDevId->id) );
1428 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1429 removeData->hasError = hasError;
1430 ++(removeData->numOfResults);
1432 // If we get suffcient result from linked devices, we have to call user callback and do free
1433 if (removeData->sizeOfResArray == removeData->numOfResults)
1435 if(!removeData->hasError)
1437 // Remove device info from prvisioning database
1438 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1440 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1441 removeData->hasError = true;
1444 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1445 removeData->hasError);
1446 DeleteRemoveData_t(removeData);
1451 * Callback handler of unlink first device.
1453 * @param[in] ctx ctx value passed to callback from calling function.
1454 * @param[in] handle handle to an invocation
1455 * @param[in] clientResponse Response from queries to remote servers.
1456 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1457 * and OC_STACK_KEEP_TRANSACTION to keep it.
1459 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1460 OCClientResponse *clientResponse)
1462 //Update the delete credential into delete device context
1463 //Save the deleted status in delDevCtx
1465 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1466 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1467 OCStackResult res = OC_STACK_ERROR;
1469 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1473 OicUuid_t revDevUuid = {.id={0}};
1474 if(UUID_LENGTH == clientResponse->identity.id_length)
1476 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1477 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1479 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1480 if (OC_STACK_OK != res)
1482 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1483 registerResultForRemoveDevice(removeData, &revDevUuid,
1484 OC_STACK_INCONSISTENT_DB, true);
1486 return OC_STACK_DELETE_TRANSACTION;
1489 registerResultForRemoveDevice(removeData, &revDevUuid,
1490 OC_STACK_RESOURCE_DELETED, false);
1494 registerResultForRemoveDevice(removeData, &revDevUuid,
1495 clientResponse->result, true);
1496 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1501 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1502 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1504 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1507 * Since server's credential was deleted,
1508 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1510 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1511 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1515 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1521 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1522 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1526 return OC_STACK_DELETE_TRANSACTION;
1529 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1530 OCProvisionDev_t* pOwnedDevList,
1531 OCUuidList_t* pLinkedUuidList,
1532 OCProvisionDev_t** ppLinkedDevList,
1533 size_t *numOfLinkedDev)
1535 // pOwnedDevList could be NULL. It means no alived and owned device now.
1536 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1537 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1539 return OC_STACK_INVALID_PARAM;
1543 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1544 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1546 // Mark the link status stale.
1547 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1548 if (OC_STACK_OK != res)
1550 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1551 return OC_STACK_INCONSISTENT_DB;
1556 // If this linked device is alive (power-on), add the deivce to the list.
1557 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1558 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1560 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1562 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1563 if (NULL == targetDev)
1565 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1566 return OC_STACK_NO_MEMORY;
1569 LL_PREPEND(*ppLinkedDevList, targetDev);
1576 *numOfLinkedDev = cnt;
1581 * Function to device revocation
1582 * This function will remove credential of target device from all devices in subnet.
1584 * @param[in] ctx Application context would be returned in result callback
1585 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1586 * @param[in] pTargetDev Device information to be revoked.
1587 * @param[in] resultCallback callback provided by API user, callback will be called when
1588 * credential revocation is finished.
1589 * @return OC_STACK_OK in case of success and other value otherwise.
1590 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1591 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1593 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1594 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1596 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1598 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1600 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1601 return OC_STACK_INVALID_PARAM;
1603 if (!resultCallback)
1605 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
1606 return OC_STACK_INVALID_CALLBACK;
1609 // Declare variables in here to handle error cases with goto statement.
1610 OCProvisionDev_t* pOwnedDevList = NULL;
1611 OCProvisionDev_t* pLinkedDevList = NULL;
1612 RemoveData_t* removeData = NULL;
1614 //1. Find all devices that has a credential of the revoked device
1615 OCUuidList_t* pLinkedUuidList = NULL;
1616 size_t numOfDevices = 0;
1617 OCStackResult res = OC_STACK_ERROR;
1618 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
1619 if (OC_STACK_OK != res)
1621 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
1624 // if there is no related device, we can skip further process.
1625 if (0 == numOfDevices)
1627 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
1628 res = OC_STACK_CONTINUE;
1632 //2. Find owned device from the network
1633 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
1634 if (OC_STACK_OK != res)
1636 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
1640 //3. Make a list of devices to send DELETE credential request
1641 // by comparing owned devices from provisioning database with mutlicast discovery result.
1642 size_t numOfLinkedDev = 0;
1643 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
1644 &pLinkedDevList, &numOfLinkedDev);
1645 if (OC_STACK_OK != res)
1647 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
1650 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
1651 { // So we don't have to send request message.
1652 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
1653 res = OC_STACK_CONTINUE;
1657 // 4. Prepare RemoveData Context data.
1658 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
1661 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1662 res = OC_STACK_NO_MEMORY;
1666 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
1667 if (!removeData->revokeTargetDev)
1669 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
1670 res = OC_STACK_NO_MEMORY;
1674 removeData->removeRes =
1675 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
1676 if (!removeData->removeRes)
1678 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1679 res = OC_STACK_NO_MEMORY;
1683 removeData->ctx = ctx;
1684 removeData->linkedDevList = pLinkedDevList;
1685 removeData->resultCallback = resultCallback;
1686 removeData->numOfResults = 0;
1687 removeData->sizeOfResArray = numOfLinkedDev;
1688 removeData->hasError = false;
1690 // 5. Send DELETE credential request to linked devices.
1691 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1692 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
1693 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
1695 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
1696 removeData->revokeTargetDev, curDev);
1697 if (OC_STACK_OK != res)
1699 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
1700 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
1704 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
1708 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
1709 PMDeleteDeviceList(pOwnedDevList);
1710 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
1712 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
1715 PDMDestoryOicUuidLinkList(pLinkedUuidList);
1716 PMDeleteDeviceList(pOwnedDevList);
1717 PMDeleteDeviceList(pLinkedDevList);
1720 OICFree(removeData->revokeTargetDev);
1721 OICFree(removeData->removeRes);
1722 OICFree(removeData);
1724 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");
1729 * Internal Function to store results in result array during GetCredResourceCB.
1731 static void registerResultForGetCredResourceCB(GetSecData_t *GetSecData,
1732 OCStackResult stackresult)
1734 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetCredResourceCB "
1735 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
1736 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
1737 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
1738 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
1739 ++(GetSecData->numOfResults);
1743 * Callback handler of SRPGetCredResource.
1745 * @param[in] ctx ctx value passed to callback from calling function.
1746 * @param[in] UNUSED handle to an invocation
1747 * @param[in] clientResponse Response from queries to remote servers.
1748 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1749 * and OC_STACK_KEEP_TRANSACTION to keep it.
1751 static OCStackApplicationResult SRPGetCredResourceCB(void *ctx, OCDoHandle UNUSED,
1752 OCClientResponse *clientResponse)
1754 OIC_LOG_V(INFO, TAG, "Inside SRPGetCredResourceCB.");
1756 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
1757 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
1758 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
1762 if(OC_STACK_OK == clientResponse->result)
1764 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
1765 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
1767 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
1769 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
1770 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
1773 OICFree(GetSecData->resArr);
1774 OICFree(GetSecData);
1776 return OC_STACK_DELETE_TRANSACTION;
1779 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
1780 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
1783 OIC_LOG_V(ERROR, TAG, "SRPGetCredResourceCB received Null clientResponse");
1784 OICFree(GetSecData->resArr);
1785 OICFree(GetSecData);
1787 return OC_STACK_DELETE_TRANSACTION;
1791 OCStackResult SRPGetCredResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
1792 OCProvisionResultCB resultCallback)
1794 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
1795 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1797 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1798 if(!PMGenerateQuery(true,
1799 selectedDeviceInfo->endpoint.addr,
1800 selectedDeviceInfo->securePort,
1801 selectedDeviceInfo->connType,
1802 query, sizeof(query), OIC_RSRC_CRED_URI))
1804 OIC_LOG(ERROR, TAG, "SRPGetCredResource : Failed to generate query");
1805 return OC_STACK_ERROR;
1807 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1809 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1810 cbData.cb = &SRPGetCredResourceCB;
1811 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
1812 if (NULL == GetSecData)
1814 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1815 return OC_STACK_NO_MEMORY;
1817 GetSecData->deviceInfo = selectedDeviceInfo;
1818 GetSecData->resultCallback = resultCallback;
1819 GetSecData->numOfResults=0;
1820 GetSecData->ctx = ctx;
1822 int noOfRiCalls = 1;
1823 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1824 if (NULL == GetSecData->resArr)
1826 OICFree(GetSecData);
1827 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1828 return OC_STACK_NO_MEMORY;
1830 cbData.context = (void *)GetSecData;
1832 OCMethod method = OC_REST_GET;
1833 OCDoHandle handle = NULL;
1834 OIC_LOG(DEBUG, TAG, "Sending Get Cred to resource server");
1835 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
1836 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1837 if (OC_STACK_OK != ret)
1839 OIC_LOG(ERROR, TAG, "OCStack resource error");
1840 OICFree(GetSecData->resArr);
1841 OICFree(GetSecData);
1843 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1844 OIC_LOG(DEBUG, TAG, "OUT SRPGetCredResource");
1850 * Internal Function to store results in result array during GetACLResourceCB.
1852 static void registerResultForGetACLResourceCB(GetSecData_t *GetSecData,
1853 OCStackResult stackresult)
1855 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetACLResourceCB "
1856 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
1857 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
1858 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
1859 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
1860 ++(GetSecData->numOfResults);
1864 * Callback handler of SRPGetACLResource.
1866 * @param[in] ctx ctx value passed to callback from calling function.
1867 * @param[in] UNUSED handle to an invocation
1868 * @param[in] clientResponse Response from queries to remote servers.
1869 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1870 * and OC_STACK_KEEP_TRANSACTION to keep it.
1872 static OCStackApplicationResult SRPGetACLResourceCB(void *ctx, OCDoHandle UNUSED,
1873 OCClientResponse *clientResponse)
1875 OIC_LOG_V(INFO, TAG, "Inside SRPGetACLResourceCB.");
1877 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
1878 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
1879 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
1883 if(OC_STACK_OK == clientResponse->result)
1885 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
1886 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
1888 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
1890 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
1891 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
1894 OICFree(GetSecData->resArr);
1895 OICFree(GetSecData);
1897 return OC_STACK_DELETE_TRANSACTION;
1900 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
1901 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
1904 OIC_LOG_V(ERROR, TAG, "SRPGetACLResourceCB received Null clientResponse");
1905 OICFree(GetSecData->resArr);
1906 OICFree(GetSecData);
1908 return OC_STACK_DELETE_TRANSACTION;
1912 OCStackResult SRPGetACLResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
1913 OCProvisionResultCB resultCallback)
1915 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
1916 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1918 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1919 if(!PMGenerateQuery(true,
1920 selectedDeviceInfo->endpoint.addr,
1921 selectedDeviceInfo->securePort,
1922 selectedDeviceInfo->connType,
1923 query, sizeof(query), OIC_RSRC_ACL_URI))
1925 OIC_LOG(ERROR, TAG, "SRPGetACLResource : Failed to generate query");
1926 return OC_STACK_ERROR;
1928 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1930 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1931 cbData.cb = &SRPGetACLResourceCB;
1932 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
1933 if (NULL == GetSecData)
1935 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1936 return OC_STACK_NO_MEMORY;
1938 GetSecData->deviceInfo = selectedDeviceInfo;
1939 GetSecData->resultCallback = resultCallback;
1940 GetSecData->numOfResults=0;
1941 GetSecData->ctx = ctx;
1943 int noOfRiCalls = 1;
1944 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1945 if (NULL == GetSecData->resArr)
1947 OICFree(GetSecData);
1948 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1949 return OC_STACK_NO_MEMORY;
1951 cbData.context = (void *)GetSecData;
1953 OCMethod method = OC_REST_GET;
1954 OCDoHandle handle = NULL;
1955 OIC_LOG(DEBUG, TAG, "Sending Get ACL to resource server");
1956 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
1957 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1958 if (OC_STACK_OK != ret)
1960 OIC_LOG(ERROR, TAG, "OCStack resource error");
1961 OICFree(GetSecData->resArr);
1962 OICFree(GetSecData);
1964 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1965 OIC_LOG(DEBUG, TAG, "OUT SRPGetACLResource");