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.**/
94 // Structure to carry get security resource APIs data to callback.
95 typedef struct GetSecData GetSecData_t;
98 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
99 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
100 OCProvisionResult_t *resArr; /**< Result array.**/
101 int numOfResults; /**< Number of results in result array.**/
105 * Structure to carry PCONF provision API data to callback.
107 typedef struct PconfData PconfData_t;
110 void *ctx; /**< Pointer to user context.**/
111 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
112 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
113 OCProvisionResult_t *resArr; /**< Result array.**/
114 int numOfResults; /**< Number of results in result array.**/
117 // Enum type index for unlink callback.
119 IDX_FIRST_DEVICE_RES = 0, // index for resulf of the first device
120 IDX_SECOND_DEVICE_RES, // index for result of the second device
121 IDX_DB_UPDATE_RES // index for result of updating provisioning database.
124 // Structure to carry unlink APIs data to callback.
125 typedef struct UnlinkData UnlinkData_t;
128 OCProvisionDev_t* unlinkDev; /**< Pointer to OCProvisionDev_t to be unlinked.**/
129 OCProvisionResult_t* unlinkRes; /**< Result array.**/
130 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
131 int numOfResults; /**< Number of results in result array.**/
134 //Example of DELETE cred request -> coaps://0.0.0.0:5684/oic/sec/cred?sub=(BASE64 ENCODED UUID)
135 const char * SRP_FORM_DELETE_CREDENTIAL = "coaps://[%s]:%d%s?%s=%s";
137 // Structure to carry remove APIs data to callback.
138 typedef struct RemoveData RemoveData_t;
141 OCProvisionDev_t* revokeTargetDev; /**< Device which is going to be revoked..**/
142 OCProvisionDev_t* linkedDevList; /**< A list of devices which have invalid credential.**/
143 OCProvisionResult_t* removeRes; /**< Result array.**/
144 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
145 size_t numOfResults; /**< Number of results in result array.**/
146 size_t sizeOfResArray;
153 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
154 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
155 OCClientResponseHandler responseHandler);
159 * Internal function to update result in result array.
161 static void registerResultForCredProvisioning(CredentialData_t *credData,
162 OCStackResult stackresult, int cause)
165 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",credData->numOfResults);
168 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
169 credData->deviceInfo1->doxm->deviceID.id,UUID_LENGTH);
173 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
174 credData->deviceInfo2->doxm->deviceID.id,UUID_LENGTH);
176 credData->resArr[(credData->numOfResults)].res = stackresult;
177 ++(credData->numOfResults);
181 * Callback handler for handling callback of provisioning device 2.
183 * @param[in] ctx ctx value passed to callback from calling function.
184 * @param[in] UNUSED handle to an invocation
185 * @param[in] clientResponse Response from queries to remote servers.
186 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
187 * and OC_STACK_KEEP_TRANSACTION to keep it.
189 static OCStackApplicationResult provisionCredentialCB2(void *ctx, OCDoHandle UNUSED,
190 OCClientResponse *clientResponse)
192 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
193 CredentialData_t *credData = (CredentialData_t *) ctx;
196 OCProvisionResultCB resultCallback = credData->resultCallback;
197 OIC_LOG(INFO, TAG, "provisionCredentialCB2 called");
200 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
202 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CREATED, 2);
203 OCStackResult res = PDMLinkDevices(&credData->deviceInfo1->doxm->deviceID,
204 &credData->deviceInfo2->doxm->deviceID);
205 if (OC_STACK_OK != res)
207 OIC_LOG(ERROR, TAG, "Error occured on PDMLinkDevices");
208 return OC_STACK_DELETE_TRANSACTION;
210 OIC_LOG(INFO, TAG, "Link created successfully");
212 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
215 OICFree(credData->resArr);
217 return OC_STACK_DELETE_TRANSACTION;
221 OIC_LOG(INFO, TAG, "provisionCredentialCB2 received Null clientResponse");
222 registerResultForCredProvisioning(credData, OC_STACK_ERROR, 2);
223 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
226 OICFree(credData->resArr);
228 return OC_STACK_DELETE_TRANSACTION;
232 * Callback handler for handling callback of provisioning device 1.
234 * @param[in] ctx ctx value passed to callback from calling function.
235 * @param[in] UNUSED handle to an invocation
236 * @param[in] clientResponse Response from queries to remote servers.
237 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
238 * and OC_STACK_KEEP_TRANSACTION to keep it.
240 static OCStackApplicationResult provisionCredentialCB1(void *ctx, OCDoHandle UNUSED,
241 OCClientResponse *clientResponse)
243 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
245 CredentialData_t* credData = (CredentialData_t*) ctx;
246 OICFree(credData->credInfoFirst);
247 const OCProvisionDev_t *deviceInfo = credData->deviceInfo2;
248 OicSecCred_t *credInfo = credData->credInfo;
249 const OCProvisionResultCB resultCallback = credData->resultCallback;
252 if (OC_STACK_RESOURCE_CREATED == clientResponse->result)
254 // send credentials to second device
255 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CREATED,1);
256 OCStackResult res = provisionCredentials(credInfo, deviceInfo, credData,
257 provisionCredentialCB2);
258 DeleteCredList(credInfo);
259 if (OC_STACK_OK != res)
261 registerResultForCredProvisioning(credData, res,2);
262 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
265 OICFree(credData->resArr);
272 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
273 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
276 OICFree(credData->resArr);
283 OIC_LOG(INFO, TAG, "provisionCredentialCB received Null clientResponse for first device");
284 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
285 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
288 DeleteCredList(credInfo);
289 OICFree(credData->resArr);
293 return OC_STACK_DELETE_TRANSACTION;
299 * Internal function for handling credential generation and sending credential to resource server.
301 * @param[in] cred Instance of cred resource.
302 * @param[in] deviceInfo information about device to which credential is to be provisioned.
303 * @param[in] responseHandler callbak called by OC stack when request API receives response.
304 * @return OC_STACK_OK in case of success and other value otherwise.
306 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
307 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
308 OCClientResponseHandler responseHandler)
310 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
313 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
314 return OC_STACK_NO_MEMORY;
316 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
318 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
319 &secPayload->payloadSize, secureFlag);
320 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
322 OCPayloadDestroy((OCPayload *)secPayload);
323 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
324 return OC_STACK_NO_MEMORY;
327 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
328 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
329 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
330 if(!PMGenerateQuery(true,
331 deviceInfo->endpoint.addr,
332 deviceInfo->securePort,
333 deviceInfo->connType,
334 query, sizeof(query), OIC_RSRC_CRED_URI))
336 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
337 return OC_STACK_ERROR;
339 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
341 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
342 cbData.cb = responseHandler;
343 cbData.context = (void *) credData;
346 OCDoHandle handle = NULL;
347 OCMethod method = OC_REST_POST;
348 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
349 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
350 OIC_LOG_V(INFO, TAG, "OCDoResource::Credential provisioning returned : %d",ret);
351 if (ret != OC_STACK_OK)
353 OIC_LOG(ERROR, TAG, "OCStack resource error");
361 * Structure to carry certificate data to callback.
363 typedef struct CertificateData CertData_t;
364 struct CertificateData
366 void *ctx; /**< Pointer to user context.**/
367 const OCProvisionDev_t *deviceInfo; /**< Pointer to OCProvisionDev_t.**/
368 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
369 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
370 OCProvisionResult_t *resArr; /**< Result array.**/
371 int numOfResults; /**< Number of results in result array.**/
375 * Structure to carry CRL provision API data to callback.
377 typedef struct CRLData CRLData_t;
380 void *ctx; /**< Pointer to user context.**/
381 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
382 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
383 OCProvisionResult_t *resArr; /**< Result array.**/
384 int numOfResults; /**< Number of results in result array.**/
388 * Internal function to update result in result array.
390 static void registerResultForCertProvisioning(CertData_t *certData,
391 OCStackResult stackresult)
394 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",certData->numOfResults);
395 memcpy(certData->resArr[(certData->numOfResults)].deviceId.id,
396 certData->deviceInfo->doxm->deviceID.id,UUID_LENGTH);
397 certData->resArr[(certData->numOfResults)].res = stackresult;
398 ++(certData->numOfResults);
402 * Internal Function to store results in result array during ACL provisioning.
404 static void registerResultForCRLProvisioning(CRLData_t *crlData,
405 OCStackResult stackresult)
407 OIC_LOG_V(INFO, TAG, "Inside registerResultForCRLProvisioning crlData->numOfResults is %d\n",
408 crlData->numOfResults);
409 memcpy(crlData->resArr[(crlData->numOfResults)].deviceId.id,
410 crlData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
411 crlData->resArr[(crlData->numOfResults)].res = stackresult;
412 ++(crlData->numOfResults);
417 * Callback handler of SRPProvisionCRL.
419 * @param[in] ctx ctx value passed to callback from calling function.
420 * @param[in] UNUSED handle to an invocation
421 * @param[in] clientResponse Response from queries to remote servers.
422 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
423 * and OC_STACK_KEEP_TRANSACTION to keep it.
425 static OCStackApplicationResult SRPProvisionCRLCB(void *ctx, OCDoHandle UNUSED,
426 OCClientResponse *clientResponse)
428 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionCRLCB.");
430 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
431 CRLData_t *crlData = (CRLData_t*)ctx;
432 OCProvisionResultCB resultCallback = crlData->resultCallback;
436 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
438 registerResultForCRLProvisioning(crlData, OC_STACK_RESOURCE_CREATED);
439 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
442 OICFree(crlData->resArr);
444 return OC_STACK_DELETE_TRANSACTION;
447 registerResultForCRLProvisioning(crlData, OC_STACK_ERROR);
448 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
451 OIC_LOG_V(ERROR, TAG, "SRPProvisionCRLCB received Null clientResponse");
452 OICFree(crlData->resArr);
454 return OC_STACK_DELETE_TRANSACTION;
457 OCStackResult SRPProvisionCRL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
458 OicSecCrl_t *crl, OCProvisionResultCB resultCallback)
460 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
461 VERIFY_NON_NULL(TAG, crl, ERROR, OC_STACK_INVALID_PARAM);
462 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
464 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
467 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
468 return OC_STACK_NO_MEMORY;
471 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
472 OCStackResult res = CrlToCBORPayload(crl, &secPayload->securityData, &secPayload->payloadSize);
473 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
476 OIC_LOG(ERROR, TAG, "Failed to BinToCrlJSON");
477 return OC_STACK_NO_MEMORY;
479 OIC_LOG(DEBUG, TAG, "Created payload for CRL:");
480 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
482 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
483 if(!PMGenerateQuery(true,
484 selectedDeviceInfo->endpoint.addr,
485 selectedDeviceInfo->securePort,
486 selectedDeviceInfo->connType,
487 query, sizeof(query), OIC_RSRC_CRL_URI))
489 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
490 OCPayloadDestroy((OCPayload *)secPayload);
491 return OC_STACK_ERROR;
493 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
495 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
496 cbData.cb = &SRPProvisionCRLCB;
497 CRLData_t *crlData = (CRLData_t *) OICCalloc(1, sizeof(CRLData_t));
500 OCPayloadDestroy((OCPayload *)secPayload);
501 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
502 return OC_STACK_NO_MEMORY;
505 crlData->deviceInfo = selectedDeviceInfo;
506 crlData->resultCallback = resultCallback;
507 crlData->numOfResults=0;
510 crlData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
511 if (crlData->resArr == NULL)
513 OCPayloadDestroy((OCPayload *)secPayload);
514 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
515 return OC_STACK_NO_MEMORY;
518 cbData.context = (void *)crlData;
520 OCMethod method = OC_REST_POST;
521 OCDoHandle handle = NULL;
522 OIC_LOG(DEBUG, TAG, "Sending CRL info to resource server");
524 OCStackResult ret = OCDoResource(&handle, method, query,
525 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
526 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
528 if (ret != OC_STACK_OK)
530 OICFree(crlData->resArr);
538 * Internal function for handling credential generation and sending cretificate credential.
540 * @param[in] cred Instance of cred resource.
541 * @param[in] deviceInfo information about device to which credential is to be provisioned.
542 * @param[in] responseHandler callbak called by OC stack when request API receives response.
543 * @return OC_STACK_OK in case of success and other value otherwise.
545 static OCStackResult provisionCertCred(const OicSecCred_t *cred,
546 const OCProvisionDev_t *deviceInfo, CertData_t *certData,
547 OCClientResponseHandler responseHandler)
549 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
552 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
553 return OC_STACK_NO_MEMORY;
555 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
557 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
558 &secPayload->payloadSize, secureFlag);
560 if ((OC_STACK_OK != res) || (NULL == secPayload->securityData))
563 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
564 return OC_STACK_NO_MEMORY;
567 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
568 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
569 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
570 if(!PMGenerateQuery(true,
571 deviceInfo->endpoint.addr,
572 deviceInfo->securePort,
573 deviceInfo->connType,
574 query, sizeof(query), OIC_RSRC_CRED_URI))
576 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
577 OCPayloadDestroy((OCPayload *)secPayload);
578 return OC_STACK_ERROR;
580 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
582 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
583 cbData.cb = responseHandler;
584 cbData.context = (void *) certData;
587 OCDoHandle handle = NULL;
588 OCMethod method = OC_REST_POST;
589 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
590 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
591 OIC_LOG_V(INFO, TAG, "OCDoResource::Certificate provisioning returned : %d",ret);
592 if (ret != OC_STACK_OK)
594 OIC_LOG(ERROR, TAG, "OCStack resource error");
601 * Callback handler for handling callback of certificate provisioning device.
603 * @param[in] ctx ctx value passed to callback from calling function.
604 * @param[in] UNUSED handle to an invocation
605 * @param[in] clientResponse Response from queries to remote servers.
606 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
607 * and OC_STACK_KEEP_TRANSACTION to keep it.
609 static OCStackApplicationResult provisionCertCB(void *ctx, OCDoHandle UNUSED,
610 OCClientResponse *clientResponse)
612 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
613 CertData_t *certData = (CertData_t *) ctx;
616 OCProvisionResultCB resultCallback = certData->resultCallback;
617 OIC_LOG(INFO, TAG, "provisionCertCred called");
620 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
622 registerResultForCertProvisioning(certData, OC_STACK_RESOURCE_CREATED);
623 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
626 OICFree(certData->resArr);
628 return OC_STACK_DELETE_TRANSACTION;
632 OIC_LOG(INFO, TAG, "provisionCertCredCB received Null clientResponse");
633 registerResultForCertProvisioning(certData, OC_STACK_ERROR);
634 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
637 OICFree(certData->resArr);
639 return OC_STACK_DELETE_TRANSACTION;
641 #endif // __WITH_X509__
643 OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
644 const OCProvisionDev_t *pDev1,
645 const OCProvisionDev_t *pDev2,
646 OCProvisionResultCB resultCallback)
648 VERIFY_NON_NULL(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM);
649 if (SYMMETRIC_PAIR_WISE_KEY == type)
651 VERIFY_NON_NULL(TAG, pDev2, ERROR, OC_STACK_INVALID_PARAM);
655 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
656 return OC_STACK_INVALID_CALLBACK;
658 if (SYMMETRIC_PAIR_WISE_KEY == type &&
659 0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
661 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
662 return OC_STACK_INVALID_PARAM;
665 if (SYMMETRIC_PAIR_WISE_KEY == type &&
666 !(OWNER_PSK_LENGTH_128 == keySize || OWNER_PSK_LENGTH_256 == keySize))
668 OIC_LOG(INFO, TAG, "Invalid key size");
669 return OC_STACK_INVALID_PARAM;
672 OIC_LOG(INFO, TAG, "In SRPProvisionCredentials");
674 if (SYMMETRIC_PAIR_WISE_KEY == type)
676 bool linkExisits = true;
677 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExisits);
679 if (res != OC_STACK_OK)
681 OIC_LOG(ERROR, TAG, "Internal error occured");
686 OIC_LOG(ERROR, TAG, "Link already exists");
687 return OC_STACK_INVALID_PARAM;
691 OicUuid_t provTooldeviceID = {{0,}};
692 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
694 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
695 return OC_STACK_ERROR;
697 OIC_LOG(INFO, TAG, "retrieved deviceid");
700 case SYMMETRIC_PAIR_WISE_KEY:
702 const OCProvisionDev_t *firstDevice = pDev1;
703 const OCProvisionDev_t *secondDevice = pDev2;
705 OicSecCred_t *firstCred = NULL;
706 OicSecCred_t *secondCred = NULL;
707 OCStackResult res = PMGeneratePairWiseCredentials(type, keySize, &provTooldeviceID,
708 &firstDevice->doxm->deviceID, &secondDevice->doxm->deviceID,
709 &firstCred, &secondCred);
710 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
711 OIC_LOG(INFO, TAG, "Credentials generated successfully");
712 CredentialData_t *credData =
713 (CredentialData_t *) OICCalloc(1, sizeof(CredentialData_t));
714 if (NULL == credData)
718 OIC_LOG(ERROR, TAG, "Memory allocation problem");
719 return OC_STACK_NO_MEMORY;
721 credData->deviceInfo1 = firstDevice;
722 credData->deviceInfo2 = secondDevice;
723 credData->credInfo = secondCred;
725 credData->credInfoFirst = firstCred;
726 credData->numOfResults = 0;
727 credData->resultCallback = resultCallback;
728 // first call to provision creds to device1.
729 // second call to provision creds to device2.
732 (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
733 if (NULL == credData->resArr)
738 OIC_LOG(ERROR, TAG, "Memory allocation problem");
739 return OC_STACK_NO_MEMORY;
741 res = provisionCredentials(firstCred, firstDevice, credData, &provisionCredentialCB1);
742 if (OC_STACK_OK != res)
744 DeleteCredList(firstCred);
745 DeleteCredList(secondCred);
746 OICFree(credData->resArr);
749 OIC_LOG_V(INFO, TAG, "provisionCredentials returned: %d",res);
750 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
754 case SIGNED_ASYMMETRIC_KEY:
756 const OCProvisionDev_t *firstDevice = pDev1;
757 OicSecCred_t *cred = NULL;
758 OCStackResult res = PMGenerateCertificateCredentials(&provTooldeviceID,
759 &firstDevice->doxm->deviceID,&cred);
760 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
761 OIC_LOG(INFO, TAG, "Certificate credentials generated successfully");
762 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
763 if (NULL == certData)
766 OIC_LOG(ERROR, TAG, "Memory allocation problem");
767 return OC_STACK_NO_MEMORY;
770 certData->deviceInfo = firstDevice;
772 certData->credInfo = cred;
773 certData->numOfResults = 0;
774 certData->resultCallback = resultCallback;
776 certData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
777 if (NULL == certData->resArr)
779 DeleteCredList(cred);
781 OIC_LOG(ERROR, TAG, "Memory allocation problem");
782 return OC_STACK_NO_MEMORY;
785 res = provisionCertCred(cred, firstDevice, certData, &provisionCertCB);
786 if (OC_STACK_OK != res)
788 OICFree(certData->resArr);
791 DeleteCredList(cred);
792 OIC_LOG_V(INFO, TAG, "provisionCertCredentials returned: %d",res);
799 OIC_LOG(ERROR, TAG, "Invalid option.");
800 return OC_STACK_INVALID_PARAM;
803 return OC_STACK_ERROR;
807 * Internal Function to store results in result array during ACL provisioning.
809 static void registerResultForACLProvisioning(ACLData_t *aclData,
810 OCStackResult stackresult)
812 OIC_LOG_V(INFO, TAG, "Inside registerResultForACLProvisioning aclData->numOfResults is %d\n",
813 aclData->numOfResults);
814 memcpy(aclData->resArr[(aclData->numOfResults)].deviceId.id,
815 aclData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
816 aclData->resArr[(aclData->numOfResults)].res = stackresult;
817 ++(aclData->numOfResults);
821 * Callback handler of SRPProvisionACL.
823 * @param[in] ctx ctx value passed to callback from calling function.
824 * @param[in] UNUSED handle to an invocation
825 * @param[in] clientResponse Response from queries to remote servers.
826 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
827 * and OC_STACK_KEEP_TRANSACTION to keep it.
829 static OCStackApplicationResult SRPProvisionACLCB(void *ctx, OCDoHandle UNUSED,
830 OCClientResponse *clientResponse)
832 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionACLCB.");
834 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
835 ACLData_t *aclData = (ACLData_t*)ctx;
836 OCProvisionResultCB resultCallback = aclData->resultCallback;
840 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
842 registerResultForACLProvisioning(aclData, OC_STACK_RESOURCE_CREATED);
843 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
846 OICFree(aclData->resArr);
848 return OC_STACK_DELETE_TRANSACTION;
851 registerResultForACLProvisioning(aclData, OC_STACK_ERROR);
852 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
855 OIC_LOG_V(ERROR, TAG, "SRPProvisionACLCB received Null clientResponse");
856 OICFree(aclData->resArr);
858 return OC_STACK_DELETE_TRANSACTION;
861 OCStackResult SRPProvisionACL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
862 OicSecAcl_t *acl, OCProvisionResultCB resultCallback)
864 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
865 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
866 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
868 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
871 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
872 return OC_STACK_NO_MEMORY;
874 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
875 if(OC_STACK_OK != AclToCBORPayload(acl, &secPayload->securityData, &secPayload->payloadSize))
877 OCPayloadDestroy((OCPayload *)secPayload);
878 OIC_LOG(ERROR, TAG, "Failed to AclToCBORPayload");
879 return OC_STACK_NO_MEMORY;
881 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
882 if(!PMGenerateQuery(true,
883 selectedDeviceInfo->endpoint.addr,
884 selectedDeviceInfo->securePort,
885 selectedDeviceInfo->connType,
886 query, sizeof(query), OIC_RSRC_ACL_URI))
888 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
889 return OC_STACK_ERROR;
891 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
893 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
894 cbData.cb = &SRPProvisionACLCB;
895 ACLData_t *aclData = (ACLData_t *) OICCalloc(1, sizeof(ACLData_t));
898 OCPayloadDestroy((OCPayload *)secPayload);
899 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
900 return OC_STACK_NO_MEMORY;
902 aclData->deviceInfo = selectedDeviceInfo;
903 aclData->resultCallback = resultCallback;
904 aclData->numOfResults=0;
906 // call to provision ACL to device1.
908 aclData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
909 if (aclData->resArr == NULL)
912 OCPayloadDestroy((OCPayload *)secPayload);
913 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
914 return OC_STACK_NO_MEMORY;
916 cbData.context = (void *)aclData;
918 OCMethod method = OC_REST_POST;
919 OCDoHandle handle = NULL;
920 OIC_LOG(DEBUG, TAG, "Sending ACL info to resource server");
921 OCStackResult ret = OCDoResource(&handle, method, query,
922 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
923 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
924 if (ret != OC_STACK_OK)
926 OICFree(aclData->resArr);
929 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
934 * Internal Function to store results in result array during Direct-Pairing provisioning.
936 static void registerResultForDirectPairingProvisioning(PconfData_t *pconfData,
937 OCStackResult stackresult)
939 OIC_LOG_V(INFO, TAG, "Inside registerResultForDirectPairingProvisioning "
940 "pconfData->numOfResults is %d\n", pconfData->numOfResults);
941 memcpy(pconfData->resArr[(pconfData->numOfResults)].deviceId.id,
942 pconfData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
943 pconfData->resArr[(pconfData->numOfResults)].res = stackresult;
944 ++(pconfData->numOfResults);
948 * Callback handler of SRPProvisionDirectPairing.
950 * @param[in] ctx ctx value passed to callback from calling function.
951 * @param[in] UNUSED handle to an invocation
952 * @param[in] clientResponse Response from queries to remote servers.
953 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
954 * and OC_STACK_KEEP_TRANSACTION to keep it.
956 static OCStackApplicationResult SRPProvisionDirectPairingCB(void *ctx, OCDoHandle UNUSED,
957 OCClientResponse *clientResponse)
959 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionDirectPairingCB.");
961 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
962 PconfData_t *pconfData = (PconfData_t*)ctx;
963 OCProvisionResultCB resultCallback = pconfData->resultCallback;
967 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
969 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_OK);
970 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
973 OICFree(pconfData->resArr);
975 return OC_STACK_DELETE_TRANSACTION;
978 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_ERROR);
979 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
982 OIC_LOG_V(ERROR, TAG, "SRPProvisionDirectPairingCB received Null clientResponse");
983 OICFree(pconfData->resArr);
985 return OC_STACK_DELETE_TRANSACTION;
988 OCStackResult SRPProvisionDirectPairing(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
989 OicSecPconf_t *pconf, OCProvisionResultCB resultCallback)
991 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
992 VERIFY_NON_NULL(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
993 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
995 // check direct-pairing capability
996 if (true != selectedDeviceInfo->doxm->dpc)
998 OIC_LOG(ERROR, TAG, "Resouce server does not have Direct-Pairing Capability ");
999 return OC_STACK_UNAUTHORIZED_REQ;
1002 OicUuid_t provTooldeviceID = {.id={0}};
1003 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1005 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1006 return OC_STACK_ERROR;
1008 memcpy(&pconf->rownerID, &provTooldeviceID, sizeof(OicUuid_t));
1010 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1013 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1014 return OC_STACK_NO_MEMORY;
1016 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1018 if (OC_STACK_OK != PconfToCBORPayload(pconf, &(secPayload->securityData),
1019 &(secPayload->payloadSize)))
1021 OCPayloadDestroy((OCPayload*)secPayload);
1022 OIC_LOG(ERROR, TAG, "Failed to PconfToCborPayload");
1023 return OC_STACK_NO_MEMORY;
1025 OIC_LOG(DEBUG, TAG, "Created payload for pconf set");
1026 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1029 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1030 if(!PMGenerateQuery(true,
1031 selectedDeviceInfo->endpoint.addr,
1032 selectedDeviceInfo->securePort,
1033 selectedDeviceInfo->connType,
1034 query, sizeof(query), OIC_RSRC_PCONF_URI))
1036 OIC_LOG(ERROR, TAG, "SRPProvisionDirectPairing : Failed to generate query");
1037 return OC_STACK_ERROR;
1039 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1041 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1042 cbData.cb = &SRPProvisionDirectPairingCB;
1043 PconfData_t *pconfData = (PconfData_t *) OICCalloc(1, sizeof(PconfData_t));
1044 if (NULL == pconfData)
1046 OCPayloadDestroy((OCPayload*)secPayload);
1047 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1048 return OC_STACK_NO_MEMORY;
1050 pconfData->deviceInfo = selectedDeviceInfo;
1051 pconfData->resultCallback = resultCallback;
1052 pconfData->numOfResults=0;
1053 pconfData->ctx = ctx;
1054 // call to provision PCONF to device1.
1055 int noOfRiCalls = 1;
1056 pconfData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1057 if (NULL == pconfData->resArr)
1060 OCPayloadDestroy((OCPayload*)secPayload);
1061 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1062 return OC_STACK_NO_MEMORY;
1064 cbData.context = (void *)pconfData;
1066 OCMethod method = OC_REST_POST;
1067 OCDoHandle handle = NULL;
1068 OIC_LOG(DEBUG, TAG, "Sending PCONF info to resource server");
1069 OCStackResult ret = OCDoResource(&handle, method, query,
1070 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1071 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1072 if (OC_STACK_OK != ret)
1074 OICFree(pconfData->resArr);
1077 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1081 static void DeleteUnlinkData_t(UnlinkData_t *unlinkData)
1085 OICFree(unlinkData->unlinkDev);
1086 OICFree(unlinkData->unlinkRes);
1087 OICFree(unlinkData);
1091 static void registerResultForUnlinkDevices(UnlinkData_t *unlinkData, OCStackResult stackresult,
1094 if (NULL != unlinkData)
1096 OIC_LOG_V(INFO, TAG, "Inside registerResultForUnlinkDevices unlinkData->numOfResults is %d\n",
1097 unlinkData->numOfResults);
1098 OIC_LOG_V(INFO, TAG, "Stack result :: %d", stackresult);
1100 OicUuid_t *pUuid = &unlinkData->unlinkRes[(unlinkData->numOfResults)].deviceId;
1102 // Set result in the result array according to the position (devNum).
1103 if (idx != IDX_DB_UPDATE_RES)
1105 memcpy(pUuid->id, unlinkData->unlinkDev[idx].doxm->deviceID.id, sizeof(pUuid->id));
1108 { // When deivce ID is 000... this means it's the result of database update.
1109 memset(pUuid->id, 0, sizeof(pUuid->id));
1111 unlinkData->unlinkRes[(unlinkData->numOfResults)].res = stackresult;
1112 ++(unlinkData->numOfResults);
1113 OIC_LOG (INFO, TAG, "Out registerResultForUnlinkDevices");
1117 static OCStackResult SendDeleteCredentialRequest(void* ctx,
1118 OCClientResponseHandler respHandler,
1119 const OCProvisionDev_t* revokedDev,
1120 const OCProvisionDev_t* destDev)
1122 OIC_LOG(DEBUG, TAG, "IN SendDeleteCredentialRequest");
1124 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1126 return OC_STACK_INVALID_PARAM;
1130 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1131 if(OC_STACK_OK != ret)
1133 OIC_LOG(ERROR, TAG, "SendDeleteCredentialRequest : Failed to canonical UUID encoding");
1134 return OC_STACK_ERROR;
1137 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1139 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1140 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, destDev->endpoint.addr,
1141 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1145 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1146 return OC_STACK_ERROR;
1148 else if ((size_t)snRet >= sizeof(reqBuf))
1150 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1151 return OC_STACK_ERROR;
1154 OCCallbackData cbData;
1155 memset(&cbData, 0, sizeof(cbData));
1156 cbData.context = ctx;
1157 cbData.cb = respHandler;
1159 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1161 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1163 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1164 &destDev->endpoint, NULL,
1165 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1166 if (OC_STACK_OK != ret)
1168 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1170 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1176 * Callback handler of unlink second device.
1178 * @param[in] ctx ctx value passed to callback from calling function.
1179 * @param[in] handle handle to an invocation
1180 * @param[in] clientResponse Response from queries to remote servers.
1181 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1182 * OC_STACK_KEEP_TRANSACTION to keep it.
1184 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1185 OCClientResponse *clientResponse)
1188 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1189 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1190 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1194 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1195 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1197 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1199 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1203 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1204 unlinkData->resultCallback(unlinkData->ctx,
1205 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1211 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1212 IDX_SECOND_DEVICE_RES);
1213 unlinkData->resultCallback(unlinkData->ctx,
1214 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1215 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1219 //Update provisioning DB when succes case.
1220 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1221 &unlinkData->unlinkDev[1].doxm->deviceID))
1223 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1224 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1225 unlinkData->resultCallback(unlinkData->ctx,
1226 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1229 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1233 DeleteUnlinkData_t(unlinkData);
1234 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1235 return OC_STACK_DELETE_TRANSACTION;
1240 * Callback handler of unlink first device.
1242 * @param[in] ctx ctx value passed to callback from calling function.
1243 * @param[in] handle handle to an invocation
1244 * @param[in] clientResponse Response from queries to remote servers.
1245 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1246 * OC_STACK_KEEP_TRANSACTION to keep it.
1248 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1249 OCClientResponse *clientResponse)
1251 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1252 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1253 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1258 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1259 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1261 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1263 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1265 // Second revocation request to second device.
1266 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1267 &unlinkData->unlinkDev[0],
1268 &unlinkData->unlinkDev[1] /*Dest*/);
1269 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1270 if (OC_STACK_OK != res)
1272 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1273 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1274 IDX_SECOND_DEVICE_RES);
1275 unlinkData->resultCallback(unlinkData->ctx,
1276 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1281 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1282 return OC_STACK_DELETE_TRANSACTION;
1287 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1289 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1290 unlinkData->unlinkRes, true);
1296 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1297 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1298 IDX_FIRST_DEVICE_RES );
1299 unlinkData->resultCallback(unlinkData->ctx,
1300 unlinkData->numOfResults, unlinkData->unlinkRes,
1302 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1306 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1307 DeleteUnlinkData_t(unlinkData);
1308 return OC_STACK_DELETE_TRANSACTION;
1312 * Function to unlink devices.
1313 * This function will remove the credential & relationship between the two devices.
1315 * @param[in] ctx Application context would be returned in result callback
1316 * @param[in] pTargetDev1 first device information to be unlinked.
1317 * @param[in] pTargetDev2 second device information to be unlinked.
1318 * @param[in] resultCallback callback provided by API user, callback will be called when
1319 * device unlink is finished.
1320 * @return OC_STACK_OK in case of success and other value otherwise.
1322 OCStackResult SRPUnlinkDevices(void* ctx,
1323 const OCProvisionDev_t* pTargetDev1,
1324 const OCProvisionDev_t* pTargetDev2,
1325 OCProvisionResultCB resultCallback)
1327 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1329 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1331 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1332 return OC_STACK_INVALID_PARAM;
1334 if (!resultCallback)
1336 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1337 return OC_STACK_INVALID_CALLBACK;
1339 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1341 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1342 return OC_STACK_INVALID_PARAM;
1345 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1346 PMPrintOCProvisionDev(pTargetDev1);
1347 PMPrintOCProvisionDev(pTargetDev2);
1349 // Mark the link status stale
1350 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1351 if (OC_STACK_OK != res)
1353 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1357 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1358 VERIFY_NON_NULL(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1360 //Initialize unlink data
1361 unlinkData->ctx = ctx;
1362 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1363 if (NULL == unlinkData->unlinkDev)
1365 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1366 res = OC_STACK_NO_MEMORY;
1370 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1371 if (NULL == unlinkData->unlinkRes)
1373 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1374 res = OC_STACK_NO_MEMORY;
1378 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1379 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1381 unlinkData->numOfResults = 0;
1382 unlinkData->resultCallback = resultCallback;
1384 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1385 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1386 if (OC_STACK_OK != res)
1388 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1395 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1396 DeleteUnlinkData_t(unlinkData);
1400 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1404 OICFree(pRemoveData->revokeTargetDev);
1405 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1406 OICFree(pRemoveData->removeRes);
1407 OICFree(pRemoveData);
1411 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1412 OCStackResult stackresult, bool hasError)
1414 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1415 removeData->numOfResults + 1);
1418 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1419 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1423 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1424 0, sizeof(pLinkedDevId->id) );
1426 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1427 removeData->hasError = hasError;
1428 ++(removeData->numOfResults);
1430 // If we get suffcient result from linked devices, we have to call user callback and do free
1431 if (removeData->sizeOfResArray == removeData->numOfResults)
1433 if(!removeData->hasError)
1435 // Remove device info from prvisioning database
1436 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1438 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1439 removeData->hasError = true;
1442 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1443 removeData->hasError);
1444 DeleteRemoveData_t(removeData);
1449 * Callback handler of unlink first device.
1451 * @param[in] ctx ctx value passed to callback from calling function.
1452 * @param[in] handle handle to an invocation
1453 * @param[in] clientResponse Response from queries to remote servers.
1454 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1455 * and OC_STACK_KEEP_TRANSACTION to keep it.
1457 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1458 OCClientResponse *clientResponse)
1460 //Update the delete credential into delete device context
1461 //Save the deleted status in delDevCtx
1463 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1464 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1465 OCStackResult res = OC_STACK_ERROR;
1467 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1471 OicUuid_t revDevUuid = {.id={0}};
1472 if(UUID_LENGTH == clientResponse->identity.id_length)
1474 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1475 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1477 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1478 if (OC_STACK_OK != res)
1480 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1481 registerResultForRemoveDevice(removeData, &revDevUuid,
1482 OC_STACK_INCONSISTENT_DB, true);
1484 return OC_STACK_DELETE_TRANSACTION;
1487 registerResultForRemoveDevice(removeData, &revDevUuid,
1488 OC_STACK_RESOURCE_DELETED, false);
1492 registerResultForRemoveDevice(removeData, &revDevUuid,
1493 clientResponse->result, true);
1494 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1499 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1500 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1502 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1505 * Since server's credential was deleted,
1506 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1508 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1509 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1513 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1519 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1520 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1524 return OC_STACK_DELETE_TRANSACTION;
1527 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1528 OCProvisionDev_t* pOwnedDevList,
1529 OCUuidList_t* pLinkedUuidList,
1530 OCProvisionDev_t** ppLinkedDevList,
1531 size_t *numOfLinkedDev)
1533 // pOwnedDevList could be NULL. It means no alived and owned device now.
1534 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1535 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1537 return OC_STACK_INVALID_PARAM;
1541 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1542 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1544 // Mark the link status stale.
1545 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1546 if (OC_STACK_OK != res)
1548 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1549 return OC_STACK_INCONSISTENT_DB;
1554 // If this linked device is alive (power-on), add the deivce to the list.
1555 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1556 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1558 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1560 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1561 if (NULL == targetDev)
1563 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1564 return OC_STACK_NO_MEMORY;
1567 LL_PREPEND(*ppLinkedDevList, targetDev);
1574 *numOfLinkedDev = cnt;
1579 * Function to device revocation
1580 * This function will remove credential of target device from all devices in subnet.
1582 * @param[in] ctx Application context would be returned in result callback
1583 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1584 * @param[in] pTargetDev Device information to be revoked.
1585 * @param[in] resultCallback callback provided by API user, callback will be called when
1586 * credential revocation is finished.
1587 * @return OC_STACK_OK in case of success and other value otherwise.
1588 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1589 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1591 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1592 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1594 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1596 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1598 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1599 return OC_STACK_INVALID_PARAM;
1601 if (!resultCallback)
1603 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
1604 return OC_STACK_INVALID_CALLBACK;
1607 // Declare variables in here to handle error cases with goto statement.
1608 OCProvisionDev_t* pOwnedDevList = NULL;
1609 OCProvisionDev_t* pLinkedDevList = NULL;
1610 RemoveData_t* removeData = NULL;
1612 //1. Find all devices that has a credential of the revoked device
1613 OCUuidList_t* pLinkedUuidList = NULL;
1614 size_t numOfDevices = 0;
1615 OCStackResult res = OC_STACK_ERROR;
1616 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
1617 if (OC_STACK_OK != res)
1619 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
1622 // if there is no related device, we can skip further process.
1623 if (0 == numOfDevices)
1625 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
1626 res = OC_STACK_CONTINUE;
1630 //2. Find owned device from the network
1631 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
1632 if (OC_STACK_OK != res)
1634 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
1638 //3. Make a list of devices to send DELETE credential request
1639 // by comparing owned devices from provisioning database with mutlicast discovery result.
1640 size_t numOfLinkedDev = 0;
1641 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
1642 &pLinkedDevList, &numOfLinkedDev);
1643 if (OC_STACK_OK != res)
1645 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
1648 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
1649 { // So we don't have to send request message.
1650 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
1651 res = OC_STACK_CONTINUE;
1655 // 4. Prepare RemoveData Context data.
1656 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
1659 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1660 res = OC_STACK_NO_MEMORY;
1664 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
1665 if (!removeData->revokeTargetDev)
1667 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
1668 res = OC_STACK_NO_MEMORY;
1672 removeData->removeRes =
1673 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
1674 if (!removeData->removeRes)
1676 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1677 res = OC_STACK_NO_MEMORY;
1681 removeData->ctx = ctx;
1682 removeData->linkedDevList = pLinkedDevList;
1683 removeData->resultCallback = resultCallback;
1684 removeData->numOfResults = 0;
1685 removeData->sizeOfResArray = numOfLinkedDev;
1686 removeData->hasError = false;
1688 // 5. Send DELETE credential request to linked devices.
1689 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1690 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
1691 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
1693 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
1694 removeData->revokeTargetDev, curDev);
1695 if (OC_STACK_OK != res)
1697 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
1698 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
1702 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
1706 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
1707 PMDeleteDeviceList(pOwnedDevList);
1708 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
1710 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
1713 PDMDestoryOicUuidLinkList(pLinkedUuidList);
1714 PMDeleteDeviceList(pOwnedDevList);
1715 PMDeleteDeviceList(pLinkedDevList);
1718 OICFree(removeData->revokeTargetDev);
1719 OICFree(removeData->removeRes);
1720 OICFree(removeData);
1722 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");
1727 * Internal Function to store results in result array during GetCredResourceCB.
1729 static void registerResultForGetCredResourceCB(GetSecData_t *GetSecData,
1730 OCStackResult stackresult)
1732 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetCredResourceCB "
1733 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
1734 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
1735 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
1736 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
1737 ++(GetSecData->numOfResults);
1741 * Callback handler of SRPGetCredResource.
1743 * @param[in] ctx ctx value passed to callback from calling function.
1744 * @param[in] UNUSED handle to an invocation
1745 * @param[in] clientResponse Response from queries to remote servers.
1746 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1747 * and OC_STACK_KEEP_TRANSACTION to keep it.
1749 static OCStackApplicationResult SRPGetCredResourceCB(void *ctx, OCDoHandle UNUSED,
1750 OCClientResponse *clientResponse)
1752 OIC_LOG_V(INFO, TAG, "Inside SRPGetCredResourceCB.");
1754 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
1755 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
1756 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
1760 if(OC_STACK_OK == clientResponse->result)
1762 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
1763 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
1765 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
1767 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
1768 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
1771 OICFree(GetSecData->resArr);
1772 OICFree(GetSecData);
1774 return OC_STACK_DELETE_TRANSACTION;
1777 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
1778 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
1781 OIC_LOG_V(ERROR, TAG, "SRPGetCredResourceCB received Null clientResponse");
1782 OICFree(GetSecData->resArr);
1783 OICFree(GetSecData);
1785 return OC_STACK_DELETE_TRANSACTION;
1789 OCStackResult SRPGetCredResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
1790 OCProvisionResultCB resultCallback)
1792 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
1793 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1795 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1796 if(!PMGenerateQuery(true,
1797 selectedDeviceInfo->endpoint.addr,
1798 selectedDeviceInfo->securePort,
1799 selectedDeviceInfo->connType,
1800 query, sizeof(query), OIC_RSRC_CRED_URI))
1802 OIC_LOG(ERROR, TAG, "SRPGetCredResource : Failed to generate query");
1803 return OC_STACK_ERROR;
1805 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1807 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1808 cbData.cb = &SRPGetCredResourceCB;
1809 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
1810 if (NULL == GetSecData)
1812 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1813 return OC_STACK_NO_MEMORY;
1815 GetSecData->deviceInfo = selectedDeviceInfo;
1816 GetSecData->resultCallback = resultCallback;
1817 GetSecData->numOfResults=0;
1818 GetSecData->ctx = ctx;
1820 int noOfRiCalls = 1;
1821 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1822 if (NULL == GetSecData->resArr)
1824 OICFree(GetSecData);
1825 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1826 return OC_STACK_NO_MEMORY;
1828 cbData.context = (void *)GetSecData;
1830 OCMethod method = OC_REST_GET;
1831 OCDoHandle handle = NULL;
1832 OIC_LOG(DEBUG, TAG, "Sending Get Cred to resource server");
1833 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
1834 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1835 if (OC_STACK_OK != ret)
1837 OIC_LOG(ERROR, TAG, "OCStack resource error");
1838 OICFree(GetSecData->resArr);
1839 OICFree(GetSecData);
1841 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1842 OIC_LOG(DEBUG, TAG, "OUT SRPGetCredResource");