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 OIC_LOG(DEBUG, TAG, "Created payload for ACL:");
884 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
886 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
887 if(!PMGenerateQuery(true,
888 selectedDeviceInfo->endpoint.addr,
889 selectedDeviceInfo->securePort,
890 selectedDeviceInfo->connType,
891 query, sizeof(query), OIC_RSRC_ACL_URI))
893 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
894 return OC_STACK_ERROR;
896 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
898 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
899 cbData.cb = &SRPProvisionACLCB;
900 ACLData_t *aclData = (ACLData_t *) OICCalloc(1, sizeof(ACLData_t));
903 OCPayloadDestroy((OCPayload *)secPayload);
904 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
905 return OC_STACK_NO_MEMORY;
907 aclData->deviceInfo = selectedDeviceInfo;
908 aclData->resultCallback = resultCallback;
909 aclData->numOfResults=0;
911 // call to provision ACL to device1.
913 aclData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
914 if (aclData->resArr == NULL)
917 OCPayloadDestroy((OCPayload *)secPayload);
918 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
919 return OC_STACK_NO_MEMORY;
921 cbData.context = (void *)aclData;
923 OCMethod method = OC_REST_POST;
924 OCDoHandle handle = NULL;
925 OIC_LOG(DEBUG, TAG, "Sending ACL info to resource server");
926 OCStackResult ret = OCDoResource(&handle, method, query,
927 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
928 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
929 if (ret != OC_STACK_OK)
931 OICFree(aclData->resArr);
934 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
939 * Internal Function to store results in result array during Direct-Pairing provisioning.
941 static void registerResultForDirectPairingProvisioning(PconfData_t *pconfData,
942 OCStackResult stackresult)
944 OIC_LOG_V(INFO, TAG, "Inside registerResultForDirectPairingProvisioning "
945 "pconfData->numOfResults is %d\n", pconfData->numOfResults);
946 memcpy(pconfData->resArr[(pconfData->numOfResults)].deviceId.id,
947 pconfData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
948 pconfData->resArr[(pconfData->numOfResults)].res = stackresult;
949 ++(pconfData->numOfResults);
953 * Callback handler of SRPProvisionDirectPairing.
955 * @param[in] ctx ctx value passed to callback from calling function.
956 * @param[in] UNUSED handle to an invocation
957 * @param[in] clientResponse Response from queries to remote servers.
958 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
959 * and OC_STACK_KEEP_TRANSACTION to keep it.
961 static OCStackApplicationResult SRPProvisionDirectPairingCB(void *ctx, OCDoHandle UNUSED,
962 OCClientResponse *clientResponse)
964 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionDirectPairingCB.");
966 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
967 PconfData_t *pconfData = (PconfData_t*)ctx;
968 OCProvisionResultCB resultCallback = pconfData->resultCallback;
972 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
974 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_OK);
975 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
978 OICFree(pconfData->resArr);
980 return OC_STACK_DELETE_TRANSACTION;
983 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_ERROR);
984 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
987 OIC_LOG_V(ERROR, TAG, "SRPProvisionDirectPairingCB received Null clientResponse");
988 OICFree(pconfData->resArr);
990 return OC_STACK_DELETE_TRANSACTION;
993 OCStackResult SRPProvisionDirectPairing(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
994 OicSecPconf_t *pconf, OCProvisionResultCB resultCallback)
996 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
997 VERIFY_NON_NULL(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
998 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1000 // check direct-pairing capability
1001 if (true != selectedDeviceInfo->doxm->dpc)
1003 OIC_LOG(ERROR, TAG, "Resouce server does not have Direct-Pairing Capability ");
1004 return OC_STACK_UNAUTHORIZED_REQ;
1007 OicUuid_t provTooldeviceID = {.id={0}};
1008 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1010 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1011 return OC_STACK_ERROR;
1013 memcpy(&pconf->rownerID, &provTooldeviceID, sizeof(OicUuid_t));
1015 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1018 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1019 return OC_STACK_NO_MEMORY;
1021 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1023 if (OC_STACK_OK != PconfToCBORPayload(pconf, &(secPayload->securityData),
1024 &(secPayload->payloadSize)))
1026 OCPayloadDestroy((OCPayload*)secPayload);
1027 OIC_LOG(ERROR, TAG, "Failed to PconfToCborPayload");
1028 return OC_STACK_NO_MEMORY;
1030 OIC_LOG(DEBUG, TAG, "Created payload for pconf set");
1031 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1034 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1035 if(!PMGenerateQuery(true,
1036 selectedDeviceInfo->endpoint.addr,
1037 selectedDeviceInfo->securePort,
1038 selectedDeviceInfo->connType,
1039 query, sizeof(query), OIC_RSRC_PCONF_URI))
1041 OIC_LOG(ERROR, TAG, "SRPProvisionDirectPairing : Failed to generate query");
1042 return OC_STACK_ERROR;
1044 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1046 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1047 cbData.cb = &SRPProvisionDirectPairingCB;
1048 PconfData_t *pconfData = (PconfData_t *) OICCalloc(1, sizeof(PconfData_t));
1049 if (NULL == pconfData)
1051 OCPayloadDestroy((OCPayload*)secPayload);
1052 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1053 return OC_STACK_NO_MEMORY;
1055 pconfData->deviceInfo = selectedDeviceInfo;
1056 pconfData->resultCallback = resultCallback;
1057 pconfData->numOfResults=0;
1058 pconfData->ctx = ctx;
1059 // call to provision PCONF to device1.
1060 int noOfRiCalls = 1;
1061 pconfData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1062 if (NULL == pconfData->resArr)
1065 OCPayloadDestroy((OCPayload*)secPayload);
1066 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1067 return OC_STACK_NO_MEMORY;
1069 cbData.context = (void *)pconfData;
1071 OCMethod method = OC_REST_POST;
1072 OCDoHandle handle = NULL;
1073 OIC_LOG(DEBUG, TAG, "Sending PCONF info to resource server");
1074 OCStackResult ret = OCDoResource(&handle, method, query,
1075 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1076 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1077 if (OC_STACK_OK != ret)
1079 OICFree(pconfData->resArr);
1082 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1086 static void DeleteUnlinkData_t(UnlinkData_t *unlinkData)
1090 OICFree(unlinkData->unlinkDev);
1091 OICFree(unlinkData->unlinkRes);
1092 OICFree(unlinkData);
1096 static void registerResultForUnlinkDevices(UnlinkData_t *unlinkData, OCStackResult stackresult,
1099 if (NULL != unlinkData)
1101 OIC_LOG_V(INFO, TAG, "Inside registerResultForUnlinkDevices unlinkData->numOfResults is %d\n",
1102 unlinkData->numOfResults);
1103 OIC_LOG_V(INFO, TAG, "Stack result :: %d", stackresult);
1105 OicUuid_t *pUuid = &unlinkData->unlinkRes[(unlinkData->numOfResults)].deviceId;
1107 // Set result in the result array according to the position (devNum).
1108 if (idx != IDX_DB_UPDATE_RES)
1110 memcpy(pUuid->id, unlinkData->unlinkDev[idx].doxm->deviceID.id, sizeof(pUuid->id));
1113 { // When deivce ID is 000... this means it's the result of database update.
1114 memset(pUuid->id, 0, sizeof(pUuid->id));
1116 unlinkData->unlinkRes[(unlinkData->numOfResults)].res = stackresult;
1117 ++(unlinkData->numOfResults);
1118 OIC_LOG (INFO, TAG, "Out registerResultForUnlinkDevices");
1122 static OCStackResult SendDeleteCredentialRequest(void* ctx,
1123 OCClientResponseHandler respHandler,
1124 const OCProvisionDev_t* revokedDev,
1125 const OCProvisionDev_t* destDev)
1127 OIC_LOG(DEBUG, TAG, "IN SendDeleteCredentialRequest");
1129 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1131 return OC_STACK_INVALID_PARAM;
1135 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1136 if(OC_STACK_OK != ret)
1138 OIC_LOG(ERROR, TAG, "SendDeleteCredentialRequest : Failed to canonical UUID encoding");
1139 return OC_STACK_ERROR;
1142 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1144 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1145 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, destDev->endpoint.addr,
1146 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1150 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1151 return OC_STACK_ERROR;
1153 else if ((size_t)snRet >= sizeof(reqBuf))
1155 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1156 return OC_STACK_ERROR;
1159 OCCallbackData cbData;
1160 memset(&cbData, 0, sizeof(cbData));
1161 cbData.context = ctx;
1162 cbData.cb = respHandler;
1164 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1166 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1168 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1169 &destDev->endpoint, NULL,
1170 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1171 if (OC_STACK_OK != ret)
1173 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1175 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1181 * Callback handler of unlink second device.
1183 * @param[in] ctx ctx value passed to callback from calling function.
1184 * @param[in] handle handle to an invocation
1185 * @param[in] clientResponse Response from queries to remote servers.
1186 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1187 * OC_STACK_KEEP_TRANSACTION to keep it.
1189 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1190 OCClientResponse *clientResponse)
1193 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1194 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1195 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1199 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1200 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1202 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1204 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1208 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1209 unlinkData->resultCallback(unlinkData->ctx,
1210 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1216 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1217 IDX_SECOND_DEVICE_RES);
1218 unlinkData->resultCallback(unlinkData->ctx,
1219 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1220 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1224 //Update provisioning DB when succes case.
1225 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1226 &unlinkData->unlinkDev[1].doxm->deviceID))
1228 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1229 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1230 unlinkData->resultCallback(unlinkData->ctx,
1231 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1234 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1238 DeleteUnlinkData_t(unlinkData);
1239 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1240 return OC_STACK_DELETE_TRANSACTION;
1245 * Callback handler of unlink first device.
1247 * @param[in] ctx ctx value passed to callback from calling function.
1248 * @param[in] handle handle to an invocation
1249 * @param[in] clientResponse Response from queries to remote servers.
1250 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1251 * OC_STACK_KEEP_TRANSACTION to keep it.
1253 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1254 OCClientResponse *clientResponse)
1256 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1257 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1258 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1263 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1264 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1266 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1268 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1270 // Second revocation request to second device.
1271 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1272 &unlinkData->unlinkDev[0],
1273 &unlinkData->unlinkDev[1] /*Dest*/);
1274 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1275 if (OC_STACK_OK != res)
1277 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1278 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1279 IDX_SECOND_DEVICE_RES);
1280 unlinkData->resultCallback(unlinkData->ctx,
1281 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1286 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1287 return OC_STACK_DELETE_TRANSACTION;
1292 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1294 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1295 unlinkData->unlinkRes, true);
1301 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1302 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1303 IDX_FIRST_DEVICE_RES );
1304 unlinkData->resultCallback(unlinkData->ctx,
1305 unlinkData->numOfResults, unlinkData->unlinkRes,
1307 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1311 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1312 DeleteUnlinkData_t(unlinkData);
1313 return OC_STACK_DELETE_TRANSACTION;
1317 * Function to unlink devices.
1318 * This function will remove the credential & relationship between the two devices.
1320 * @param[in] ctx Application context would be returned in result callback
1321 * @param[in] pTargetDev1 first device information to be unlinked.
1322 * @param[in] pTargetDev2 second device information to be unlinked.
1323 * @param[in] resultCallback callback provided by API user, callback will be called when
1324 * device unlink is finished.
1325 * @return OC_STACK_OK in case of success and other value otherwise.
1327 OCStackResult SRPUnlinkDevices(void* ctx,
1328 const OCProvisionDev_t* pTargetDev1,
1329 const OCProvisionDev_t* pTargetDev2,
1330 OCProvisionResultCB resultCallback)
1332 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1334 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1336 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1337 return OC_STACK_INVALID_PARAM;
1339 if (!resultCallback)
1341 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1342 return OC_STACK_INVALID_CALLBACK;
1344 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1346 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1347 return OC_STACK_INVALID_PARAM;
1350 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1351 PMPrintOCProvisionDev(pTargetDev1);
1352 PMPrintOCProvisionDev(pTargetDev2);
1354 // Mark the link status stale
1355 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1356 if (OC_STACK_OK != res)
1358 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1362 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1363 VERIFY_NON_NULL(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1365 //Initialize unlink data
1366 unlinkData->ctx = ctx;
1367 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1368 if (NULL == unlinkData->unlinkDev)
1370 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1371 res = OC_STACK_NO_MEMORY;
1375 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1376 if (NULL == unlinkData->unlinkRes)
1378 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1379 res = OC_STACK_NO_MEMORY;
1383 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1384 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1386 unlinkData->numOfResults = 0;
1387 unlinkData->resultCallback = resultCallback;
1389 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1390 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1391 if (OC_STACK_OK != res)
1393 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1400 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1401 DeleteUnlinkData_t(unlinkData);
1405 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1409 OICFree(pRemoveData->revokeTargetDev);
1410 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1411 OICFree(pRemoveData->removeRes);
1412 OICFree(pRemoveData);
1416 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1417 OCStackResult stackresult, bool hasError)
1419 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1420 removeData->numOfResults + 1);
1423 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1424 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1428 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1429 0, sizeof(pLinkedDevId->id) );
1431 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1432 removeData->hasError = hasError;
1433 ++(removeData->numOfResults);
1435 // If we get suffcient result from linked devices, we have to call user callback and do free
1436 if (removeData->sizeOfResArray == removeData->numOfResults)
1438 if(!removeData->hasError)
1440 // Remove device info from prvisioning database
1441 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1443 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1444 removeData->hasError = true;
1447 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1448 removeData->hasError);
1449 DeleteRemoveData_t(removeData);
1454 * Callback handler of unlink first device.
1456 * @param[in] ctx ctx value passed to callback from calling function.
1457 * @param[in] handle handle to an invocation
1458 * @param[in] clientResponse Response from queries to remote servers.
1459 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1460 * and OC_STACK_KEEP_TRANSACTION to keep it.
1462 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1463 OCClientResponse *clientResponse)
1465 //Update the delete credential into delete device context
1466 //Save the deleted status in delDevCtx
1468 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1469 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1470 OCStackResult res = OC_STACK_ERROR;
1472 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1476 OicUuid_t revDevUuid = {.id={0}};
1477 if(UUID_LENGTH == clientResponse->identity.id_length)
1479 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1480 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1482 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1483 if (OC_STACK_OK != res)
1485 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1486 registerResultForRemoveDevice(removeData, &revDevUuid,
1487 OC_STACK_INCONSISTENT_DB, true);
1489 return OC_STACK_DELETE_TRANSACTION;
1492 registerResultForRemoveDevice(removeData, &revDevUuid,
1493 OC_STACK_RESOURCE_DELETED, false);
1497 registerResultForRemoveDevice(removeData, &revDevUuid,
1498 clientResponse->result, true);
1499 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1504 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1505 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1507 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1510 * Since server's credential was deleted,
1511 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1513 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1514 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1518 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1524 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1525 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1529 return OC_STACK_DELETE_TRANSACTION;
1532 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1533 OCProvisionDev_t* pOwnedDevList,
1534 OCUuidList_t* pLinkedUuidList,
1535 OCProvisionDev_t** ppLinkedDevList,
1536 size_t *numOfLinkedDev)
1538 // pOwnedDevList could be NULL. It means no alived and owned device now.
1539 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1540 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1542 return OC_STACK_INVALID_PARAM;
1546 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1547 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1549 // Mark the link status stale.
1550 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1551 if (OC_STACK_OK != res)
1553 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1554 return OC_STACK_INCONSISTENT_DB;
1559 // If this linked device is alive (power-on), add the deivce to the list.
1560 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1561 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1563 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1565 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1566 if (NULL == targetDev)
1568 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1569 return OC_STACK_NO_MEMORY;
1572 LL_PREPEND(*ppLinkedDevList, targetDev);
1579 *numOfLinkedDev = cnt;
1584 * Function to device revocation
1585 * This function will remove credential of target device from all devices in subnet.
1587 * @param[in] ctx Application context would be returned in result callback
1588 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1589 * @param[in] pTargetDev Device information to be revoked.
1590 * @param[in] resultCallback callback provided by API user, callback will be called when
1591 * credential revocation is finished.
1592 * @return OC_STACK_OK in case of success and other value otherwise.
1593 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1594 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1596 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1597 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1599 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1601 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1603 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1604 return OC_STACK_INVALID_PARAM;
1606 if (!resultCallback)
1608 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
1609 return OC_STACK_INVALID_CALLBACK;
1612 // Declare variables in here to handle error cases with goto statement.
1613 OCProvisionDev_t* pOwnedDevList = NULL;
1614 OCProvisionDev_t* pLinkedDevList = NULL;
1615 RemoveData_t* removeData = NULL;
1617 //1. Find all devices that has a credential of the revoked device
1618 OCUuidList_t* pLinkedUuidList = NULL;
1619 size_t numOfDevices = 0;
1620 OCStackResult res = OC_STACK_ERROR;
1621 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
1622 if (OC_STACK_OK != res)
1624 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
1627 // if there is no related device, we can skip further process.
1628 if (0 == numOfDevices)
1630 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
1631 res = OC_STACK_CONTINUE;
1635 //2. Find owned device from the network
1636 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
1637 if (OC_STACK_OK != res)
1639 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
1643 //3. Make a list of devices to send DELETE credential request
1644 // by comparing owned devices from provisioning database with mutlicast discovery result.
1645 size_t numOfLinkedDev = 0;
1646 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
1647 &pLinkedDevList, &numOfLinkedDev);
1648 if (OC_STACK_OK != res)
1650 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
1653 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
1654 { // So we don't have to send request message.
1655 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
1656 res = OC_STACK_CONTINUE;
1660 // 4. Prepare RemoveData Context data.
1661 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
1664 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1665 res = OC_STACK_NO_MEMORY;
1669 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
1670 if (!removeData->revokeTargetDev)
1672 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
1673 res = OC_STACK_NO_MEMORY;
1677 removeData->removeRes =
1678 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
1679 if (!removeData->removeRes)
1681 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1682 res = OC_STACK_NO_MEMORY;
1686 removeData->ctx = ctx;
1687 removeData->linkedDevList = pLinkedDevList;
1688 removeData->resultCallback = resultCallback;
1689 removeData->numOfResults = 0;
1690 removeData->sizeOfResArray = numOfLinkedDev;
1691 removeData->hasError = false;
1693 // 5. Send DELETE credential request to linked devices.
1694 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1695 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
1696 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
1698 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
1699 removeData->revokeTargetDev, curDev);
1700 if (OC_STACK_OK != res)
1702 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
1703 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
1707 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
1711 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
1712 PMDeleteDeviceList(pOwnedDevList);
1713 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
1715 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
1718 PDMDestoryOicUuidLinkList(pLinkedUuidList);
1719 PMDeleteDeviceList(pOwnedDevList);
1720 PMDeleteDeviceList(pLinkedDevList);
1723 OICFree(removeData->revokeTargetDev);
1724 OICFree(removeData->removeRes);
1725 OICFree(removeData);
1727 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");
1732 * Internal Function to store results in result array during GetCredResourceCB.
1734 static void registerResultForGetCredResourceCB(GetSecData_t *GetSecData,
1735 OCStackResult stackresult)
1737 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetCredResourceCB "
1738 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
1739 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
1740 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
1741 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
1742 ++(GetSecData->numOfResults);
1746 * Callback handler of SRPGetCredResource.
1748 * @param[in] ctx ctx value passed to callback from calling function.
1749 * @param[in] UNUSED handle to an invocation
1750 * @param[in] clientResponse Response from queries to remote servers.
1751 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1752 * and OC_STACK_KEEP_TRANSACTION to keep it.
1754 static OCStackApplicationResult SRPGetCredResourceCB(void *ctx, OCDoHandle UNUSED,
1755 OCClientResponse *clientResponse)
1757 OIC_LOG_V(INFO, TAG, "Inside SRPGetCredResourceCB.");
1759 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
1760 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
1761 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
1765 if(OC_STACK_OK == clientResponse->result)
1767 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
1768 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
1770 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
1772 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
1773 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
1776 OICFree(GetSecData->resArr);
1777 OICFree(GetSecData);
1779 return OC_STACK_DELETE_TRANSACTION;
1782 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
1783 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
1786 OIC_LOG_V(ERROR, TAG, "SRPGetCredResourceCB received Null clientResponse");
1787 OICFree(GetSecData->resArr);
1788 OICFree(GetSecData);
1790 return OC_STACK_DELETE_TRANSACTION;
1794 OCStackResult SRPGetCredResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
1795 OCProvisionResultCB resultCallback)
1797 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
1798 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1800 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1801 if(!PMGenerateQuery(true,
1802 selectedDeviceInfo->endpoint.addr,
1803 selectedDeviceInfo->securePort,
1804 selectedDeviceInfo->connType,
1805 query, sizeof(query), OIC_RSRC_CRED_URI))
1807 OIC_LOG(ERROR, TAG, "SRPGetCredResource : Failed to generate query");
1808 return OC_STACK_ERROR;
1810 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1812 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1813 cbData.cb = &SRPGetCredResourceCB;
1814 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
1815 if (NULL == GetSecData)
1817 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1818 return OC_STACK_NO_MEMORY;
1820 GetSecData->deviceInfo = selectedDeviceInfo;
1821 GetSecData->resultCallback = resultCallback;
1822 GetSecData->numOfResults=0;
1823 GetSecData->ctx = ctx;
1825 int noOfRiCalls = 1;
1826 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1827 if (NULL == GetSecData->resArr)
1829 OICFree(GetSecData);
1830 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1831 return OC_STACK_NO_MEMORY;
1833 cbData.context = (void *)GetSecData;
1835 OCMethod method = OC_REST_GET;
1836 OCDoHandle handle = NULL;
1837 OIC_LOG(DEBUG, TAG, "Sending Get Cred to resource server");
1838 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
1839 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1840 if (OC_STACK_OK != ret)
1842 OIC_LOG(ERROR, TAG, "OCStack resource error");
1843 OICFree(GetSecData->resArr);
1844 OICFree(GetSecData);
1846 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1847 OIC_LOG(DEBUG, TAG, "OUT SRPGetCredResource");
1853 * Internal Function to store results in result array during GetACLResourceCB.
1855 static void registerResultForGetACLResourceCB(GetSecData_t *GetSecData,
1856 OCStackResult stackresult)
1858 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetACLResourceCB "
1859 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
1860 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
1861 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
1862 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
1863 ++(GetSecData->numOfResults);
1867 * Callback handler of SRPGetACLResource.
1869 * @param[in] ctx ctx value passed to callback from calling function.
1870 * @param[in] UNUSED handle to an invocation
1871 * @param[in] clientResponse Response from queries to remote servers.
1872 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1873 * and OC_STACK_KEEP_TRANSACTION to keep it.
1875 static OCStackApplicationResult SRPGetACLResourceCB(void *ctx, OCDoHandle UNUSED,
1876 OCClientResponse *clientResponse)
1878 OIC_LOG_V(INFO, TAG, "Inside SRPGetACLResourceCB.");
1880 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
1881 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
1882 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
1886 if(OC_STACK_OK == clientResponse->result)
1888 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
1889 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
1891 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
1893 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
1894 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
1897 OICFree(GetSecData->resArr);
1898 OICFree(GetSecData);
1900 return OC_STACK_DELETE_TRANSACTION;
1903 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
1904 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
1907 OIC_LOG_V(ERROR, TAG, "SRPGetACLResourceCB received Null clientResponse");
1908 OICFree(GetSecData->resArr);
1909 OICFree(GetSecData);
1911 return OC_STACK_DELETE_TRANSACTION;
1915 OCStackResult SRPGetACLResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
1916 OCProvisionResultCB resultCallback)
1918 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
1919 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1921 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1922 if(!PMGenerateQuery(true,
1923 selectedDeviceInfo->endpoint.addr,
1924 selectedDeviceInfo->securePort,
1925 selectedDeviceInfo->connType,
1926 query, sizeof(query), OIC_RSRC_ACL_URI))
1928 OIC_LOG(ERROR, TAG, "SRPGetACLResource : Failed to generate query");
1929 return OC_STACK_ERROR;
1931 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1933 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1934 cbData.cb = &SRPGetACLResourceCB;
1935 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
1936 if (NULL == GetSecData)
1938 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1939 return OC_STACK_NO_MEMORY;
1941 GetSecData->deviceInfo = selectedDeviceInfo;
1942 GetSecData->resultCallback = resultCallback;
1943 GetSecData->numOfResults=0;
1944 GetSecData->ctx = ctx;
1946 int noOfRiCalls = 1;
1947 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1948 if (NULL == GetSecData->resArr)
1950 OICFree(GetSecData);
1951 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1952 return OC_STACK_NO_MEMORY;
1954 cbData.context = (void *)GetSecData;
1956 OCMethod method = OC_REST_GET;
1957 OCDoHandle handle = NULL;
1958 OIC_LOG(DEBUG, TAG, "Sending Get ACL to resource server");
1959 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
1960 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1961 if (OC_STACK_OK != ret)
1963 OIC_LOG(ERROR, TAG, "OCStack resource error");
1964 OICFree(GetSecData->resArr);
1965 OICFree(GetSecData);
1967 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1968 OIC_LOG(DEBUG, TAG, "OUT SRPGetACLResource");