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";
138 const char * SRP_FORM_DELETE_CREDENTIAL_TCP = "coaps+tcp://[%s]:%d%s?%s=%s";
140 // Structure to carry remove APIs data to callback.
141 typedef struct RemoveData RemoveData_t;
144 OCProvisionDev_t* revokeTargetDev; /**< Device which is going to be revoked..**/
145 OCProvisionDev_t* linkedDevList; /**< A list of devices which have invalid credential.**/
146 OCProvisionResult_t* removeRes; /**< Result array.**/
147 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
148 size_t numOfResults; /**< Number of results in result array.**/
149 size_t sizeOfResArray;
156 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
157 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
158 OCClientResponseHandler responseHandler);
162 * Internal function to update result in result array.
164 static void registerResultForCredProvisioning(CredentialData_t *credData,
165 OCStackResult stackresult, int cause)
168 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",credData->numOfResults);
171 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
172 credData->deviceInfo1->doxm->deviceID.id,UUID_LENGTH);
176 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
177 credData->deviceInfo2->doxm->deviceID.id,UUID_LENGTH);
179 credData->resArr[(credData->numOfResults)].res = stackresult;
180 ++(credData->numOfResults);
184 * Callback handler for handling callback of provisioning device 2.
186 * @param[in] ctx ctx value passed to callback from calling function.
187 * @param[in] UNUSED handle to an invocation
188 * @param[in] clientResponse Response from queries to remote servers.
189 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
190 * and OC_STACK_KEEP_TRANSACTION to keep it.
192 static OCStackApplicationResult provisionCredentialCB2(void *ctx, OCDoHandle UNUSED,
193 OCClientResponse *clientResponse)
195 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
196 CredentialData_t *credData = (CredentialData_t *) ctx;
199 OCProvisionResultCB resultCallback = credData->resultCallback;
200 OIC_LOG(INFO, TAG, "provisionCredentialCB2 called");
203 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
205 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CHANGED, 2);
206 OCStackResult res = PDMLinkDevices(&credData->deviceInfo1->doxm->deviceID,
207 &credData->deviceInfo2->doxm->deviceID);
208 if (OC_STACK_OK != res)
210 OIC_LOG(ERROR, TAG, "Error occured on PDMLinkDevices");
211 return OC_STACK_DELETE_TRANSACTION;
213 OIC_LOG(INFO, TAG, "Link created successfully");
215 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
218 OICFree(credData->resArr);
220 return OC_STACK_DELETE_TRANSACTION;
224 OIC_LOG(INFO, TAG, "provisionCredentialCB2 received Null clientResponse");
225 registerResultForCredProvisioning(credData, OC_STACK_ERROR, 2);
226 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
229 OICFree(credData->resArr);
231 return OC_STACK_DELETE_TRANSACTION;
235 * Callback handler for handling callback of provisioning device 1.
237 * @param[in] ctx ctx value passed to callback from calling function.
238 * @param[in] UNUSED handle to an invocation
239 * @param[in] clientResponse Response from queries to remote servers.
240 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
241 * and OC_STACK_KEEP_TRANSACTION to keep it.
243 static OCStackApplicationResult provisionCredentialCB1(void *ctx, OCDoHandle UNUSED,
244 OCClientResponse *clientResponse)
246 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
248 CredentialData_t* credData = (CredentialData_t*) ctx;
249 OICFree(credData->credInfoFirst);
250 const OCProvisionDev_t *deviceInfo = credData->deviceInfo2;
251 OicSecCred_t *credInfo = credData->credInfo;
252 const OCProvisionResultCB resultCallback = credData->resultCallback;
255 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
257 // send credentials to second device
258 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CHANGED,1);
259 OCStackResult res = provisionCredentials(credInfo, deviceInfo, credData,
260 provisionCredentialCB2);
261 DeleteCredList(credInfo);
262 if (OC_STACK_OK != res)
264 registerResultForCredProvisioning(credData, res,2);
265 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
268 OICFree(credData->resArr);
275 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
276 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
279 OICFree(credData->resArr);
286 OIC_LOG(INFO, TAG, "provisionCredentialCB received Null clientResponse for first device");
287 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
288 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
291 DeleteCredList(credInfo);
292 OICFree(credData->resArr);
296 return OC_STACK_DELETE_TRANSACTION;
302 * Internal function for handling credential generation and sending credential to resource server.
304 * @param[in] cred Instance of cred resource.
305 * @param[in] deviceInfo information about device to which credential is to be provisioned.
306 * @param[in] responseHandler callbak called by OC stack when request API receives response.
307 * @return OC_STACK_OK in case of success and other value otherwise.
309 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
310 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
311 OCClientResponseHandler responseHandler)
313 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
316 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
317 return OC_STACK_NO_MEMORY;
319 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
321 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
322 &secPayload->payloadSize, secureFlag);
323 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
325 OCPayloadDestroy((OCPayload *)secPayload);
326 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
327 return OC_STACK_NO_MEMORY;
330 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
331 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
332 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
333 if(!PMGenerateQuery(true,
334 deviceInfo->endpoint.addr,
335 deviceInfo->securePort,
336 deviceInfo->connType,
337 query, sizeof(query), OIC_RSRC_CRED_URI))
339 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
340 return OC_STACK_ERROR;
342 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
344 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
345 cbData.cb = responseHandler;
346 cbData.context = (void *) credData;
349 OCDoHandle handle = NULL;
350 OCMethod method = OC_REST_POST;
351 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
352 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
353 OIC_LOG_V(INFO, TAG, "OCDoResource::Credential provisioning returned : %d",ret);
354 if (ret != OC_STACK_OK)
356 OIC_LOG(ERROR, TAG, "OCStack resource error");
364 * Structure to carry certificate data to callback.
366 typedef struct CertificateData CertData_t;
367 struct CertificateData
369 void *ctx; /**< Pointer to user context.**/
370 const OCProvisionDev_t *deviceInfo; /**< Pointer to OCProvisionDev_t.**/
371 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
372 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
373 OCProvisionResult_t *resArr; /**< Result array.**/
374 int numOfResults; /**< Number of results in result array.**/
378 * Structure to carry CRL provision API data to callback.
380 typedef struct CRLData CRLData_t;
383 void *ctx; /**< Pointer to user context.**/
384 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
385 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
386 OCProvisionResult_t *resArr; /**< Result array.**/
387 int numOfResults; /**< Number of results in result array.**/
391 * Internal function to update result in result array.
393 static void registerResultForCertProvisioning(CertData_t *certData,
394 OCStackResult stackresult)
397 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",certData->numOfResults);
398 memcpy(certData->resArr[(certData->numOfResults)].deviceId.id,
399 certData->deviceInfo->doxm->deviceID.id,UUID_LENGTH);
400 certData->resArr[(certData->numOfResults)].res = stackresult;
401 ++(certData->numOfResults);
405 * Internal Function to store results in result array during ACL provisioning.
407 static void registerResultForCRLProvisioning(CRLData_t *crlData,
408 OCStackResult stackresult)
410 OIC_LOG_V(INFO, TAG, "Inside registerResultForCRLProvisioning crlData->numOfResults is %d\n",
411 crlData->numOfResults);
412 memcpy(crlData->resArr[(crlData->numOfResults)].deviceId.id,
413 crlData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
414 crlData->resArr[(crlData->numOfResults)].res = stackresult;
415 ++(crlData->numOfResults);
420 * Callback handler of SRPProvisionCRL.
422 * @param[in] ctx ctx value passed to callback from calling function.
423 * @param[in] UNUSED handle to an invocation
424 * @param[in] clientResponse Response from queries to remote servers.
425 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
426 * and OC_STACK_KEEP_TRANSACTION to keep it.
428 static OCStackApplicationResult SRPProvisionCRLCB(void *ctx, OCDoHandle UNUSED,
429 OCClientResponse *clientResponse)
431 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionCRLCB.");
433 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
434 CRLData_t *crlData = (CRLData_t*)ctx;
435 OCProvisionResultCB resultCallback = crlData->resultCallback;
439 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
441 registerResultForCRLProvisioning(crlData, OC_STACK_RESOURCE_CHANGED);
442 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
445 OICFree(crlData->resArr);
447 return OC_STACK_DELETE_TRANSACTION;
450 registerResultForCRLProvisioning(crlData, OC_STACK_ERROR);
451 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
454 OIC_LOG_V(ERROR, TAG, "SRPProvisionCRLCB received Null clientResponse");
455 OICFree(crlData->resArr);
457 return OC_STACK_DELETE_TRANSACTION;
460 OCStackResult SRPProvisionCRL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
461 OicSecCrl_t *crl, OCProvisionResultCB resultCallback)
463 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
464 VERIFY_NON_NULL(TAG, crl, ERROR, OC_STACK_INVALID_PARAM);
465 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
467 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
470 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
471 return OC_STACK_NO_MEMORY;
474 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
475 OCStackResult res = CrlToCBORPayload(crl, &secPayload->securityData, &secPayload->payloadSize);
476 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
479 OIC_LOG(ERROR, TAG, "Failed to BinToCrlJSON");
480 return OC_STACK_NO_MEMORY;
482 OIC_LOG(DEBUG, TAG, "Created payload for CRL:");
483 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
485 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
486 if(!PMGenerateQuery(true,
487 selectedDeviceInfo->endpoint.addr,
488 selectedDeviceInfo->securePort,
489 selectedDeviceInfo->connType,
490 query, sizeof(query), OIC_RSRC_CRL_URI))
492 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
493 OCPayloadDestroy((OCPayload *)secPayload);
494 return OC_STACK_ERROR;
496 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
498 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
499 cbData.cb = &SRPProvisionCRLCB;
500 CRLData_t *crlData = (CRLData_t *) OICCalloc(1, sizeof(CRLData_t));
503 OCPayloadDestroy((OCPayload *)secPayload);
504 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
505 return OC_STACK_NO_MEMORY;
508 crlData->deviceInfo = selectedDeviceInfo;
509 crlData->resultCallback = resultCallback;
510 crlData->numOfResults=0;
513 crlData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
514 if (crlData->resArr == NULL)
516 OCPayloadDestroy((OCPayload *)secPayload);
517 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
518 return OC_STACK_NO_MEMORY;
521 cbData.context = (void *)crlData;
523 OCMethod method = OC_REST_POST;
524 OCDoHandle handle = NULL;
525 OIC_LOG(DEBUG, TAG, "Sending CRL info to resource server");
527 OCStackResult ret = OCDoResource(&handle, method, query,
528 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
529 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
531 if (ret != OC_STACK_OK)
533 OICFree(crlData->resArr);
541 * Internal function for handling credential generation and sending cretificate credential.
543 * @param[in] cred Instance of cred resource.
544 * @param[in] deviceInfo information about device to which credential is to be provisioned.
545 * @param[in] responseHandler callbak called by OC stack when request API receives response.
546 * @return OC_STACK_OK in case of success and other value otherwise.
548 static OCStackResult provisionCertCred(const OicSecCred_t *cred,
549 const OCProvisionDev_t *deviceInfo, CertData_t *certData,
550 OCClientResponseHandler responseHandler)
552 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
555 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
556 return OC_STACK_NO_MEMORY;
558 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
560 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
561 &secPayload->payloadSize, secureFlag);
563 if ((OC_STACK_OK != res) || (NULL == secPayload->securityData))
566 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
567 return OC_STACK_NO_MEMORY;
570 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
571 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
572 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
573 if(!PMGenerateQuery(true,
574 deviceInfo->endpoint.addr,
575 deviceInfo->securePort,
576 deviceInfo->connType,
577 query, sizeof(query), OIC_RSRC_CRED_URI))
579 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
580 OCPayloadDestroy((OCPayload *)secPayload);
581 return OC_STACK_ERROR;
583 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
585 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
586 cbData.cb = responseHandler;
587 cbData.context = (void *) certData;
590 OCDoHandle handle = NULL;
591 OCMethod method = OC_REST_POST;
592 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
593 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
594 OIC_LOG_V(INFO, TAG, "OCDoResource::Certificate provisioning returned : %d",ret);
595 if (ret != OC_STACK_OK)
597 OIC_LOG(ERROR, TAG, "OCStack resource error");
604 * Callback handler for handling callback of certificate provisioning device.
606 * @param[in] ctx ctx value passed to callback from calling function.
607 * @param[in] UNUSED handle to an invocation
608 * @param[in] clientResponse Response from queries to remote servers.
609 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
610 * and OC_STACK_KEEP_TRANSACTION to keep it.
612 static OCStackApplicationResult provisionCertCB(void *ctx, OCDoHandle UNUSED,
613 OCClientResponse *clientResponse)
615 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
616 CertData_t *certData = (CertData_t *) ctx;
619 OCProvisionResultCB resultCallback = certData->resultCallback;
620 OIC_LOG(INFO, TAG, "provisionCertCred called");
623 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
625 registerResultForCertProvisioning(certData, OC_STACK_RESOURCE_CHANGED);
626 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
629 OICFree(certData->resArr);
631 return OC_STACK_DELETE_TRANSACTION;
635 OIC_LOG(INFO, TAG, "provisionCertCredCB received Null clientResponse");
636 registerResultForCertProvisioning(certData, OC_STACK_ERROR);
637 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
640 OICFree(certData->resArr);
642 return OC_STACK_DELETE_TRANSACTION;
644 #endif // __WITH_X509__
646 OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
647 const OCProvisionDev_t *pDev1,
648 const OCProvisionDev_t *pDev2,
649 OCProvisionResultCB resultCallback)
651 VERIFY_NON_NULL(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM);
652 if (SYMMETRIC_PAIR_WISE_KEY == type)
654 VERIFY_NON_NULL(TAG, pDev2, ERROR, OC_STACK_INVALID_PARAM);
658 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
659 return OC_STACK_INVALID_CALLBACK;
661 if (SYMMETRIC_PAIR_WISE_KEY == type &&
662 0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
664 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
665 return OC_STACK_INVALID_PARAM;
668 if (SYMMETRIC_PAIR_WISE_KEY == type &&
669 !(OWNER_PSK_LENGTH_128 == keySize || OWNER_PSK_LENGTH_256 == keySize))
671 OIC_LOG(INFO, TAG, "Invalid key size");
672 return OC_STACK_INVALID_PARAM;
675 OIC_LOG(INFO, TAG, "In SRPProvisionCredentials");
677 if (SYMMETRIC_PAIR_WISE_KEY == type)
679 bool linkExisits = true;
680 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExisits);
682 if (res != OC_STACK_OK)
684 OIC_LOG(ERROR, TAG, "Internal error occured");
689 OIC_LOG(ERROR, TAG, "Link already exists");
690 return OC_STACK_INVALID_PARAM;
694 OicUuid_t provTooldeviceID = {{0,}};
695 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
697 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
698 return OC_STACK_ERROR;
700 OIC_LOG(INFO, TAG, "retrieved deviceid");
703 case SYMMETRIC_PAIR_WISE_KEY:
705 const OCProvisionDev_t *firstDevice = pDev1;
706 const OCProvisionDev_t *secondDevice = pDev2;
708 OicSecCred_t *firstCred = NULL;
709 OicSecCred_t *secondCred = NULL;
710 OCStackResult res = PMGeneratePairWiseCredentials(type, keySize, &provTooldeviceID,
711 &firstDevice->doxm->deviceID, &secondDevice->doxm->deviceID,
712 &firstCred, &secondCred);
713 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
714 OIC_LOG(INFO, TAG, "Credentials generated successfully");
715 CredentialData_t *credData =
716 (CredentialData_t *) OICCalloc(1, sizeof(CredentialData_t));
717 if (NULL == credData)
721 OIC_LOG(ERROR, TAG, "Memory allocation problem");
722 return OC_STACK_NO_MEMORY;
724 credData->deviceInfo1 = firstDevice;
725 credData->deviceInfo2 = secondDevice;
726 credData->credInfo = secondCred;
728 credData->credInfoFirst = firstCred;
729 credData->numOfResults = 0;
730 credData->resultCallback = resultCallback;
731 // first call to provision creds to device1.
732 // second call to provision creds to device2.
735 (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
736 if (NULL == credData->resArr)
741 OIC_LOG(ERROR, TAG, "Memory allocation problem");
742 return OC_STACK_NO_MEMORY;
744 res = provisionCredentials(firstCred, firstDevice, credData, &provisionCredentialCB1);
745 if (OC_STACK_OK != res)
747 DeleteCredList(firstCred);
748 DeleteCredList(secondCred);
749 OICFree(credData->resArr);
752 OIC_LOG_V(INFO, TAG, "provisionCredentials returned: %d",res);
753 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
757 case SIGNED_ASYMMETRIC_KEY:
759 const OCProvisionDev_t *firstDevice = pDev1;
760 OicSecCred_t *cred = NULL;
761 OCStackResult res = PMGenerateCertificateCredentials(&provTooldeviceID,
762 &firstDevice->doxm->deviceID,&cred);
763 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
764 OIC_LOG(INFO, TAG, "Certificate credentials generated successfully");
765 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
766 if (NULL == certData)
769 OIC_LOG(ERROR, TAG, "Memory allocation problem");
770 return OC_STACK_NO_MEMORY;
773 certData->deviceInfo = firstDevice;
775 certData->credInfo = cred;
776 certData->numOfResults = 0;
777 certData->resultCallback = resultCallback;
779 certData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
780 if (NULL == certData->resArr)
782 DeleteCredList(cred);
784 OIC_LOG(ERROR, TAG, "Memory allocation problem");
785 return OC_STACK_NO_MEMORY;
788 res = provisionCertCred(cred, firstDevice, certData, &provisionCertCB);
789 if (OC_STACK_OK != res)
791 OICFree(certData->resArr);
794 DeleteCredList(cred);
795 OIC_LOG_V(INFO, TAG, "provisionCertCredentials returned: %d",res);
802 OIC_LOG(ERROR, TAG, "Invalid option.");
803 return OC_STACK_INVALID_PARAM;
806 return OC_STACK_ERROR;
810 * Internal Function to store results in result array during ACL provisioning.
812 static void registerResultForACLProvisioning(ACLData_t *aclData,
813 OCStackResult stackresult)
815 OIC_LOG_V(INFO, TAG, "Inside registerResultForACLProvisioning aclData->numOfResults is %d\n",
816 aclData->numOfResults);
817 memcpy(aclData->resArr[(aclData->numOfResults)].deviceId.id,
818 aclData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
819 aclData->resArr[(aclData->numOfResults)].res = stackresult;
820 ++(aclData->numOfResults);
824 * Callback handler of SRPProvisionACL.
826 * @param[in] ctx ctx value passed to callback from calling function.
827 * @param[in] UNUSED handle to an invocation
828 * @param[in] clientResponse Response from queries to remote servers.
829 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
830 * and OC_STACK_KEEP_TRANSACTION to keep it.
832 static OCStackApplicationResult SRPProvisionACLCB(void *ctx, OCDoHandle UNUSED,
833 OCClientResponse *clientResponse)
835 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionACLCB.");
837 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
838 ACLData_t *aclData = (ACLData_t*)ctx;
839 OCProvisionResultCB resultCallback = aclData->resultCallback;
843 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
845 registerResultForACLProvisioning(aclData, OC_STACK_RESOURCE_CHANGED);
846 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
849 OICFree(aclData->resArr);
851 return OC_STACK_DELETE_TRANSACTION;
854 registerResultForACLProvisioning(aclData, OC_STACK_ERROR);
855 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
858 OIC_LOG_V(ERROR, TAG, "SRPProvisionACLCB received Null clientResponse");
859 OICFree(aclData->resArr);
861 return OC_STACK_DELETE_TRANSACTION;
864 OCStackResult SRPProvisionACL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
865 OicSecAcl_t *acl, OCProvisionResultCB resultCallback)
867 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
868 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
869 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
871 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
874 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
875 return OC_STACK_NO_MEMORY;
877 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
878 if(OC_STACK_OK != AclToCBORPayload(acl, &secPayload->securityData, &secPayload->payloadSize))
880 OCPayloadDestroy((OCPayload *)secPayload);
881 OIC_LOG(ERROR, TAG, "Failed to AclToCBORPayload");
882 return OC_STACK_NO_MEMORY;
884 OIC_LOG(DEBUG, TAG, "Created payload for ACL:");
885 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
887 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
888 if(!PMGenerateQuery(true,
889 selectedDeviceInfo->endpoint.addr,
890 selectedDeviceInfo->securePort,
891 selectedDeviceInfo->connType,
892 query, sizeof(query), OIC_RSRC_ACL_URI))
894 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
895 return OC_STACK_ERROR;
897 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
899 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
900 cbData.cb = &SRPProvisionACLCB;
901 ACLData_t *aclData = (ACLData_t *) OICCalloc(1, sizeof(ACLData_t));
904 OCPayloadDestroy((OCPayload *)secPayload);
905 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
906 return OC_STACK_NO_MEMORY;
908 aclData->deviceInfo = selectedDeviceInfo;
909 aclData->resultCallback = resultCallback;
910 aclData->numOfResults=0;
912 // call to provision ACL to device1.
914 aclData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
915 if (aclData->resArr == NULL)
918 OCPayloadDestroy((OCPayload *)secPayload);
919 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
920 return OC_STACK_NO_MEMORY;
922 cbData.context = (void *)aclData;
924 OCMethod method = OC_REST_POST;
925 OCDoHandle handle = NULL;
926 OIC_LOG(DEBUG, TAG, "Sending ACL info to resource server");
927 OCStackResult ret = OCDoResource(&handle, method, query,
928 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
929 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
930 if (ret != OC_STACK_OK)
932 OICFree(aclData->resArr);
935 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
940 * Internal Function to store results in result array during Direct-Pairing provisioning.
942 static void registerResultForDirectPairingProvisioning(PconfData_t *pconfData,
943 OCStackResult stackresult)
945 OIC_LOG_V(INFO, TAG, "Inside registerResultForDirectPairingProvisioning "
946 "pconfData->numOfResults is %d\n", pconfData->numOfResults);
947 memcpy(pconfData->resArr[(pconfData->numOfResults)].deviceId.id,
948 pconfData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
949 pconfData->resArr[(pconfData->numOfResults)].res = stackresult;
950 ++(pconfData->numOfResults);
954 * Callback handler of SRPProvisionDirectPairing.
956 * @param[in] ctx ctx value passed to callback from calling function.
957 * @param[in] UNUSED handle to an invocation
958 * @param[in] clientResponse Response from queries to remote servers.
959 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
960 * and OC_STACK_KEEP_TRANSACTION to keep it.
962 static OCStackApplicationResult SRPProvisionDirectPairingCB(void *ctx, OCDoHandle UNUSED,
963 OCClientResponse *clientResponse)
965 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionDirectPairingCB.");
967 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
968 PconfData_t *pconfData = (PconfData_t*)ctx;
969 OCProvisionResultCB resultCallback = pconfData->resultCallback;
973 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
975 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_OK);
976 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
979 OICFree(pconfData->resArr);
981 return OC_STACK_DELETE_TRANSACTION;
984 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_ERROR);
985 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
988 OIC_LOG_V(ERROR, TAG, "SRPProvisionDirectPairingCB received Null clientResponse");
989 OICFree(pconfData->resArr);
991 return OC_STACK_DELETE_TRANSACTION;
994 OCStackResult SRPProvisionDirectPairing(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
995 OicSecPconf_t *pconf, OCProvisionResultCB resultCallback)
997 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
998 VERIFY_NON_NULL(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
999 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1001 // check direct-pairing capability
1002 if (true != selectedDeviceInfo->doxm->dpc)
1004 OIC_LOG(ERROR, TAG, "Resouce server does not have Direct-Pairing Capability ");
1005 return OC_STACK_UNAUTHORIZED_REQ;
1008 OicUuid_t provTooldeviceID = {.id={0}};
1009 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1011 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1012 return OC_STACK_ERROR;
1014 memcpy(&pconf->rownerID, &provTooldeviceID, sizeof(OicUuid_t));
1016 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1019 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1020 return OC_STACK_NO_MEMORY;
1022 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1024 if (OC_STACK_OK != PconfToCBORPayload(pconf, &(secPayload->securityData),
1025 &(secPayload->payloadSize)))
1027 OCPayloadDestroy((OCPayload*)secPayload);
1028 OIC_LOG(ERROR, TAG, "Failed to PconfToCborPayload");
1029 return OC_STACK_NO_MEMORY;
1031 OIC_LOG(DEBUG, TAG, "Created payload for pconf set");
1032 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1035 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1036 if(!PMGenerateQuery(true,
1037 selectedDeviceInfo->endpoint.addr,
1038 selectedDeviceInfo->securePort,
1039 selectedDeviceInfo->connType,
1040 query, sizeof(query), OIC_RSRC_PCONF_URI))
1042 OIC_LOG(ERROR, TAG, "SRPProvisionDirectPairing : Failed to generate query");
1043 return OC_STACK_ERROR;
1045 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1047 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1048 cbData.cb = &SRPProvisionDirectPairingCB;
1049 PconfData_t *pconfData = (PconfData_t *) OICCalloc(1, sizeof(PconfData_t));
1050 if (NULL == pconfData)
1052 OCPayloadDestroy((OCPayload*)secPayload);
1053 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1054 return OC_STACK_NO_MEMORY;
1056 pconfData->deviceInfo = selectedDeviceInfo;
1057 pconfData->resultCallback = resultCallback;
1058 pconfData->numOfResults=0;
1059 pconfData->ctx = ctx;
1060 // call to provision PCONF to device1.
1061 int noOfRiCalls = 1;
1062 pconfData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1063 if (NULL == pconfData->resArr)
1066 OCPayloadDestroy((OCPayload*)secPayload);
1067 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1068 return OC_STACK_NO_MEMORY;
1070 cbData.context = (void *)pconfData;
1072 OCMethod method = OC_REST_POST;
1073 OCDoHandle handle = NULL;
1074 OIC_LOG(DEBUG, TAG, "Sending PCONF info to resource server");
1075 OCStackResult ret = OCDoResource(&handle, method, query,
1076 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1077 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1078 if (OC_STACK_OK != ret)
1080 OICFree(pconfData->resArr);
1083 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1087 static void DeleteUnlinkData_t(UnlinkData_t *unlinkData)
1091 OICFree(unlinkData->unlinkDev);
1092 OICFree(unlinkData->unlinkRes);
1093 OICFree(unlinkData);
1097 static void registerResultForUnlinkDevices(UnlinkData_t *unlinkData, OCStackResult stackresult,
1100 if (NULL != unlinkData)
1102 OIC_LOG_V(INFO, TAG, "Inside registerResultForUnlinkDevices unlinkData->numOfResults is %d\n",
1103 unlinkData->numOfResults);
1104 OIC_LOG_V(INFO, TAG, "Stack result :: %d", stackresult);
1106 OicUuid_t *pUuid = &unlinkData->unlinkRes[(unlinkData->numOfResults)].deviceId;
1108 // Set result in the result array according to the position (devNum).
1109 if (idx != IDX_DB_UPDATE_RES)
1111 memcpy(pUuid->id, unlinkData->unlinkDev[idx].doxm->deviceID.id, sizeof(pUuid->id));
1114 { // When deivce ID is 000... this means it's the result of database update.
1115 memset(pUuid->id, 0, sizeof(pUuid->id));
1117 unlinkData->unlinkRes[(unlinkData->numOfResults)].res = stackresult;
1118 ++(unlinkData->numOfResults);
1119 OIC_LOG (INFO, TAG, "Out registerResultForUnlinkDevices");
1123 static OCStackResult SendDeleteCredentialRequest(void* ctx,
1124 OCClientResponseHandler respHandler,
1125 const OCProvisionDev_t* revokedDev,
1126 const OCProvisionDev_t* destDev)
1128 OIC_LOG(DEBUG, TAG, "IN SendDeleteCredentialRequest");
1130 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1132 return OC_STACK_INVALID_PARAM;
1136 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1137 if(OC_STACK_OK != ret)
1139 OIC_LOG(ERROR, TAG, "SendDeleteCredentialRequest : Failed to canonical UUID encoding");
1140 return OC_STACK_ERROR;
1143 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1145 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1146 char *srpUri = SRP_FORM_DELETE_CREDENTIAL;
1148 if(CA_ADAPTER_TCP == destDev->endpoint.adapter)
1150 srpUri = SRP_FORM_DELETE_CREDENTIAL_TCP;
1153 snRet = snprintf(reqBuf, sizeof(reqBuf), srpUri, destDev->endpoint.addr,
1154 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1158 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1159 return OC_STACK_ERROR;
1161 else if ((size_t)snRet >= sizeof(reqBuf))
1163 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1164 return OC_STACK_ERROR;
1167 OCCallbackData cbData;
1168 memset(&cbData, 0, sizeof(cbData));
1169 cbData.context = ctx;
1170 cbData.cb = respHandler;
1172 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1174 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1176 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1177 &destDev->endpoint, NULL,
1178 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1179 if (OC_STACK_OK != ret)
1181 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1183 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1188 static OCStackResult SendDeleteACLRequest(void* ctx,
1189 OCClientResponseHandler respHandler,
1190 const OCProvisionDev_t* revokedDev,
1191 const OCProvisionDev_t* destDev)
1193 OIC_LOG(DEBUG, TAG, "IN SendDeleteACLRequest");
1195 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1197 return OC_STACK_INVALID_PARAM;
1201 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1202 if(OC_STACK_OK != ret)
1204 OIC_LOG(ERROR, TAG, "SendDeleteACLRequest : Failed to canonical UUID encoding");
1205 return OC_STACK_ERROR;
1208 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1210 //coaps://0.0.0.0:5684/oic/sec/acl?subjectuuid=(Canonical ENCODED UUID)
1211 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, destDev->endpoint.addr,
1212 destDev->securePort, OIC_RSRC_ACL_URI, OIC_JSON_SUBJECTID_NAME, subID);
1216 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error (snprintf) %d\n", snRet);
1217 return OC_STACK_ERROR;
1219 else if ((size_t)snRet >= sizeof(reqBuf))
1221 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Truncated (snprintf) %d\n", snRet);
1222 return OC_STACK_ERROR;
1225 OCCallbackData cbData;
1226 memset(&cbData, 0, sizeof(cbData));
1227 cbData.context = ctx;
1228 cbData.cb = respHandler;
1230 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1232 OIC_LOG(DEBUG, TAG, "Sending remove ACL request to resource server");
1234 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1235 &destDev->endpoint, NULL,
1236 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1237 if (OC_STACK_OK != ret)
1239 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error in OCDoResource %d", ret);
1241 OIC_LOG(DEBUG, TAG, "OUT SendDeleteACLRequest");
1247 * Callback handler of unlink second device.
1249 * @param[in] ctx ctx value passed to callback from calling function.
1250 * @param[in] handle handle to an invocation
1251 * @param[in] clientResponse Response from queries to remote servers.
1252 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1253 * OC_STACK_KEEP_TRANSACTION to keep it.
1255 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1256 OCClientResponse *clientResponse)
1259 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1260 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1261 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1265 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1266 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1268 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1270 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1274 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1275 unlinkData->resultCallback(unlinkData->ctx,
1276 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1282 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1283 IDX_SECOND_DEVICE_RES);
1284 unlinkData->resultCallback(unlinkData->ctx,
1285 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1286 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1290 //Update provisioning DB when succes case.
1291 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1292 &unlinkData->unlinkDev[1].doxm->deviceID))
1294 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1295 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1296 unlinkData->resultCallback(unlinkData->ctx,
1297 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1300 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1304 DeleteUnlinkData_t(unlinkData);
1305 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1306 return OC_STACK_DELETE_TRANSACTION;
1311 * Callback handler of unlink first device.
1313 * @param[in] ctx ctx value passed to callback from calling function.
1314 * @param[in] handle handle to an invocation
1315 * @param[in] clientResponse Response from queries to remote servers.
1316 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1317 * OC_STACK_KEEP_TRANSACTION to keep it.
1319 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1320 OCClientResponse *clientResponse)
1322 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1323 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1324 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1329 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1330 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1332 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1334 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1336 // Second revocation request to second device.
1337 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1338 &unlinkData->unlinkDev[0],
1339 &unlinkData->unlinkDev[1] /*Dest*/);
1340 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1341 if (OC_STACK_OK != res)
1343 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1344 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1345 IDX_SECOND_DEVICE_RES);
1346 unlinkData->resultCallback(unlinkData->ctx,
1347 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1352 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1353 return OC_STACK_DELETE_TRANSACTION;
1358 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1360 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1361 unlinkData->unlinkRes, true);
1367 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1368 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1369 IDX_FIRST_DEVICE_RES );
1370 unlinkData->resultCallback(unlinkData->ctx,
1371 unlinkData->numOfResults, unlinkData->unlinkRes,
1373 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1377 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1378 DeleteUnlinkData_t(unlinkData);
1379 return OC_STACK_DELETE_TRANSACTION;
1383 * Function to unlink devices.
1384 * This function will remove the credential & relationship between the two devices.
1386 * @param[in] ctx Application context would be returned in result callback
1387 * @param[in] pTargetDev1 first device information to be unlinked.
1388 * @param[in] pTargetDev2 second device information to be unlinked.
1389 * @param[in] resultCallback callback provided by API user, callback will be called when
1390 * device unlink is finished.
1391 * @return OC_STACK_OK in case of success and other value otherwise.
1393 OCStackResult SRPUnlinkDevices(void* ctx,
1394 const OCProvisionDev_t* pTargetDev1,
1395 const OCProvisionDev_t* pTargetDev2,
1396 OCProvisionResultCB resultCallback)
1398 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1400 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1402 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1403 return OC_STACK_INVALID_PARAM;
1405 if (!resultCallback)
1407 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1408 return OC_STACK_INVALID_CALLBACK;
1410 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1412 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1413 return OC_STACK_INVALID_PARAM;
1416 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1417 PMPrintOCProvisionDev(pTargetDev1);
1418 PMPrintOCProvisionDev(pTargetDev2);
1420 // Mark the link status stale
1421 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1422 if (OC_STACK_OK != res)
1424 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1428 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1429 VERIFY_NON_NULL(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1431 //Initialize unlink data
1432 unlinkData->ctx = ctx;
1433 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1434 if (NULL == unlinkData->unlinkDev)
1436 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1437 res = OC_STACK_NO_MEMORY;
1441 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1442 if (NULL == unlinkData->unlinkRes)
1444 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1445 res = OC_STACK_NO_MEMORY;
1449 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1450 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1452 unlinkData->numOfResults = 0;
1453 unlinkData->resultCallback = resultCallback;
1455 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1456 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1457 if (OC_STACK_OK != res)
1459 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1466 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1467 DeleteUnlinkData_t(unlinkData);
1471 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1475 OICFree(pRemoveData->revokeTargetDev);
1476 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1477 OICFree(pRemoveData->removeRes);
1478 OICFree(pRemoveData);
1482 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1483 OCStackResult stackresult, bool hasError)
1485 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1486 removeData->numOfResults + 1);
1489 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1490 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1494 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1495 0, sizeof(pLinkedDevId->id) );
1497 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1498 removeData->hasError = hasError;
1499 ++(removeData->numOfResults);
1501 // If we get suffcient result from linked devices, we have to call user callback and do free
1502 if (removeData->sizeOfResArray == removeData->numOfResults)
1504 if(!removeData->hasError)
1506 // Remove device info from prvisioning database
1507 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1509 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1510 removeData->hasError = true;
1513 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1514 removeData->hasError);
1515 DeleteRemoveData_t(removeData);
1519 static void registerResultForResetDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1520 OCStackResult stackresult, bool hasError)
1522 OIC_LOG_V(INFO, TAG, "Inside registerResultForResetDevice removeData->numOfResults is %zu\n",
1523 removeData->numOfResults + 1);
1526 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1527 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1531 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1532 0, sizeof(pLinkedDevId->id) );
1534 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1535 removeData->hasError = hasError;
1536 ++(removeData->numOfResults);
1538 // If we get suffcient result from linked devices, we have to call user callback and do free
1539 if (removeData->sizeOfResArray == removeData->numOfResults)
1541 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1542 removeData->hasError);
1543 DeleteRemoveData_t(removeData);
1548 * Callback handler of unlink first device.
1550 * @param[in] ctx ctx value passed to callback from calling function.
1551 * @param[in] handle handle to an invocation
1552 * @param[in] clientResponse Response from queries to remote servers.
1553 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1554 * and OC_STACK_KEEP_TRANSACTION to keep it.
1556 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1557 OCClientResponse *clientResponse)
1559 //Update the delete credential into delete device context
1560 //Save the deleted status in delDevCtx
1562 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1563 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1564 OCStackResult res = OC_STACK_ERROR;
1566 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1570 OicUuid_t revDevUuid = {.id={0}};
1571 if(UUID_LENGTH == clientResponse->identity.id_length)
1573 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1574 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1576 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1577 if (OC_STACK_OK != res)
1579 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1580 registerResultForRemoveDevice(removeData, &revDevUuid,
1581 OC_STACK_INCONSISTENT_DB, true);
1583 return OC_STACK_DELETE_TRANSACTION;
1586 registerResultForRemoveDevice(removeData, &revDevUuid,
1587 OC_STACK_RESOURCE_DELETED, false);
1591 registerResultForRemoveDevice(removeData, &revDevUuid,
1592 clientResponse->result, true);
1593 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1598 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1599 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1601 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1604 * Since server's credential was deleted,
1605 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1607 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1608 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1612 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1618 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1619 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1623 return OC_STACK_DELETE_TRANSACTION;
1627 * Callback handler of reset device.
1629 * @param[in] ctx ctx value passed to callback from calling function.
1630 * @param[in] handle handle to an invocation
1631 * @param[in] clientResponse Response from queries to remote servers.
1632 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1633 * and OC_STACK_KEEP_TRANSACTION to keep it.
1635 static OCStackApplicationResult SRPSyncDeviceCredCB(void *delDevCtx, OCDoHandle handle,
1636 OCClientResponse *clientResponse)
1638 //Update the delete credential into delete device context
1639 //Save the deleted status in delDevCtx
1641 OIC_LOG_V(INFO, TAG, "Inside SRPSyncDeviceCredCB.");
1642 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1643 OCStackResult res = OC_STACK_ERROR;
1645 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1646 OCProvisionDev_t * pTargetDev = PMCloneOCProvisionDev(removeData->revokeTargetDev);
1647 OCProvisionResultCB resultCallback = removeData->resultCallback;
1650 OicUuid_t revDevUuid = {.id={0}};
1651 if(UUID_LENGTH == clientResponse->identity.id_length)
1653 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1654 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1656 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1657 if (OC_STACK_OK != res)
1659 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1660 registerResultForResetDevice(removeData, &revDevUuid,
1661 OC_STACK_INCONSISTENT_DB, true);
1663 return OC_STACK_DELETE_TRANSACTION;
1666 registerResultForResetDevice(removeData, &revDevUuid,
1667 OC_STACK_RESOURCE_DELETED, false);
1671 registerResultForResetDevice(removeData, &revDevUuid,
1672 clientResponse->result, false);
1673 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1678 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1679 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1681 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1684 * Since server's credential was deleted,
1685 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1687 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1688 registerResultForResetDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1692 registerResultForResetDevice(removeData, NULL, clientResponse->result, true);
1698 registerResultForResetDevice(removeData, NULL, OC_STACK_ERROR, true);
1699 OIC_LOG(ERROR, TAG, "SRPSyncDevice received Null clientResponse");
1702 SRPResetDevice(pTargetDev, resultCallback);
1704 return OC_STACK_DELETE_TRANSACTION;
1708 * Callback handler of reset device sync-up
1710 * @param[in] ctx ctx value passed to callback from calling function.
1711 * @param[in] handle handle to an invocation
1712 * @param[in] clientResponse Response from queries to remote servers.
1713 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1714 * and OC_STACK_KEEP_TRANSACTION to keep it.
1716 static OCStackApplicationResult SRPSyncDeviceACLCB(void *ctx, OCDoHandle handle,
1717 OCClientResponse *clientResponse)
1719 return OC_STACK_DELETE_TRANSACTION;
1723 * Callback handler of device remote reset.
1725 * @param[in] ctx ctx value passed to callback from calling function.
1726 * @param[in] handle handle to an invocation
1727 * @param[in] clientResponse Response from queries to remote servers.
1728 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1729 * and OC_STACK_KEEP_TRANSACTION to keep it.
1731 static OCStackApplicationResult SRPResetDeviceCB(void *ctx, OCDoHandle handle,
1732 OCClientResponse *clientResponse)
1734 OIC_LOG(DEBUG, TAG, "IN SRPResetDeviceCB");
1735 if(OC_STACK_OK == clientResponse->result)
1737 OIC_LOG(DEBUG, TAG, "Change Target Device Pstat Cm SUCCEEDED");
1740 // Delete Cred and ACL related to the target device.
1741 const OicSecCred_t *cred = NULL;
1742 OCProvisionDev_t * pTargetDev = (OCProvisionDev_t *)ctx;
1743 cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
1746 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to get credential of target device.");
1750 OCStackResult res = RemoveCredential(&cred->subject);
1751 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1753 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove credential.");
1757 res = RemoveACE(&cred->subject, NULL);
1758 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1760 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove ACL.");
1763 if (OC_STACK_OK != PDMDeleteDevice(&pTargetDev->doxm->deviceID))
1765 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to delete device from PDM");
1768 //Close the DTLS session of the reset device.
1769 CAEndpoint_t* endpoint = (CAEndpoint_t *)&clientResponse->devAddr;
1770 CAResult_t caResult = CACloseDtlsSession(endpoint);
1771 if(CA_STATUS_OK != caResult)
1773 OIC_LOG_V(WARNING, TAG, "OCResetDevice : Failed to close DTLS session : %d", caResult);
1777 * If there is no linked device, PM does not send any request.
1778 * So we should directly invoke the result callback to inform the result of OCResetDevice.
1780 if(OC_STACK_NO_RESOURCE == res)
1786 OICFree(pTargetDev);
1787 return OC_STACK_DELETE_TRANSACTION;
1791 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1792 OCProvisionDev_t* pOwnedDevList,
1793 OCUuidList_t* pLinkedUuidList,
1794 OCProvisionDev_t** ppLinkedDevList,
1795 size_t *numOfLinkedDev)
1797 // pOwnedDevList could be NULL. It means no alived and owned device now.
1798 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1799 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1801 return OC_STACK_INVALID_PARAM;
1805 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1806 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1808 // Mark the link status stale.
1809 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1810 if (OC_STACK_OK != res)
1812 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1813 return OC_STACK_INCONSISTENT_DB;
1818 // If this linked device is alive (power-on), add the deivce to the list.
1819 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1820 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1822 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1824 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1825 if (NULL == targetDev)
1827 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1828 return OC_STACK_NO_MEMORY;
1831 LL_PREPEND(*ppLinkedDevList, targetDev);
1838 *numOfLinkedDev = cnt;
1843 * Function to device revocation
1844 * This function will remove credential of target device from all devices in subnet.
1846 * @param[in] ctx Application context would be returned in result callback
1847 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1848 * @param[in] pTargetDev Device information to be revoked.
1849 * @param[in] resultCallback callback provided by API user, callback will be called when
1850 * credential revocation is finished.
1851 * @return OC_STACK_OK in case of success and other value otherwise.
1852 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1853 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1855 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1856 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1858 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1860 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1862 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1863 return OC_STACK_INVALID_PARAM;
1865 if (!resultCallback)
1867 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
1868 return OC_STACK_INVALID_CALLBACK;
1871 // Declare variables in here to handle error cases with goto statement.
1872 OCProvisionDev_t* pOwnedDevList = NULL;
1873 OCProvisionDev_t* pLinkedDevList = NULL;
1874 RemoveData_t* removeData = NULL;
1876 //1. Find all devices that has a credential of the revoked device
1877 OCUuidList_t* pLinkedUuidList = NULL;
1878 size_t numOfDevices = 0;
1879 OCStackResult res = OC_STACK_ERROR;
1880 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
1881 if (OC_STACK_OK != res)
1883 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
1886 // if there is no related device, we can skip further process.
1887 if (0 == numOfDevices)
1889 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
1890 res = OC_STACK_CONTINUE;
1894 //2. Find owned device from the network
1895 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
1896 if (OC_STACK_OK != res)
1898 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
1902 //3. Make a list of devices to send DELETE credential request
1903 // by comparing owned devices from provisioning database with mutlicast discovery result.
1904 size_t numOfLinkedDev = 0;
1905 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
1906 &pLinkedDevList, &numOfLinkedDev);
1907 if (OC_STACK_OK != res)
1909 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
1912 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
1913 { // So we don't have to send request message.
1914 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
1915 res = OC_STACK_CONTINUE;
1919 // 4. Prepare RemoveData Context data.
1920 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
1923 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1924 res = OC_STACK_NO_MEMORY;
1928 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
1929 if (!removeData->revokeTargetDev)
1931 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
1932 res = OC_STACK_NO_MEMORY;
1936 removeData->removeRes =
1937 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
1938 if (!removeData->removeRes)
1940 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1941 res = OC_STACK_NO_MEMORY;
1945 removeData->ctx = ctx;
1946 removeData->linkedDevList = pLinkedDevList;
1947 removeData->resultCallback = resultCallback;
1948 removeData->numOfResults = 0;
1949 removeData->sizeOfResArray = numOfLinkedDev;
1950 removeData->hasError = false;
1952 // 5. Send DELETE credential request to linked devices.
1953 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1954 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
1955 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
1957 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
1958 removeData->revokeTargetDev, curDev);
1959 if (OC_STACK_OK != res)
1961 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
1962 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
1966 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
1970 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
1971 PMDeleteDeviceList(pOwnedDevList);
1972 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
1974 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
1977 PDMDestoryOicUuidLinkList(pLinkedUuidList);
1978 PMDeleteDeviceList(pOwnedDevList);
1979 PMDeleteDeviceList(pLinkedDevList);
1982 OICFree(removeData->revokeTargetDev);
1983 OICFree(removeData->removeRes);
1984 OICFree(removeData);
1986 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");
1991 * Function to device revocation
1992 * This function will remove credential of target device from all devices in subnet.
1994 * @param[in] ctx Application context would be returned in result callback
1995 * @param[in] pOwnedDevList List of owned devices
1996 * @param[in] pTargetDev Device information to be revoked.
1997 * @param[in] resultCallback callback provided by API user, callback will be called when
1998 * credential revocation is finished.
1999 * @return OC_STACK_OK in case of success and other value otherwise.
2000 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2001 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2003 OCStackResult SRPRemoveDeviceWithoutDiscovery(void* ctx, const OCProvisionDev_t* pOwnedDevList,
2004 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2006 OIC_LOG(INFO, TAG, "IN SRPRemoveDeviceWithoutDiscovery");
2008 if (!pTargetDev || !pOwnedDevList)
2010 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL parameters");
2011 return OC_STACK_INVALID_PARAM;
2013 if (!resultCallback)
2015 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL Callback");
2016 return OC_STACK_INVALID_CALLBACK;
2019 // Declare variables in here to handle error cases with goto statement.
2020 OCProvisionDev_t* pLinkedDevList = NULL;
2021 RemoveData_t* removeData = NULL;
2023 //1. Find all devices that has a credential of the revoked device
2024 OCUuidList_t* pLinkedUuidList = NULL;
2025 size_t numOfDevices = 0;
2026 OCStackResult res = OC_STACK_ERROR;
2027 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2028 if (OC_STACK_OK != res)
2030 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to get linked devices information");
2033 // if there is no related device, we can skip further process.
2034 if (0 == numOfDevices)
2036 OIC_LOG(DEBUG, TAG, "SRPRemoveDeviceWithoutDiscovery : No linked device found.");
2037 res = OC_STACK_CONTINUE;
2041 //2. Make a list of devices to send DELETE credential request
2042 // by comparing owned devices from provisioning database with mutlicast discovery result.
2043 size_t numOfLinkedDev = 0;
2044 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2045 &pLinkedDevList, &numOfLinkedDev);
2046 if (OC_STACK_OK != res)
2048 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : GetListofDevToReqDeleteCred() failed");
2051 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2052 { // So we don't have to send request message.
2053 OIC_LOG(DEBUG, TAG, "SRPRemoveDeviceWithoutDiscovery : No alived & linked device found.");
2054 res = OC_STACK_CONTINUE;
2058 // 3. Prepare RemoveData Context data.
2059 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2062 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2063 res = OC_STACK_NO_MEMORY;
2067 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2068 if (!removeData->revokeTargetDev)
2070 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : PMCloneOCProvisionDev Failed");
2071 res = OC_STACK_NO_MEMORY;
2075 removeData->removeRes =
2076 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2077 if (!removeData->removeRes)
2079 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2080 res = OC_STACK_NO_MEMORY;
2084 removeData->ctx = ctx;
2085 removeData->linkedDevList = pLinkedDevList;
2086 removeData->resultCallback = resultCallback;
2087 removeData->numOfResults = 0;
2088 removeData->sizeOfResArray = numOfLinkedDev;
2089 removeData->hasError = false;
2091 // 5. Send DELETE credential request to linked devices.
2092 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2093 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2094 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2096 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2097 removeData->revokeTargetDev, curDev);
2098 if (OC_STACK_OK != res)
2100 OIC_LOG_V(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Fail to send the DELETE credential request to\
2101 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2105 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2109 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2110 OIC_LOG(INFO, TAG, "OUT SRPRemoveDeviceWithoutDiscovery");
2112 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2115 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2116 PMDeleteDeviceList(pLinkedDevList);
2119 OICFree(removeData->revokeTargetDev);
2120 OICFree(removeData->removeRes);
2121 OICFree(removeData);
2123 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDeviceWithoutDiscovery");
2128 * Function to sync-up credential and ACL of the target device.
2129 * This function will remove credential and ACL of target device from all devices in subnet.
2131 * @param[in] ctx Application context would be returned in result callback
2132 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
2133 * @param[in] pTargetDev Device information to be revoked.
2134 * @param[in] resultCallback callback provided by API user, callback will be called when
2135 * credential revocation is finished.
2136 * when there is an error, this user callback is called immediately.
2137 * @return OC_STACK_OK in case of success and other value otherwise.
2138 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2139 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2141 OCStackResult SRPSyncDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
2142 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2144 OIC_LOG(INFO, TAG, "IN SRPSyncDevice");
2145 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
2147 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL parameters");
2148 return OC_STACK_INVALID_PARAM;
2150 if (!resultCallback)
2152 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL Callback");
2153 return OC_STACK_INVALID_CALLBACK;
2156 // Declare variables in here to handle error cases with goto statement.
2157 OCProvisionDev_t* pOwnedDevList = NULL;
2158 OCProvisionDev_t* pLinkedDevList = NULL;
2159 RemoveData_t* removeData = NULL;
2161 //1. Find all devices that has a credential of the revoked device
2162 OCUuidList_t* pLinkedUuidList = NULL;
2163 size_t numOfDevices = 0;
2164 OCStackResult res = OC_STACK_ERROR;
2165 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2166 if (OC_STACK_OK != res)
2168 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to get linked devices information");
2171 // if there is no related device, we can skip further process.
2172 if (0 == numOfDevices)
2174 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No linked device found.");
2175 res = OC_STACK_CONTINUE;
2179 //2. Find owned device from the network
2180 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
2181 if (OC_STACK_OK != res)
2183 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to PMDeviceDiscovery");
2187 //3. Make a list of devices to send DELETE credential request
2188 // by comparing owned devices from provisioning database with mutlicast discovery result.
2189 size_t numOfLinkedDev = 0;
2190 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2191 &pLinkedDevList, &numOfLinkedDev);
2192 if (OC_STACK_OK != res)
2194 OIC_LOG(ERROR, TAG, "SRPSyncDevice : GetListofDevToReqDeleteCred() failed");
2197 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2198 { // So we don't have to send request message.
2199 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No alived & linked device found.");
2200 res = OC_STACK_CONTINUE;
2204 // 4. Prepare RemoveData Context data.
2205 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2208 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to memory allocation");
2209 res = OC_STACK_NO_MEMORY;
2213 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2214 if (!removeData->revokeTargetDev)
2216 OIC_LOG(ERROR, TAG, "SRPSyncDevice : PMCloneOCProvisionDev Failed");
2217 res = OC_STACK_NO_MEMORY;
2221 removeData->removeRes =
2222 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2223 if (!removeData->removeRes)
2225 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to allocate memory");
2226 res = OC_STACK_NO_MEMORY;
2230 removeData->ctx = ctx;
2231 removeData->linkedDevList = pLinkedDevList;
2232 removeData->resultCallback = resultCallback;
2233 removeData->numOfResults = 0;
2234 removeData->sizeOfResArray = numOfLinkedDev;
2235 removeData->hasError = false;
2237 // 5. Send DELETE credential request to linked devices.
2238 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2239 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2240 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2242 res = SendDeleteACLRequest((void*)removeData, &SRPSyncDeviceACLCB,
2243 removeData->revokeTargetDev, curDev);
2244 if (OC_STACK_OK != res)
2246 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE ACL request to\
2247 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2250 res = SendDeleteCredentialRequest((void*)removeData, &SRPSyncDeviceCredCB,
2251 removeData->revokeTargetDev, curDev);
2252 if (OC_STACK_OK != res)
2254 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE credential request to\
2255 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2256 totalRes = OC_STACK_ERROR;
2260 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2264 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2265 PMDeleteDeviceList(pOwnedDevList);
2266 OIC_LOG(INFO, TAG, "OUT SRPSyncDevice");
2268 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2271 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2272 PMDeleteDeviceList(pOwnedDevList);
2273 PMDeleteDeviceList(pLinkedDevList);
2276 OICFree(removeData->revokeTargetDev);
2277 OICFree(removeData->removeRes);
2278 OICFree(removeData);
2280 OIC_LOG(INFO, TAG, "OUT ERROR case SRPSyncDevice");
2285 * Function for remote reset
2286 * This function will send pstat PUT message to the target device to initiate remote reset.
2288 * @param[in] pTargetDev Device information to be revoked.
2289 * @param[in] resultCallback callback provided by API user, callback will be called when
2290 * credential revocation is finished.
2291 * when there is an error, this user callback is called immediately.
2292 * @return OC_STACK_OK in case of success and other value otherwise.
2293 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2294 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2296 OCStackResult SRPResetDevice(const OCProvisionDev_t* pTargetDev,
2297 OCProvisionResultCB resultCallback)
2299 OIC_LOG(INFO, TAG, "IN SRPResetDevice");
2302 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL parameters");
2303 return OC_STACK_INVALID_PARAM;
2305 if (!resultCallback)
2307 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL Callback");
2308 return OC_STACK_INVALID_CALLBACK;
2311 OCStackResult res = OC_STACK_ERROR;
2312 OicSecPstat_t * pstat = (OicSecPstat_t *) OICCalloc(1, sizeof(OicSecPstat_t));
2315 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2316 return OC_STACK_NO_MEMORY;
2320 pstat->isOp = false;
2321 memcpy(pstat->deviceID.id, pTargetDev->doxm->deviceID.id, sizeof(OicUuid_t));
2322 pstat->tm = TAKE_OWNER;
2323 pstat->om = (OicSecDpom_t)(SINGLE_SERVICE_SERVER_DRIVEN | MULTIPLE_SERVICE_CLIENT_DRIVEN);
2325 pstat->sm = (OicSecDpom_t *) OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
2326 if (NULL == pstat->sm)
2328 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2330 return OC_STACK_NO_MEMORY;
2332 pstat->sm[0] = (OicSecDpom_t)(SINGLE_SERVICE_SERVER_DRIVEN | MULTIPLE_SERVICE_CLIENT_DRIVEN);
2334 OCSecurityPayload * secPayload = (OCSecurityPayload *) OICCalloc(1, sizeof(OCSecurityPayload));
2337 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2338 res = OC_STACK_NO_MEMORY;
2341 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2343 if (OC_STACK_OK != PstatToCBORPayload(pstat, &(secPayload->securityData),
2344 &(secPayload->payloadSize), true))
2346 OCPayloadDestroy((OCPayload *) secPayload);
2347 OIC_LOG(ERROR, TAG, "Failed to PstatToCBORPayload");
2348 res = OC_STACK_NO_MEMORY;
2351 OIC_LOG(DEBUG, TAG, "Created payload for pstat set");
2352 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2354 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2355 if (!PMGenerateQuery(true,
2356 pTargetDev->endpoint.addr,
2357 pTargetDev->securePort,
2358 pTargetDev->connType,
2359 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2361 OIC_LOG(ERROR, TAG, "SRPResetDevice : Failed to generate query");
2362 OCPayloadDestroy((OCPayload *) secPayload);
2363 res = OC_STACK_ERROR;
2366 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2368 OCProvisionDev_t * targetDev = PMCloneOCProvisionDev(pTargetDev);
2369 OCCallbackData cbData = { .context = NULL, .cb = NULL, .cd = NULL };
2370 cbData.cb = &SRPResetDeviceCB;
2371 cbData.context = (void *) targetDev;
2373 OCMethod method = OC_REST_POST;
2374 OCDoHandle handle = NULL;
2375 OIC_LOG(DEBUG, TAG, "Sending PSTAT info to resource server");
2376 res = OCDoResource(&handle, method, query,
2377 &targetDev->endpoint, (OCPayload *)secPayload,
2378 targetDev->connType, OC_LOW_QOS, &cbData, NULL, 0);\
2379 if (OC_STACK_OK != res)
2381 OIC_LOG(ERROR, TAG, "OCStack resource error");
2387 OIC_LOG(INFO, TAG, "OUT SRPResetDevice");
2392 * Internal Function to store results in result array during GetCredResourceCB.
2394 static void registerResultForGetCredResourceCB(GetSecData_t *GetSecData,
2395 OCStackResult stackresult)
2397 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetCredResourceCB "
2398 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2399 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2400 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2401 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2402 ++(GetSecData->numOfResults);
2406 * Callback handler of SRPGetCredResource.
2408 * @param[in] ctx ctx value passed to callback from calling function.
2409 * @param[in] UNUSED handle to an invocation
2410 * @param[in] clientResponse Response from queries to remote servers.
2411 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2412 * and OC_STACK_KEEP_TRANSACTION to keep it.
2414 static OCStackApplicationResult SRPGetCredResourceCB(void *ctx, OCDoHandle UNUSED,
2415 OCClientResponse *clientResponse)
2417 OIC_LOG_V(INFO, TAG, "Inside SRPGetCredResourceCB.");
2419 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2420 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2421 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2425 if(OC_STACK_OK == clientResponse->result)
2427 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2428 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2430 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2432 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2433 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2436 OICFree(GetSecData->resArr);
2437 OICFree(GetSecData);
2439 return OC_STACK_DELETE_TRANSACTION;
2442 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2443 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2446 OIC_LOG_V(ERROR, TAG, "SRPGetCredResourceCB received Null clientResponse");
2447 OICFree(GetSecData->resArr);
2448 OICFree(GetSecData);
2450 return OC_STACK_DELETE_TRANSACTION;
2454 OCStackResult SRPGetCredResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2455 OCProvisionResultCB resultCallback)
2457 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2458 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2460 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2461 if(!PMGenerateQuery(true,
2462 selectedDeviceInfo->endpoint.addr,
2463 selectedDeviceInfo->securePort,
2464 selectedDeviceInfo->connType,
2465 query, sizeof(query), OIC_RSRC_CRED_URI))
2467 OIC_LOG(ERROR, TAG, "SRPGetCredResource : Failed to generate query");
2468 return OC_STACK_ERROR;
2470 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2472 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2473 cbData.cb = &SRPGetCredResourceCB;
2474 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2475 if (NULL == GetSecData)
2477 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2478 return OC_STACK_NO_MEMORY;
2480 GetSecData->deviceInfo = selectedDeviceInfo;
2481 GetSecData->resultCallback = resultCallback;
2482 GetSecData->numOfResults=0;
2483 GetSecData->ctx = ctx;
2485 int noOfRiCalls = 1;
2486 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2487 if (NULL == GetSecData->resArr)
2489 OICFree(GetSecData);
2490 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2491 return OC_STACK_NO_MEMORY;
2493 cbData.context = (void *)GetSecData;
2495 OCMethod method = OC_REST_GET;
2496 OCDoHandle handle = NULL;
2497 OIC_LOG(DEBUG, TAG, "Sending Get Cred to resource server");
2498 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2499 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2500 if (OC_STACK_OK != ret)
2502 OIC_LOG(ERROR, TAG, "OCStack resource error");
2503 OICFree(GetSecData->resArr);
2504 OICFree(GetSecData);
2506 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2507 OIC_LOG(DEBUG, TAG, "OUT SRPGetCredResource");
2513 * Internal Function to store results in result array during GetACLResourceCB.
2515 static void registerResultForGetACLResourceCB(GetSecData_t *GetSecData,
2516 OCStackResult stackresult)
2518 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetACLResourceCB "
2519 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2520 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2521 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2522 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2523 ++(GetSecData->numOfResults);
2527 * Callback handler of SRPGetACLResource.
2529 * @param[in] ctx ctx value passed to callback from calling function.
2530 * @param[in] UNUSED handle to an invocation
2531 * @param[in] clientResponse Response from queries to remote servers.
2532 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2533 * and OC_STACK_KEEP_TRANSACTION to keep it.
2535 static OCStackApplicationResult SRPGetACLResourceCB(void *ctx, OCDoHandle UNUSED,
2536 OCClientResponse *clientResponse)
2538 OIC_LOG_V(INFO, TAG, "Inside SRPGetACLResourceCB.");
2540 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2541 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2542 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2546 if(OC_STACK_OK == clientResponse->result)
2548 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2549 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2551 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2553 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2554 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2557 OICFree(GetSecData->resArr);
2558 OICFree(GetSecData);
2560 return OC_STACK_DELETE_TRANSACTION;
2563 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2564 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2567 OIC_LOG_V(ERROR, TAG, "SRPGetACLResourceCB received Null clientResponse");
2568 OICFree(GetSecData->resArr);
2569 OICFree(GetSecData);
2571 return OC_STACK_DELETE_TRANSACTION;
2575 OCStackResult SRPGetACLResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2576 OCProvisionResultCB resultCallback)
2578 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2579 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2581 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2582 if(!PMGenerateQuery(true,
2583 selectedDeviceInfo->endpoint.addr,
2584 selectedDeviceInfo->securePort,
2585 selectedDeviceInfo->connType,
2586 query, sizeof(query), OIC_RSRC_ACL_URI))
2588 OIC_LOG(ERROR, TAG, "SRPGetACLResource : Failed to generate query");
2589 return OC_STACK_ERROR;
2591 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2593 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2594 cbData.cb = &SRPGetACLResourceCB;
2595 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2596 if (NULL == GetSecData)
2598 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2599 return OC_STACK_NO_MEMORY;
2601 GetSecData->deviceInfo = selectedDeviceInfo;
2602 GetSecData->resultCallback = resultCallback;
2603 GetSecData->numOfResults=0;
2604 GetSecData->ctx = ctx;
2606 int noOfRiCalls = 1;
2607 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2608 if (NULL == GetSecData->resArr)
2610 OICFree(GetSecData);
2611 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2612 return OC_STACK_NO_MEMORY;
2614 cbData.context = (void *)GetSecData;
2616 OCMethod method = OC_REST_GET;
2617 OCDoHandle handle = NULL;
2618 OIC_LOG(DEBUG, TAG, "Sending Get ACL to resource server");
2619 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2620 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2621 if (OC_STACK_OK != ret)
2623 OIC_LOG(ERROR, TAG, "OCStack resource error");
2624 OICFree(GetSecData->resArr);
2625 OICFree(GetSecData);
2627 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2628 OIC_LOG(DEBUG, TAG, "OUT SRPGetACLResource");