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 * *****************************************************************/
20 #include "iotivity_config.h"
28 #include "ocprovisioningmanager.h"
29 #include "secureresourceprovider.h"
31 #include "oic_malloc.h"
32 #include "aclresource.h"
33 #include "pstatresource.h"
34 #include "srmresourcestrings.h"
35 #include "credresource.h"
36 #include "doxmresource.h"
37 #include "pconfresource.h"
38 #include "credentialgenerator.h"
39 #include "cainterface.h"
42 #include "pmutility.h"
43 #include "provisioningdatabasemanager.h"
46 #include "ocpayload.h"
49 #include "crlresource.h"
55 * Macro to verify argument is not equal to NULL.
56 * eg: VERIFY_NON_NULL(TAG, ptrData, ERROR,OC_STACK_ERROR);
58 #define VERIFY_NON_NULL(tag, arg, logLevel, retValue) { if (NULL == (arg)) \
59 { OIC_LOG((logLevel), tag, #arg " is NULL"); return retValue; } }
62 * Macro to verify success of operation.
63 * eg: VERIFY_SUCCESS(TAG, OC_STACK_OK == foo(), ERROR, OC_STACK_ERROR);
65 #define VERIFY_SUCCESS(tag, op, logLevel, retValue) { if (!(op)) \
66 {OIC_LOG((logLevel), tag, #op " failed!!"); return retValue;} }
69 * Structure to carry credential data to callback.
71 typedef struct CredentialData CredentialData_t;
74 void *ctx; /**< Pointer to user context.**/
75 const OCProvisionDev_t *deviceInfo1; /**< Pointer to OCProvisionDev_t.**/
76 const OCProvisionDev_t *deviceInfo2; /**< Pointer to OCProvisionDev_t.**/
77 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
78 OicSecCred_t *credInfoFirst; /**< Pointer to OicSecCred_t.**/
79 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
80 OCProvisionResult_t *resArr; /**< Result array.**/
81 int numOfResults; /**< Number of results in result array.**/
85 * Structure to carry ACL provision API data to callback.
87 typedef struct ACLData ACLData_t;
90 void *ctx; /**< Pointer to user context.**/
91 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
92 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
93 OCProvisionResult_t *resArr; /**< Result array.**/
94 int numOfResults; /**< Number of results in result array.**/
97 // Structure to carry get security resource APIs data to callback.
98 typedef struct GetSecData GetSecData_t;
101 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
102 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
103 OCProvisionResult_t *resArr; /**< Result array.**/
104 int numOfResults; /**< Number of results in result array.**/
108 * Structure to carry PCONF provision API data to callback.
110 typedef struct PconfData PconfData_t;
113 void *ctx; /**< Pointer to user context.**/
114 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
115 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
116 OCProvisionResult_t *resArr; /**< Result array.**/
117 int numOfResults; /**< Number of results in result array.**/
120 // Enum type index for unlink callback.
122 IDX_FIRST_DEVICE_RES = 0, // index for resulf of the first device
123 IDX_SECOND_DEVICE_RES, // index for result of the second device
124 IDX_DB_UPDATE_RES // index for result of updating provisioning database.
127 // Structure to carry unlink APIs data to callback.
128 typedef struct UnlinkData UnlinkData_t;
131 OCProvisionDev_t* unlinkDev; /**< Pointer to OCProvisionDev_t to be unlinked.**/
132 OCProvisionResult_t* unlinkRes; /**< Result array.**/
133 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
134 int numOfResults; /**< Number of results in result array.**/
137 //Example of DELETE cred request -> coaps://0.0.0.0:5684/oic/sec/cred?sub=(BASE64 ENCODED UUID)
138 const char * SRP_FORM_DELETE_CREDENTIAL = "coaps://[%s]:%d%s?%s=%s";
139 const char * SRP_FORM_DELETE_CREDENTIAL_TCP = "coaps+tcp://[%s]:%d%s?%s=%s";
141 // Structure to carry remove APIs data to callback.
142 typedef struct RemoveData RemoveData_t;
145 OCProvisionDev_t* revokeTargetDev; /**< Device which is going to be revoked..**/
146 OCProvisionDev_t* linkedDevList; /**< A list of devices which have invalid credential.**/
147 OCProvisionResult_t* removeRes; /**< Result array.**/
148 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
149 size_t numOfResults; /**< Number of results in result array.**/
150 size_t sizeOfResArray;
157 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
158 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
159 OCClientResponseHandler responseHandler);
163 * Internal function to update result in result array.
165 static void registerResultForCredProvisioning(CredentialData_t *credData,
166 OCStackResult stackresult, int cause)
169 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",credData->numOfResults);
172 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
173 credData->deviceInfo1->doxm->deviceID.id,UUID_LENGTH);
177 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
178 credData->deviceInfo2->doxm->deviceID.id,UUID_LENGTH);
180 credData->resArr[(credData->numOfResults)].res = stackresult;
181 ++(credData->numOfResults);
185 * Callback handler for handling callback of provisioning device 2.
187 * @param[in] ctx ctx value passed to callback from calling function.
188 * @param[in] UNUSED handle to an invocation
189 * @param[in] clientResponse Response from queries to remote servers.
190 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
191 * and OC_STACK_KEEP_TRANSACTION to keep it.
193 static OCStackApplicationResult provisionCredentialCB2(void *ctx, OCDoHandle UNUSED,
194 OCClientResponse *clientResponse)
196 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
197 CredentialData_t *credData = (CredentialData_t *) ctx;
200 OCProvisionResultCB resultCallback = credData->resultCallback;
201 OIC_LOG(INFO, TAG, "provisionCredentialCB2 called");
204 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
206 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CHANGED, 2);
207 OCStackResult res = PDMLinkDevices(&credData->deviceInfo1->doxm->deviceID,
208 &credData->deviceInfo2->doxm->deviceID);
209 if (OC_STACK_OK != res)
211 OIC_LOG(ERROR, TAG, "Error occured on PDMLinkDevices");
212 return OC_STACK_DELETE_TRANSACTION;
214 OIC_LOG(INFO, TAG, "Link created successfully");
216 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
219 OICFree(credData->resArr);
221 return OC_STACK_DELETE_TRANSACTION;
225 OIC_LOG(INFO, TAG, "provisionCredentialCB2 received Null clientResponse");
226 registerResultForCredProvisioning(credData, OC_STACK_ERROR, 2);
227 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
230 OICFree(credData->resArr);
232 return OC_STACK_DELETE_TRANSACTION;
236 * Callback handler for handling callback of provisioning device 1.
238 * @param[in] ctx ctx value passed to callback from calling function.
239 * @param[in] UNUSED handle to an invocation
240 * @param[in] clientResponse Response from queries to remote servers.
241 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
242 * and OC_STACK_KEEP_TRANSACTION to keep it.
244 static OCStackApplicationResult provisionCredentialCB1(void *ctx, OCDoHandle UNUSED,
245 OCClientResponse *clientResponse)
247 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
249 CredentialData_t* credData = (CredentialData_t*) ctx;
250 OICFree(credData->credInfoFirst);
251 const OCProvisionDev_t *deviceInfo = credData->deviceInfo2;
252 OicSecCred_t *credInfo = credData->credInfo;
253 const OCProvisionResultCB resultCallback = credData->resultCallback;
256 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
258 // send credentials to second device
259 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CHANGED,1);
260 OCStackResult res = provisionCredentials(credInfo, deviceInfo, credData,
261 provisionCredentialCB2);
262 DeleteCredList(credInfo);
263 if (OC_STACK_OK != res)
265 registerResultForCredProvisioning(credData, res,2);
266 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
269 OICFree(credData->resArr);
276 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
277 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
280 OICFree(credData->resArr);
287 OIC_LOG(INFO, TAG, "provisionCredentialCB received Null clientResponse for first device");
288 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
289 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
292 DeleteCredList(credInfo);
293 OICFree(credData->resArr);
297 return OC_STACK_DELETE_TRANSACTION;
303 * Internal function for handling credential generation and sending credential to resource server.
305 * @param[in] cred Instance of cred resource.
306 * @param[in] deviceInfo information about device to which credential is to be provisioned.
307 * @param[in] responseHandler callbak called by OC stack when request API receives response.
308 * @return OC_STACK_OK in case of success and other value otherwise.
310 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
311 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
312 OCClientResponseHandler responseHandler)
314 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
317 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
318 return OC_STACK_NO_MEMORY;
320 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
322 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
323 &secPayload->payloadSize, secureFlag);
324 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
326 OCPayloadDestroy((OCPayload *)secPayload);
327 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
328 return OC_STACK_NO_MEMORY;
331 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
332 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
333 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
334 if(!PMGenerateQuery(true,
335 deviceInfo->endpoint.addr,
336 deviceInfo->securePort,
337 deviceInfo->connType,
338 query, sizeof(query), OIC_RSRC_CRED_URI))
340 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
341 return OC_STACK_ERROR;
343 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
345 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
346 cbData.cb = responseHandler;
347 cbData.context = (void *) credData;
350 OCDoHandle handle = NULL;
351 OCMethod method = OC_REST_POST;
352 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
353 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
354 OIC_LOG_V(INFO, TAG, "OCDoResource::Credential provisioning returned : %d",ret);
355 if (ret != OC_STACK_OK)
357 OIC_LOG(ERROR, TAG, "OCStack resource error");
363 #if defined(__WITH_X509__) || defined(__WITH_TLS__)
365 * Structure to carry certificate data to callback.
367 typedef struct CertificateData CertData_t;
368 struct CertificateData
370 void *ctx; /**< Pointer to user context.**/
371 const OCProvisionDev_t *deviceInfo; /**< Pointer to OCProvisionDev_t.**/
372 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
373 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
374 OCProvisionResult_t *resArr; /**< Result array.**/
375 int numOfResults; /**< Number of results in result array.**/
379 * Structure to carry CRL provision API data to callback.
381 typedef struct CRLData CRLData_t;
384 void *ctx; /**< Pointer to user context.**/
385 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
386 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
387 OCProvisionResult_t *resArr; /**< Result array.**/
388 int numOfResults; /**< Number of results in result array.**/
392 * Internal function to update result in result array.
394 static void registerResultForCertProvisioning(CertData_t *certData,
395 OCStackResult stackresult)
398 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",certData->numOfResults);
399 memcpy(certData->resArr[(certData->numOfResults)].deviceId.id,
400 certData->deviceInfo->doxm->deviceID.id,UUID_LENGTH);
401 certData->resArr[(certData->numOfResults)].res = stackresult;
402 ++(certData->numOfResults);
406 * Internal Function to store results in result array during ACL provisioning.
408 static void registerResultForCRLProvisioning(CRLData_t *crlData,
409 OCStackResult stackresult)
411 OIC_LOG_V(INFO, TAG, "Inside registerResultForCRLProvisioning crlData->numOfResults is %d\n",
412 crlData->numOfResults);
413 memcpy(crlData->resArr[(crlData->numOfResults)].deviceId.id,
414 crlData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
415 crlData->resArr[(crlData->numOfResults)].res = stackresult;
416 ++(crlData->numOfResults);
421 * Callback handler of SRPProvisionCRL.
423 * @param[in] ctx ctx value passed to callback from calling function.
424 * @param[in] UNUSED handle to an invocation
425 * @param[in] clientResponse Response from queries to remote servers.
426 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
427 * and OC_STACK_KEEP_TRANSACTION to keep it.
429 static OCStackApplicationResult SRPProvisionCRLCB(void *ctx, OCDoHandle UNUSED,
430 OCClientResponse *clientResponse)
432 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionCRLCB.");
434 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
435 CRLData_t *crlData = (CRLData_t*)ctx;
436 OCProvisionResultCB resultCallback = crlData->resultCallback;
440 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
442 registerResultForCRLProvisioning(crlData, OC_STACK_RESOURCE_CHANGED);
443 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
446 OICFree(crlData->resArr);
448 return OC_STACK_DELETE_TRANSACTION;
451 registerResultForCRLProvisioning(crlData, OC_STACK_ERROR);
452 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
455 OIC_LOG_V(ERROR, TAG, "SRPProvisionCRLCB received Null clientResponse");
456 OICFree(crlData->resArr);
458 return OC_STACK_DELETE_TRANSACTION;
461 OCStackResult SRPProvisionCRL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
462 OicSecCrl_t *crl, OCProvisionResultCB resultCallback)
464 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
465 VERIFY_NON_NULL(TAG, crl, ERROR, OC_STACK_INVALID_PARAM);
466 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
468 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
471 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
472 return OC_STACK_NO_MEMORY;
475 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
476 OCStackResult res = CrlToCBORPayload(crl, &secPayload->securityData, &secPayload->payloadSize);
477 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
480 OIC_LOG(ERROR, TAG, "Failed to BinToCrlJSON");
481 return OC_STACK_NO_MEMORY;
483 OIC_LOG(DEBUG, TAG, "Created payload for CRL:");
484 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
486 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
487 if(!PMGenerateQuery(true,
488 selectedDeviceInfo->endpoint.addr,
489 selectedDeviceInfo->securePort,
490 selectedDeviceInfo->connType,
491 query, sizeof(query), OIC_RSRC_CRL_URI))
493 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
494 OCPayloadDestroy((OCPayload *)secPayload);
495 return OC_STACK_ERROR;
497 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
499 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
500 cbData.cb = &SRPProvisionCRLCB;
501 CRLData_t *crlData = (CRLData_t *) OICCalloc(1, sizeof(CRLData_t));
504 OCPayloadDestroy((OCPayload *)secPayload);
505 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
506 return OC_STACK_NO_MEMORY;
509 crlData->deviceInfo = selectedDeviceInfo;
510 crlData->resultCallback = resultCallback;
511 crlData->numOfResults=0;
514 crlData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
515 if (crlData->resArr == NULL)
517 OCPayloadDestroy((OCPayload *)secPayload);
518 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
519 return OC_STACK_NO_MEMORY;
522 cbData.context = (void *)crlData;
524 OCMethod method = OC_REST_POST;
525 OCDoHandle handle = NULL;
526 OIC_LOG(DEBUG, TAG, "Sending CRL info to resource server");
528 OCStackResult ret = OCDoResource(&handle, method, query,
529 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
530 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
532 if (ret != OC_STACK_OK)
534 OICFree(crlData->resArr);
542 * Internal function for handling credential generation and sending cretificate credential.
544 * @param[in] cred Instance of cred resource.
545 * @param[in] deviceInfo information about device to which credential is to be provisioned.
546 * @param[in] responseHandler callbak called by OC stack when request API receives response.
547 * @return OC_STACK_OK in case of success and other value otherwise.
549 static OCStackResult provisionCertCred(const OicSecCred_t *cred,
550 const OCProvisionDev_t *deviceInfo, CertData_t *certData,
551 OCClientResponseHandler responseHandler)
553 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
556 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
557 return OC_STACK_NO_MEMORY;
559 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
561 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
562 &secPayload->payloadSize, secureFlag);
564 if ((OC_STACK_OK != res) || (NULL == secPayload->securityData))
567 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
568 return OC_STACK_NO_MEMORY;
571 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
572 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
573 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
574 if(!PMGenerateQuery(true,
575 deviceInfo->endpoint.addr,
576 deviceInfo->securePort,
577 deviceInfo->connType,
578 query, sizeof(query), OIC_RSRC_CRED_URI))
580 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
581 OCPayloadDestroy((OCPayload *)secPayload);
582 return OC_STACK_ERROR;
584 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
586 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
587 cbData.cb = responseHandler;
588 cbData.context = (void *) certData;
591 OCDoHandle handle = NULL;
592 OCMethod method = OC_REST_POST;
593 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
594 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
595 OIC_LOG_V(INFO, TAG, "OCDoResource::Certificate provisioning returned : %d",ret);
596 if (ret != OC_STACK_OK)
598 OIC_LOG(ERROR, TAG, "OCStack resource error");
605 * Callback handler for handling callback of certificate provisioning device.
607 * @param[in] ctx ctx value passed to callback from calling function.
608 * @param[in] UNUSED handle to an invocation
609 * @param[in] clientResponse Response from queries to remote servers.
610 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
611 * and OC_STACK_KEEP_TRANSACTION to keep it.
613 static OCStackApplicationResult provisionCertCB(void *ctx, OCDoHandle UNUSED,
614 OCClientResponse *clientResponse)
616 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
617 CertData_t *certData = (CertData_t *) ctx;
620 OCProvisionResultCB resultCallback = certData->resultCallback;
621 OIC_LOG(INFO, TAG, "provisionCertCred called");
624 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
626 registerResultForCertProvisioning(certData, OC_STACK_RESOURCE_CHANGED);
627 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
630 OICFree(certData->resArr);
632 return OC_STACK_DELETE_TRANSACTION;
636 OIC_LOG(INFO, TAG, "provisionCertCredCB received Null clientResponse");
637 registerResultForCertProvisioning(certData, OC_STACK_ERROR);
638 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
641 OICFree(certData->resArr);
643 return OC_STACK_DELETE_TRANSACTION;
646 OCStackResult SRPProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint16_t credId,
647 const OCProvisionDev_t *selectedDeviceInfo, OCProvisionResultCB resultCallback)
649 OIC_LOG(INFO, TAG, "In SRPProvisionTrustCertChain");
650 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
651 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
652 if (SIGNED_ASYMMETRIC_KEY != type || NULL == type)
654 OIC_LOG(INFO, TAG, "Invalid key type");
655 return OC_STACK_INVALID_PARAM;
658 OicSecCred_t *trustCertChainCred = GetCredResourceDataByCredId(credId);
659 if(NULL == trustCertChainCred)
661 OIC_LOG(ERROR, TAG, "Can not find matched Trust Cert. Chain.");
662 return OC_STACK_NO_RESOURCE;
665 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
668 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
669 return OC_STACK_NO_MEMORY;
671 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
673 if(OC_STACK_OK != CredToCBORPayload(trustCertChainCred, &secPayload->securityData, &secPayload->payloadSize, secureFlag))
675 OCPayloadDestroy((OCPayload *)secPayload);
676 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
677 return OC_STACK_NO_MEMORY;
679 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
680 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
682 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
683 if(!PMGenerateQuery(true,
684 selectedDeviceInfo->endpoint.addr,
685 selectedDeviceInfo->securePort,
686 selectedDeviceInfo->connType,
687 query, sizeof(query), OIC_RSRC_CRED_URI))
689 OIC_LOG(ERROR, TAG, "SRPProvisionTrustCertChain : Failed to generate query");
690 return OC_STACK_ERROR;
692 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
694 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
695 cbData.cb = &provisionCertCB;
696 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
697 if (NULL == certData)
699 OICFree(trustCertChainCred);
700 OIC_LOG(ERROR, TAG, "Memory allocation problem");
701 return OC_STACK_NO_MEMORY;
703 certData->deviceInfo = selectedDeviceInfo;
704 certData->resultCallback = resultCallback;
705 certData->credInfo = trustCertChainCred;
706 certData->numOfResults=0;
710 certData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
711 if (certData->resArr == NULL)
713 DeleteCredList(trustCertChainCred);
715 OCPayloadDestroy((OCPayload *)secPayload);
716 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
717 return OC_STACK_NO_MEMORY;
719 cbData.context = (void *)certData;
721 OCMethod method = OC_REST_POST;
722 OCDoHandle handle = NULL;
723 OIC_LOG(DEBUG, TAG, "Sending Cred info to resource server");
724 OCStackResult ret = OCDoResource(&handle, method, query,
725 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
726 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
727 if (ret != OC_STACK_OK)
729 OICFree(certData->resArr);
732 DeleteCredList(trustCertChainCred);
734 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
738 OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
739 OicEncodingType_t encodingType, uint16_t *credId)
741 OIC_LOG(DEBUG, TAG, "IN SRPSaveTrustCertChain");
742 VERIFY_NON_NULL(TAG, trustCertChain, ERROR, OC_STACK_INVALID_PARAM);
743 VERIFY_NON_NULL(TAG, credId, ERROR, OC_STACK_INVALID_PARAM);
745 OCStackResult res = OC_STACK_ERROR;
747 OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(*cred));
748 VERIFY_NON_NULL(TAG, cred, ERROR, OC_STACK_NO_MEMORY);
750 memcpy(cred->subject.id, &WILDCARD_SUBJECT_ID, WILDCARD_SUBJECT_ID_LEN);
752 cred->credUsage= (char *)OICCalloc(1, strlen(TRUST_CA)+1 );
753 VERIFY_NON_NULL(TAG, cred->credUsage, ERROR, OC_STACK_NO_MEMORY);
754 OICStrcpy(cred->credUsage, strlen(TRUST_CA) + 1, TRUST_CA) ;
756 cred->credType = SIGNED_ASYMMETRIC_KEY;
758 cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize);
759 VERIFY_NON_NULL(TAG, cred->optionalData.data, ERROR, OC_STACK_NO_MEMORY);
760 memcpy(cred->optionalData.data, trustCertChain, chainSize);
761 cred->optionalData.len = chainSize;
762 cred->optionalData.encoding = encodingType;
764 res = AddCredential(cred);
765 if(res != OC_STACK_OK)
767 DeleteCredList(cred);
770 *credId = cred->credId;
772 OIC_LOG(DEBUG, TAG, "OUT SRPSaveTrustCertChain");
776 #endif // __WITH_X509__ || __WITH_TLS__
778 OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
779 const OCProvisionDev_t *pDev1,
780 const OCProvisionDev_t *pDev2,
781 OCProvisionResultCB resultCallback)
783 VERIFY_NON_NULL(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM);
784 if (SYMMETRIC_PAIR_WISE_KEY == type)
786 VERIFY_NON_NULL(TAG, pDev2, ERROR, OC_STACK_INVALID_PARAM);
790 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
791 return OC_STACK_INVALID_CALLBACK;
793 if (SYMMETRIC_PAIR_WISE_KEY == type &&
794 0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
796 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
797 return OC_STACK_INVALID_PARAM;
800 if (SYMMETRIC_PAIR_WISE_KEY == type &&
801 !(OWNER_PSK_LENGTH_128 == keySize || OWNER_PSK_LENGTH_256 == keySize))
803 OIC_LOG(INFO, TAG, "Invalid key size");
804 return OC_STACK_INVALID_PARAM;
807 OIC_LOG(INFO, TAG, "In SRPProvisionCredentials");
809 if (SYMMETRIC_PAIR_WISE_KEY == type)
811 bool linkExisits = true;
812 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExisits);
814 if (res != OC_STACK_OK)
816 OIC_LOG(ERROR, TAG, "Internal error occured");
821 OIC_LOG(ERROR, TAG, "Link already exists");
822 return OC_STACK_INVALID_PARAM;
826 OicUuid_t provTooldeviceID = {{0,}};
827 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
829 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
830 return OC_STACK_ERROR;
832 OIC_LOG(INFO, TAG, "retrieved deviceid");
835 case SYMMETRIC_PAIR_WISE_KEY:
837 const OCProvisionDev_t *firstDevice = pDev1;
838 const OCProvisionDev_t *secondDevice = pDev2;
840 OicSecCred_t *firstCred = NULL;
841 OicSecCred_t *secondCred = NULL;
842 OCStackResult res = PMGeneratePairWiseCredentials(type, keySize, &provTooldeviceID,
843 &firstDevice->doxm->deviceID, &secondDevice->doxm->deviceID,
844 &firstCred, &secondCred);
845 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
846 OIC_LOG(INFO, TAG, "Credentials generated successfully");
847 CredentialData_t *credData =
848 (CredentialData_t *) OICCalloc(1, sizeof(CredentialData_t));
849 if (NULL == credData)
853 OIC_LOG(ERROR, TAG, "Memory allocation problem");
854 return OC_STACK_NO_MEMORY;
856 credData->deviceInfo1 = firstDevice;
857 credData->deviceInfo2 = secondDevice;
858 credData->credInfo = secondCred;
860 credData->credInfoFirst = firstCred;
861 credData->numOfResults = 0;
862 credData->resultCallback = resultCallback;
863 // first call to provision creds to device1.
864 // second call to provision creds to device2.
867 (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
868 if (NULL == credData->resArr)
873 OIC_LOG(ERROR, TAG, "Memory allocation problem");
874 return OC_STACK_NO_MEMORY;
876 res = provisionCredentials(firstCred, firstDevice, credData, &provisionCredentialCB1);
877 if (OC_STACK_OK != res)
879 DeleteCredList(firstCred);
880 DeleteCredList(secondCred);
881 OICFree(credData->resArr);
884 OIC_LOG_V(INFO, TAG, "provisionCredentials returned: %d",res);
885 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
889 case SIGNED_ASYMMETRIC_KEY:
891 const OCProvisionDev_t *firstDevice = pDev1;
892 OicSecCred_t *cred = NULL;
893 OCStackResult res = PMGenerateCertificateCredentials(&provTooldeviceID,
894 &firstDevice->doxm->deviceID,&cred);
895 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
896 OIC_LOG(INFO, TAG, "Certificate credentials generated successfully");
897 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
898 if (NULL == certData)
901 OIC_LOG(ERROR, TAG, "Memory allocation problem");
902 return OC_STACK_NO_MEMORY;
905 certData->deviceInfo = firstDevice;
907 certData->credInfo = cred;
908 certData->numOfResults = 0;
909 certData->resultCallback = resultCallback;
911 certData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
912 if (NULL == certData->resArr)
914 DeleteCredList(cred);
916 OIC_LOG(ERROR, TAG, "Memory allocation problem");
917 return OC_STACK_NO_MEMORY;
920 res = provisionCertCred(cred, firstDevice, certData, &provisionCertCB);
921 if (OC_STACK_OK != res)
923 OICFree(certData->resArr);
926 DeleteCredList(cred);
927 OIC_LOG_V(INFO, TAG, "provisionCertCredentials returned: %d",res);
934 OIC_LOG(ERROR, TAG, "Invalid option.");
935 return OC_STACK_INVALID_PARAM;
938 return OC_STACK_ERROR;
942 * Internal Function to store results in result array during ACL provisioning.
944 static void registerResultForACLProvisioning(ACLData_t *aclData,
945 OCStackResult stackresult)
947 OIC_LOG_V(INFO, TAG, "Inside registerResultForACLProvisioning aclData->numOfResults is %d\n",
948 aclData->numOfResults);
949 memcpy(aclData->resArr[(aclData->numOfResults)].deviceId.id,
950 aclData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
951 aclData->resArr[(aclData->numOfResults)].res = stackresult;
952 ++(aclData->numOfResults);
956 * Callback handler of SRPProvisionACL.
958 * @param[in] ctx ctx value passed to callback from calling function.
959 * @param[in] UNUSED handle to an invocation
960 * @param[in] clientResponse Response from queries to remote servers.
961 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
962 * and OC_STACK_KEEP_TRANSACTION to keep it.
964 static OCStackApplicationResult SRPProvisionACLCB(void *ctx, OCDoHandle UNUSED,
965 OCClientResponse *clientResponse)
967 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionACLCB.");
969 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
970 ACLData_t *aclData = (ACLData_t*)ctx;
971 OCProvisionResultCB resultCallback = aclData->resultCallback;
975 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
977 registerResultForACLProvisioning(aclData, OC_STACK_RESOURCE_CHANGED);
978 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
981 OICFree(aclData->resArr);
983 return OC_STACK_DELETE_TRANSACTION;
986 registerResultForACLProvisioning(aclData, OC_STACK_ERROR);
987 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
990 OIC_LOG_V(ERROR, TAG, "SRPProvisionACLCB received Null clientResponse");
991 OICFree(aclData->resArr);
993 return OC_STACK_DELETE_TRANSACTION;
996 OCStackResult SRPProvisionACL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
997 OicSecAcl_t *acl, OCProvisionResultCB resultCallback)
999 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
1000 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
1001 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1003 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1006 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1007 return OC_STACK_NO_MEMORY;
1009 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1010 if(OC_STACK_OK != AclToCBORPayload(acl, &secPayload->securityData, &secPayload->payloadSize))
1012 OCPayloadDestroy((OCPayload *)secPayload);
1013 OIC_LOG(ERROR, TAG, "Failed to AclToCBORPayload");
1014 return OC_STACK_NO_MEMORY;
1016 OIC_LOG(DEBUG, TAG, "Created payload for ACL:");
1017 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1019 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1020 if(!PMGenerateQuery(true,
1021 selectedDeviceInfo->endpoint.addr,
1022 selectedDeviceInfo->securePort,
1023 selectedDeviceInfo->connType,
1024 query, sizeof(query), OIC_RSRC_ACL_URI))
1026 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
1027 return OC_STACK_ERROR;
1029 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1031 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1032 cbData.cb = &SRPProvisionACLCB;
1033 ACLData_t *aclData = (ACLData_t *) OICCalloc(1, sizeof(ACLData_t));
1034 if (aclData == NULL)
1036 OCPayloadDestroy((OCPayload *)secPayload);
1037 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1038 return OC_STACK_NO_MEMORY;
1040 aclData->deviceInfo = selectedDeviceInfo;
1041 aclData->resultCallback = resultCallback;
1042 aclData->numOfResults=0;
1044 // call to provision ACL to device1.
1045 int noOfRiCalls = 1;
1046 aclData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1047 if (aclData->resArr == NULL)
1050 OCPayloadDestroy((OCPayload *)secPayload);
1051 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1052 return OC_STACK_NO_MEMORY;
1054 cbData.context = (void *)aclData;
1056 OCMethod method = OC_REST_POST;
1057 OCDoHandle handle = NULL;
1058 OIC_LOG(DEBUG, TAG, "Sending ACL info to resource server");
1059 OCStackResult ret = OCDoResource(&handle, method, query,
1060 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1061 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1062 if (ret != OC_STACK_OK)
1064 OICFree(aclData->resArr);
1067 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1072 * Internal Function to store results in result array during Direct-Pairing provisioning.
1074 static void registerResultForDirectPairingProvisioning(PconfData_t *pconfData,
1075 OCStackResult stackresult)
1077 OIC_LOG_V(INFO, TAG, "Inside registerResultForDirectPairingProvisioning "
1078 "pconfData->numOfResults is %d\n", pconfData->numOfResults);
1079 memcpy(pconfData->resArr[(pconfData->numOfResults)].deviceId.id,
1080 pconfData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
1081 pconfData->resArr[(pconfData->numOfResults)].res = stackresult;
1082 ++(pconfData->numOfResults);
1086 * Callback handler of SRPProvisionDirectPairing.
1088 * @param[in] ctx ctx value passed to callback from calling function.
1089 * @param[in] UNUSED handle to an invocation
1090 * @param[in] clientResponse Response from queries to remote servers.
1091 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1092 * and OC_STACK_KEEP_TRANSACTION to keep it.
1094 static OCStackApplicationResult SRPProvisionDirectPairingCB(void *ctx, OCDoHandle UNUSED,
1095 OCClientResponse *clientResponse)
1097 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionDirectPairingCB.");
1099 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
1100 PconfData_t *pconfData = (PconfData_t*)ctx;
1101 OCProvisionResultCB resultCallback = pconfData->resultCallback;
1105 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1107 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_OK);
1108 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
1111 OICFree(pconfData->resArr);
1113 return OC_STACK_DELETE_TRANSACTION;
1116 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_ERROR);
1117 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
1120 OIC_LOG_V(ERROR, TAG, "SRPProvisionDirectPairingCB received Null clientResponse");
1121 OICFree(pconfData->resArr);
1123 return OC_STACK_DELETE_TRANSACTION;
1126 OCStackResult SRPProvisionDirectPairing(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
1127 OicSecPconf_t *pconf, OCProvisionResultCB resultCallback)
1129 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
1130 VERIFY_NON_NULL(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
1131 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1133 // check direct-pairing capability
1134 if (true != selectedDeviceInfo->doxm->dpc)
1136 OIC_LOG(ERROR, TAG, "Resouce server does not have Direct-Pairing Capability ");
1137 return OC_STACK_UNAUTHORIZED_REQ;
1140 OicUuid_t provTooldeviceID = {.id={0}};
1141 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1143 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1144 return OC_STACK_ERROR;
1146 memcpy(&pconf->rownerID, &provTooldeviceID, sizeof(OicUuid_t));
1148 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1151 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1152 return OC_STACK_NO_MEMORY;
1154 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1156 if (OC_STACK_OK != PconfToCBORPayload(pconf, &(secPayload->securityData),
1157 &(secPayload->payloadSize)))
1159 OCPayloadDestroy((OCPayload*)secPayload);
1160 OIC_LOG(ERROR, TAG, "Failed to PconfToCborPayload");
1161 return OC_STACK_NO_MEMORY;
1163 OIC_LOG(DEBUG, TAG, "Created payload for pconf set");
1164 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1167 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1168 if(!PMGenerateQuery(true,
1169 selectedDeviceInfo->endpoint.addr,
1170 selectedDeviceInfo->securePort,
1171 selectedDeviceInfo->connType,
1172 query, sizeof(query), OIC_RSRC_PCONF_URI))
1174 OIC_LOG(ERROR, TAG, "SRPProvisionDirectPairing : Failed to generate query");
1175 return OC_STACK_ERROR;
1177 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1179 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1180 cbData.cb = &SRPProvisionDirectPairingCB;
1181 PconfData_t *pconfData = (PconfData_t *) OICCalloc(1, sizeof(PconfData_t));
1182 if (NULL == pconfData)
1184 OCPayloadDestroy((OCPayload*)secPayload);
1185 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1186 return OC_STACK_NO_MEMORY;
1188 pconfData->deviceInfo = selectedDeviceInfo;
1189 pconfData->resultCallback = resultCallback;
1190 pconfData->numOfResults=0;
1191 pconfData->ctx = ctx;
1192 // call to provision PCONF to device1.
1193 int noOfRiCalls = 1;
1194 pconfData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1195 if (NULL == pconfData->resArr)
1198 OCPayloadDestroy((OCPayload*)secPayload);
1199 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1200 return OC_STACK_NO_MEMORY;
1202 cbData.context = (void *)pconfData;
1204 OCMethod method = OC_REST_POST;
1205 OCDoHandle handle = NULL;
1206 OIC_LOG(DEBUG, TAG, "Sending PCONF info to resource server");
1207 OCStackResult ret = OCDoResource(&handle, method, query,
1208 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1209 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1210 if (OC_STACK_OK != ret)
1212 OICFree(pconfData->resArr);
1215 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1219 static void DeleteUnlinkData_t(UnlinkData_t *unlinkData)
1223 OICFree(unlinkData->unlinkDev);
1224 OICFree(unlinkData->unlinkRes);
1225 OICFree(unlinkData);
1229 static void registerResultForUnlinkDevices(UnlinkData_t *unlinkData, OCStackResult stackresult,
1232 if (NULL != unlinkData)
1234 OIC_LOG_V(INFO, TAG, "Inside registerResultForUnlinkDevices unlinkData->numOfResults is %d\n",
1235 unlinkData->numOfResults);
1236 OIC_LOG_V(INFO, TAG, "Stack result :: %d", stackresult);
1238 OicUuid_t *pUuid = &unlinkData->unlinkRes[(unlinkData->numOfResults)].deviceId;
1240 // Set result in the result array according to the position (devNum).
1241 if (idx != IDX_DB_UPDATE_RES)
1243 memcpy(pUuid->id, unlinkData->unlinkDev[idx].doxm->deviceID.id, sizeof(pUuid->id));
1246 { // When deivce ID is 000... this means it's the result of database update.
1247 memset(pUuid->id, 0, sizeof(pUuid->id));
1249 unlinkData->unlinkRes[(unlinkData->numOfResults)].res = stackresult;
1250 ++(unlinkData->numOfResults);
1251 OIC_LOG (INFO, TAG, "Out registerResultForUnlinkDevices");
1255 static OCStackResult SendDeleteCredentialRequest(void* ctx,
1256 OCClientResponseHandler respHandler,
1257 const OCProvisionDev_t* revokedDev,
1258 const OCProvisionDev_t* destDev)
1260 OIC_LOG(DEBUG, TAG, "IN SendDeleteCredentialRequest");
1262 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1264 return OC_STACK_INVALID_PARAM;
1268 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1269 if(OC_STACK_OK != ret)
1271 OIC_LOG(ERROR, TAG, "SendDeleteCredentialRequest : Failed to canonical UUID encoding");
1272 return OC_STACK_ERROR;
1275 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1277 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1278 char *srpUri = SRP_FORM_DELETE_CREDENTIAL;
1280 if(CA_ADAPTER_TCP == destDev->endpoint.adapter)
1282 srpUri = SRP_FORM_DELETE_CREDENTIAL_TCP;
1285 snRet = snprintf(reqBuf, sizeof(reqBuf), srpUri, destDev->endpoint.addr,
1286 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1290 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1291 return OC_STACK_ERROR;
1293 else if ((size_t)snRet >= sizeof(reqBuf))
1295 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1296 return OC_STACK_ERROR;
1299 OCCallbackData cbData;
1300 memset(&cbData, 0, sizeof(cbData));
1301 cbData.context = ctx;
1302 cbData.cb = respHandler;
1304 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1306 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1308 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1309 &destDev->endpoint, NULL,
1310 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1311 if (OC_STACK_OK != ret)
1313 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1315 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1320 static OCStackResult SendDeleteACLRequest(void* ctx,
1321 OCClientResponseHandler respHandler,
1322 const OCProvisionDev_t* revokedDev,
1323 const OCProvisionDev_t* destDev)
1325 OIC_LOG(DEBUG, TAG, "IN SendDeleteACLRequest");
1327 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1329 return OC_STACK_INVALID_PARAM;
1333 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1334 if(OC_STACK_OK != ret)
1336 OIC_LOG(ERROR, TAG, "SendDeleteACLRequest : Failed to canonical UUID encoding");
1337 return OC_STACK_ERROR;
1340 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1342 //coaps://0.0.0.0:5684/oic/sec/acl?subjectuuid=(Canonical ENCODED UUID)
1343 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, destDev->endpoint.addr,
1344 destDev->securePort, OIC_RSRC_ACL_URI, OIC_JSON_SUBJECTID_NAME, subID);
1348 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error (snprintf) %d\n", snRet);
1349 return OC_STACK_ERROR;
1351 else if ((size_t)snRet >= sizeof(reqBuf))
1353 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Truncated (snprintf) %d\n", snRet);
1354 return OC_STACK_ERROR;
1357 OCCallbackData cbData;
1358 memset(&cbData, 0, sizeof(cbData));
1359 cbData.context = ctx;
1360 cbData.cb = respHandler;
1362 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1364 OIC_LOG(DEBUG, TAG, "Sending remove ACL request to resource server");
1366 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1367 &destDev->endpoint, NULL,
1368 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1369 if (OC_STACK_OK != ret)
1371 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error in OCDoResource %d", ret);
1373 OIC_LOG(DEBUG, TAG, "OUT SendDeleteACLRequest");
1379 * Callback handler of unlink second device.
1381 * @param[in] ctx ctx value passed to callback from calling function.
1382 * @param[in] handle handle to an invocation
1383 * @param[in] clientResponse Response from queries to remote servers.
1384 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1385 * OC_STACK_KEEP_TRANSACTION to keep it.
1387 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1388 OCClientResponse *clientResponse)
1391 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1392 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1393 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1397 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1398 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1400 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1402 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1406 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1407 unlinkData->resultCallback(unlinkData->ctx,
1408 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1414 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1415 IDX_SECOND_DEVICE_RES);
1416 unlinkData->resultCallback(unlinkData->ctx,
1417 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1418 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1422 //Update provisioning DB when succes case.
1423 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1424 &unlinkData->unlinkDev[1].doxm->deviceID))
1426 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1427 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1428 unlinkData->resultCallback(unlinkData->ctx,
1429 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1432 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1436 DeleteUnlinkData_t(unlinkData);
1437 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1438 return OC_STACK_DELETE_TRANSACTION;
1443 * Callback handler of unlink first device.
1445 * @param[in] ctx ctx value passed to callback from calling function.
1446 * @param[in] handle handle to an invocation
1447 * @param[in] clientResponse Response from queries to remote servers.
1448 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1449 * OC_STACK_KEEP_TRANSACTION to keep it.
1451 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1452 OCClientResponse *clientResponse)
1454 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1455 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1456 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1461 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1462 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1464 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1466 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1468 // Second revocation request to second device.
1469 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1470 &unlinkData->unlinkDev[0],
1471 &unlinkData->unlinkDev[1] /*Dest*/);
1472 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1473 if (OC_STACK_OK != res)
1475 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1476 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1477 IDX_SECOND_DEVICE_RES);
1478 unlinkData->resultCallback(unlinkData->ctx,
1479 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1484 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1485 return OC_STACK_DELETE_TRANSACTION;
1490 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1492 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1493 unlinkData->unlinkRes, true);
1499 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1500 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1501 IDX_FIRST_DEVICE_RES );
1502 unlinkData->resultCallback(unlinkData->ctx,
1503 unlinkData->numOfResults, unlinkData->unlinkRes,
1505 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1509 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1510 DeleteUnlinkData_t(unlinkData);
1511 return OC_STACK_DELETE_TRANSACTION;
1515 * Function to unlink devices.
1516 * This function will remove the credential & relationship between the two devices.
1518 * @param[in] ctx Application context would be returned in result callback
1519 * @param[in] pTargetDev1 first device information to be unlinked.
1520 * @param[in] pTargetDev2 second device information to be unlinked.
1521 * @param[in] resultCallback callback provided by API user, callback will be called when
1522 * device unlink is finished.
1523 * @return OC_STACK_OK in case of success and other value otherwise.
1525 OCStackResult SRPUnlinkDevices(void* ctx,
1526 const OCProvisionDev_t* pTargetDev1,
1527 const OCProvisionDev_t* pTargetDev2,
1528 OCProvisionResultCB resultCallback)
1530 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1532 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1534 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1535 return OC_STACK_INVALID_PARAM;
1537 if (!resultCallback)
1539 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1540 return OC_STACK_INVALID_CALLBACK;
1542 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1544 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1545 return OC_STACK_INVALID_PARAM;
1548 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1549 PMPrintOCProvisionDev(pTargetDev1);
1550 PMPrintOCProvisionDev(pTargetDev2);
1552 // Mark the link status stale
1553 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1554 if (OC_STACK_OK != res)
1556 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1560 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1561 VERIFY_NON_NULL(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1563 //Initialize unlink data
1564 unlinkData->ctx = ctx;
1565 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1566 if (NULL == unlinkData->unlinkDev)
1568 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1569 res = OC_STACK_NO_MEMORY;
1573 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1574 if (NULL == unlinkData->unlinkRes)
1576 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1577 res = OC_STACK_NO_MEMORY;
1581 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1582 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1584 unlinkData->numOfResults = 0;
1585 unlinkData->resultCallback = resultCallback;
1587 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1588 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1589 if (OC_STACK_OK != res)
1591 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1598 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1599 DeleteUnlinkData_t(unlinkData);
1603 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1607 OICFree(pRemoveData->revokeTargetDev);
1608 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1609 OICFree(pRemoveData->removeRes);
1610 OICFree(pRemoveData);
1614 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1615 OCStackResult stackresult, bool hasError)
1617 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1618 removeData->numOfResults + 1);
1621 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1622 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1626 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1627 0, sizeof(pLinkedDevId->id) );
1629 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1630 removeData->hasError = hasError;
1631 ++(removeData->numOfResults);
1633 // If we get suffcient result from linked devices, we have to call user callback and do free
1634 if (removeData->sizeOfResArray == removeData->numOfResults)
1636 if(!removeData->hasError)
1638 // Remove device info from prvisioning database
1639 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1641 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1642 removeData->hasError = true;
1645 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1646 removeData->hasError);
1647 DeleteRemoveData_t(removeData);
1651 static void registerResultForResetDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1652 OCStackResult stackresult, bool hasError)
1654 OIC_LOG_V(INFO, TAG, "Inside registerResultForResetDevice removeData->numOfResults is %zu\n",
1655 removeData->numOfResults + 1);
1658 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1659 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1663 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1664 0, sizeof(pLinkedDevId->id) );
1666 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1667 removeData->hasError = hasError;
1668 ++(removeData->numOfResults);
1670 // If we get suffcient result from linked devices, we have to call user callback and do free
1671 if (removeData->sizeOfResArray == removeData->numOfResults)
1673 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1674 removeData->hasError);
1675 DeleteRemoveData_t(removeData);
1680 * Callback handler of unlink first device.
1682 * @param[in] ctx ctx value passed to callback from calling function.
1683 * @param[in] handle handle to an invocation
1684 * @param[in] clientResponse Response from queries to remote servers.
1685 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1686 * and OC_STACK_KEEP_TRANSACTION to keep it.
1688 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1689 OCClientResponse *clientResponse)
1691 //Update the delete credential into delete device context
1692 //Save the deleted status in delDevCtx
1694 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1695 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1696 OCStackResult res = OC_STACK_ERROR;
1698 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1702 OicUuid_t revDevUuid = {.id={0}};
1703 if(UUID_LENGTH == clientResponse->identity.id_length)
1705 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1706 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1708 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1709 if (OC_STACK_OK != res)
1711 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1712 registerResultForRemoveDevice(removeData, &revDevUuid,
1713 OC_STACK_INCONSISTENT_DB, true);
1715 return OC_STACK_DELETE_TRANSACTION;
1718 registerResultForRemoveDevice(removeData, &revDevUuid,
1719 OC_STACK_RESOURCE_DELETED, false);
1723 registerResultForRemoveDevice(removeData, &revDevUuid,
1724 clientResponse->result, true);
1725 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1730 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1731 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1733 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1736 * Since server's credential was deleted,
1737 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1739 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1740 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1744 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1750 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1751 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1755 return OC_STACK_DELETE_TRANSACTION;
1759 * Callback handler of reset device.
1761 * @param[in] ctx ctx value passed to callback from calling function.
1762 * @param[in] handle handle to an invocation
1763 * @param[in] clientResponse Response from queries to remote servers.
1764 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1765 * and OC_STACK_KEEP_TRANSACTION to keep it.
1767 static OCStackApplicationResult SRPSyncDeviceCredCB(void *delDevCtx, OCDoHandle handle,
1768 OCClientResponse *clientResponse)
1770 //Update the delete credential into delete device context
1771 //Save the deleted status in delDevCtx
1773 OIC_LOG_V(INFO, TAG, "Inside SRPSyncDeviceCredCB.");
1774 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1775 OCStackResult res = OC_STACK_ERROR;
1777 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1778 OCProvisionDev_t * pTargetDev = PMCloneOCProvisionDev(removeData->revokeTargetDev);
1779 OCProvisionResultCB resultCallback = removeData->resultCallback;
1782 OicUuid_t revDevUuid = {.id={0}};
1783 if(UUID_LENGTH == clientResponse->identity.id_length)
1785 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1786 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1788 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1789 if (OC_STACK_OK != res)
1791 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1792 registerResultForResetDevice(removeData, &revDevUuid,
1793 OC_STACK_INCONSISTENT_DB, true);
1795 return OC_STACK_DELETE_TRANSACTION;
1798 registerResultForResetDevice(removeData, &revDevUuid,
1799 OC_STACK_RESOURCE_DELETED, false);
1803 registerResultForResetDevice(removeData, &revDevUuid,
1804 clientResponse->result, false);
1805 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1810 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1811 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1813 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1816 * Since server's credential was deleted,
1817 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1819 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1820 registerResultForResetDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1824 registerResultForResetDevice(removeData, NULL, clientResponse->result, true);
1830 registerResultForResetDevice(removeData, NULL, OC_STACK_ERROR, true);
1831 OIC_LOG(ERROR, TAG, "SRPSyncDevice received Null clientResponse");
1834 SRPResetDevice(pTargetDev, resultCallback);
1836 return OC_STACK_DELETE_TRANSACTION;
1840 * Callback handler of reset device sync-up
1842 * @param[in] ctx ctx value passed to callback from calling function.
1843 * @param[in] handle handle to an invocation
1844 * @param[in] clientResponse Response from queries to remote servers.
1845 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1846 * and OC_STACK_KEEP_TRANSACTION to keep it.
1848 static OCStackApplicationResult SRPSyncDeviceACLCB(void *ctx, OCDoHandle handle,
1849 OCClientResponse *clientResponse)
1851 return OC_STACK_DELETE_TRANSACTION;
1855 * Callback handler of device remote reset.
1857 * @param[in] ctx ctx value passed to callback from calling function.
1858 * @param[in] handle handle to an invocation
1859 * @param[in] clientResponse Response from queries to remote servers.
1860 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1861 * and OC_STACK_KEEP_TRANSACTION to keep it.
1863 static OCStackApplicationResult SRPResetDeviceCB(void *ctx, OCDoHandle handle,
1864 OCClientResponse *clientResponse)
1866 OIC_LOG(DEBUG, TAG, "IN SRPResetDeviceCB");
1867 if(OC_STACK_OK == clientResponse->result)
1869 OIC_LOG(DEBUG, TAG, "Change Target Device Pstat Cm SUCCEEDED");
1872 // Delete Cred and ACL related to the target device.
1873 const OicSecCred_t *cred = NULL;
1874 OCProvisionDev_t * pTargetDev = (OCProvisionDev_t *)ctx;
1875 cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
1878 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to get credential of target device.");
1882 OCStackResult res = RemoveCredential(&cred->subject);
1883 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1885 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove credential.");
1889 res = RemoveACE(&cred->subject, NULL);
1890 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1892 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove ACL.");
1895 if (OC_STACK_OK != PDMDeleteDevice(&pTargetDev->doxm->deviceID))
1897 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to delete device from PDM");
1900 //Close the DTLS session of the reset device.
1901 CAEndpoint_t* endpoint = (CAEndpoint_t *)&clientResponse->devAddr;
1902 CAResult_t caResult = CACloseDtlsSession(endpoint);
1903 if(CA_STATUS_OK != caResult)
1905 OIC_LOG_V(WARNING, TAG, "OCResetDevice : Failed to close DTLS session : %d", caResult);
1909 * If there is no linked device, PM does not send any request.
1910 * So we should directly invoke the result callback to inform the result of OCResetDevice.
1912 if(OC_STACK_NO_RESOURCE == res)
1918 OICFree(pTargetDev);
1919 return OC_STACK_DELETE_TRANSACTION;
1923 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1924 OCProvisionDev_t* pOwnedDevList,
1925 OCUuidList_t* pLinkedUuidList,
1926 OCProvisionDev_t** ppLinkedDevList,
1927 size_t *numOfLinkedDev)
1929 // pOwnedDevList could be NULL. It means no alived and owned device now.
1930 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1931 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1933 return OC_STACK_INVALID_PARAM;
1937 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1938 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1940 // Mark the link status stale.
1941 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1942 if (OC_STACK_OK != res)
1944 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1945 return OC_STACK_INCONSISTENT_DB;
1950 // If this linked device is alive (power-on), add the deivce to the list.
1951 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1952 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1954 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1956 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1957 if (NULL == targetDev)
1959 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1960 return OC_STACK_NO_MEMORY;
1963 LL_PREPEND(*ppLinkedDevList, targetDev);
1970 *numOfLinkedDev = cnt;
1975 * Function to device revocation
1976 * This function will remove credential of target device from all devices in subnet.
1978 * @param[in] ctx Application context would be returned in result callback
1979 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1980 * @param[in] pTargetDev Device information to be revoked.
1981 * @param[in] resultCallback callback provided by API user, callback will be called when
1982 * credential revocation is finished.
1983 * @return OC_STACK_OK in case of success and other value otherwise.
1984 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1985 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1987 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1988 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1990 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1992 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1994 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1995 return OC_STACK_INVALID_PARAM;
1997 if (!resultCallback)
1999 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
2000 return OC_STACK_INVALID_CALLBACK;
2003 // Declare variables in here to handle error cases with goto statement.
2004 OCProvisionDev_t* pOwnedDevList = NULL;
2005 OCProvisionDev_t* pLinkedDevList = NULL;
2006 RemoveData_t* removeData = NULL;
2008 //1. Find all devices that has a credential of the revoked device
2009 OCUuidList_t* pLinkedUuidList = NULL;
2010 size_t numOfDevices = 0;
2011 OCStackResult res = OC_STACK_ERROR;
2012 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2013 if (OC_STACK_OK != res)
2015 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
2018 // if there is no related device, we can skip further process.
2019 if (0 == numOfDevices)
2021 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
2022 res = OC_STACK_CONTINUE;
2026 //2. Find owned device from the network
2027 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
2028 if (OC_STACK_OK != res)
2030 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
2034 //3. Make a list of devices to send DELETE credential request
2035 // by comparing owned devices from provisioning database with mutlicast discovery result.
2036 size_t numOfLinkedDev = 0;
2037 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2038 &pLinkedDevList, &numOfLinkedDev);
2039 if (OC_STACK_OK != res)
2041 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
2044 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2045 { // So we don't have to send request message.
2046 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
2047 res = OC_STACK_CONTINUE;
2051 // 4. Prepare RemoveData Context data.
2052 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2055 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
2056 res = OC_STACK_NO_MEMORY;
2060 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2061 if (!removeData->revokeTargetDev)
2063 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
2064 res = OC_STACK_NO_MEMORY;
2068 removeData->removeRes =
2069 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2070 if (!removeData->removeRes)
2072 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
2073 res = OC_STACK_NO_MEMORY;
2077 removeData->ctx = ctx;
2078 removeData->linkedDevList = pLinkedDevList;
2079 removeData->resultCallback = resultCallback;
2080 removeData->numOfResults = 0;
2081 removeData->sizeOfResArray = numOfLinkedDev;
2082 removeData->hasError = false;
2084 // 5. Send DELETE credential request to linked devices.
2085 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2086 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2087 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2089 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2090 removeData->revokeTargetDev, curDev);
2091 if (OC_STACK_OK != res)
2093 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
2094 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2098 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2102 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2103 PMDeleteDeviceList(pOwnedDevList);
2104 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
2106 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2109 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2110 PMDeleteDeviceList(pOwnedDevList);
2111 PMDeleteDeviceList(pLinkedDevList);
2114 OICFree(removeData->revokeTargetDev);
2115 OICFree(removeData->removeRes);
2116 OICFree(removeData);
2118 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");
2123 * Function to device revocation
2124 * This function will remove credential of target device from all devices in subnet.
2126 * @param[in] ctx Application context would be returned in result callback
2127 * @param[in] pOwnedDevList List of owned devices
2128 * @param[in] pTargetDev Device information to be revoked.
2129 * @param[in] resultCallback callback provided by API user, callback will be called when
2130 * credential revocation is finished.
2131 * @return OC_STACK_OK in case of success and other value otherwise.
2132 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2133 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2135 OCStackResult SRPRemoveDeviceWithoutDiscovery(void* ctx, const OCProvisionDev_t* pOwnedDevList,
2136 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2138 OIC_LOG(INFO, TAG, "IN SRPRemoveDeviceWithoutDiscovery");
2142 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : Owned Device List is empty");
2143 return OC_STACK_CONTINUE;
2147 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL parameters");
2148 return OC_STACK_INVALID_PARAM;
2150 if (!resultCallback)
2152 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL Callback");
2153 return OC_STACK_INVALID_CALLBACK;
2156 // Declare variables in here to handle error cases with goto statement.
2157 OCProvisionDev_t* pLinkedDevList = NULL;
2158 RemoveData_t* removeData = NULL;
2160 //1. Find all devices that has a credential of the revoked device
2161 OCUuidList_t* pLinkedUuidList = NULL;
2162 size_t numOfDevices = 0;
2163 OCStackResult res = OC_STACK_ERROR;
2164 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2165 if (OC_STACK_OK != res)
2167 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to get linked devices information");
2168 return OC_STACK_CONTINUE;
2170 // if there is no related device, we can skip further process.
2171 if (0 == numOfDevices)
2173 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : No linked device found.");
2174 return OC_STACK_CONTINUE;
2177 //2. Make a list of devices to send DELETE credential request
2178 // by comparing owned devices from provisioning database with mutlicast discovery result.
2179 size_t numOfLinkedDev = 0;
2180 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2181 &pLinkedDevList, &numOfLinkedDev);
2182 if (OC_STACK_OK != res)
2184 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : GetListofDevToReqDeleteCred() failed");
2187 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2188 { // So we don't have to send request message.
2189 OIC_LOG(DEBUG, TAG, "SRPRemoveDeviceWithoutDiscovery : No alived & linked device found.");
2190 return OC_STACK_CONTINUE;
2193 // 3. Prepare RemoveData Context data.
2194 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2197 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2198 res = OC_STACK_NO_MEMORY;
2202 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2203 if (!removeData->revokeTargetDev)
2205 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : PMCloneOCProvisionDev Failed");
2206 res = OC_STACK_NO_MEMORY;
2210 removeData->removeRes =
2211 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2212 if (!removeData->removeRes)
2214 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2215 res = OC_STACK_NO_MEMORY;
2219 removeData->ctx = ctx;
2220 removeData->linkedDevList = pLinkedDevList;
2221 removeData->resultCallback = resultCallback;
2222 removeData->numOfResults = 0;
2223 removeData->sizeOfResArray = numOfLinkedDev;
2224 removeData->hasError = false;
2226 // 5. Send DELETE credential request to linked devices.
2227 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2228 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2229 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2231 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2232 removeData->revokeTargetDev, curDev);
2233 if (OC_STACK_OK != res)
2235 OIC_LOG_V(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Fail to send the DELETE credential request to\
2236 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2240 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2244 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2245 OIC_LOG(INFO, TAG, "OUT SRPRemoveDeviceWithoutDiscovery");
2247 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2250 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2251 PMDeleteDeviceList(pLinkedDevList);
2254 OICFree(removeData->revokeTargetDev);
2255 OICFree(removeData->removeRes);
2256 OICFree(removeData);
2258 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDeviceWithoutDiscovery");
2263 * Function to sync-up credential and ACL of the target device.
2264 * This function will remove credential and ACL of target device from all devices in subnet.
2266 * @param[in] ctx Application context would be returned in result callback
2267 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
2268 * @param[in] pTargetDev Device information to be revoked.
2269 * @param[in] resultCallback callback provided by API user, callback will be called when
2270 * credential revocation is finished.
2271 * when there is an error, this user callback is called immediately.
2272 * @return OC_STACK_OK in case of success and other value otherwise.
2273 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2274 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2276 OCStackResult SRPSyncDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
2277 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2279 OIC_LOG(INFO, TAG, "IN SRPSyncDevice");
2280 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
2282 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL parameters");
2283 return OC_STACK_INVALID_PARAM;
2285 if (!resultCallback)
2287 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL Callback");
2288 return OC_STACK_INVALID_CALLBACK;
2291 // Declare variables in here to handle error cases with goto statement.
2292 OCProvisionDev_t* pOwnedDevList = NULL;
2293 OCProvisionDev_t* pLinkedDevList = NULL;
2294 RemoveData_t* removeData = NULL;
2296 //1. Find all devices that has a credential of the revoked device
2297 OCUuidList_t* pLinkedUuidList = NULL;
2298 size_t numOfDevices = 0;
2299 OCStackResult res = OC_STACK_ERROR;
2300 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2301 if (OC_STACK_OK != res)
2303 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to get linked devices information");
2306 // if there is no related device, we can skip further process.
2307 if (0 == numOfDevices)
2309 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No linked device found.");
2310 res = OC_STACK_CONTINUE;
2314 //2. Find owned device from the network
2315 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
2316 if (OC_STACK_OK != res)
2318 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to PMDeviceDiscovery");
2322 //3. Make a list of devices to send DELETE credential request
2323 // by comparing owned devices from provisioning database with mutlicast discovery result.
2324 size_t numOfLinkedDev = 0;
2325 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2326 &pLinkedDevList, &numOfLinkedDev);
2327 if (OC_STACK_OK != res)
2329 OIC_LOG(ERROR, TAG, "SRPSyncDevice : GetListofDevToReqDeleteCred() failed");
2332 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2333 { // So we don't have to send request message.
2334 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No alived & linked device found.");
2335 res = OC_STACK_CONTINUE;
2339 // 4. Prepare RemoveData Context data.
2340 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2343 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to memory allocation");
2344 res = OC_STACK_NO_MEMORY;
2348 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2349 if (!removeData->revokeTargetDev)
2351 OIC_LOG(ERROR, TAG, "SRPSyncDevice : PMCloneOCProvisionDev Failed");
2352 res = OC_STACK_NO_MEMORY;
2356 removeData->removeRes =
2357 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2358 if (!removeData->removeRes)
2360 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to allocate memory");
2361 res = OC_STACK_NO_MEMORY;
2365 removeData->ctx = ctx;
2366 removeData->linkedDevList = pLinkedDevList;
2367 removeData->resultCallback = resultCallback;
2368 removeData->numOfResults = 0;
2369 removeData->sizeOfResArray = numOfLinkedDev;
2370 removeData->hasError = false;
2372 // 5. Send DELETE credential request to linked devices.
2373 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2374 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2375 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2377 res = SendDeleteACLRequest((void*)removeData, &SRPSyncDeviceACLCB,
2378 removeData->revokeTargetDev, curDev);
2379 if (OC_STACK_OK != res)
2381 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE ACL request to\
2382 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2385 res = SendDeleteCredentialRequest((void*)removeData, &SRPSyncDeviceCredCB,
2386 removeData->revokeTargetDev, curDev);
2387 if (OC_STACK_OK != res)
2389 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE credential request to\
2390 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2391 totalRes = OC_STACK_ERROR;
2395 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2399 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2400 PMDeleteDeviceList(pOwnedDevList);
2401 OIC_LOG(INFO, TAG, "OUT SRPSyncDevice");
2403 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2406 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2407 PMDeleteDeviceList(pOwnedDevList);
2408 PMDeleteDeviceList(pLinkedDevList);
2411 OICFree(removeData->revokeTargetDev);
2412 OICFree(removeData->removeRes);
2413 OICFree(removeData);
2415 OIC_LOG(INFO, TAG, "OUT ERROR case SRPSyncDevice");
2420 * Function for remote reset
2421 * This function will send pstat PUT message to the target device to initiate remote reset.
2423 * @param[in] pTargetDev Device information to be revoked.
2424 * @param[in] resultCallback callback provided by API user, callback will be called when
2425 * credential revocation is finished.
2426 * when there is an error, this user callback is called immediately.
2427 * @return OC_STACK_OK in case of success and other value otherwise.
2428 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2429 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2431 OCStackResult SRPResetDevice(const OCProvisionDev_t* pTargetDev,
2432 OCProvisionResultCB resultCallback)
2434 OIC_LOG(INFO, TAG, "IN SRPResetDevice");
2437 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL parameters");
2438 return OC_STACK_INVALID_PARAM;
2440 if (!resultCallback)
2442 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL Callback");
2443 return OC_STACK_INVALID_CALLBACK;
2446 OCStackResult res = OC_STACK_ERROR;
2447 OicSecPstat_t * pstat = (OicSecPstat_t *) OICCalloc(1, sizeof(OicSecPstat_t));
2450 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2451 return OC_STACK_NO_MEMORY;
2455 pstat->isOp = false;
2456 memcpy(pstat->deviceID.id, pTargetDev->doxm->deviceID.id, sizeof(OicUuid_t));
2457 pstat->tm = TAKE_OWNER;
2458 pstat->om = (OicSecDpom_t)(SINGLE_SERVICE_SERVER_DRIVEN | MULTIPLE_SERVICE_CLIENT_DRIVEN);
2460 pstat->sm = (OicSecDpom_t *) OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
2461 if (NULL == pstat->sm)
2463 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2465 return OC_STACK_NO_MEMORY;
2467 pstat->sm[0] = (OicSecDpom_t)(SINGLE_SERVICE_SERVER_DRIVEN | MULTIPLE_SERVICE_CLIENT_DRIVEN);
2469 OCSecurityPayload * secPayload = (OCSecurityPayload *) OICCalloc(1, sizeof(OCSecurityPayload));
2472 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2473 res = OC_STACK_NO_MEMORY;
2476 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2478 if (OC_STACK_OK != PstatToCBORPayload(pstat, &(secPayload->securityData),
2479 &(secPayload->payloadSize), true))
2481 OCPayloadDestroy((OCPayload *) secPayload);
2482 OIC_LOG(ERROR, TAG, "Failed to PstatToCBORPayload");
2483 res = OC_STACK_NO_MEMORY;
2486 OIC_LOG(DEBUG, TAG, "Created payload for pstat set");
2487 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2489 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2490 if (!PMGenerateQuery(true,
2491 pTargetDev->endpoint.addr,
2492 pTargetDev->securePort,
2493 pTargetDev->connType,
2494 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2496 OIC_LOG(ERROR, TAG, "SRPResetDevice : Failed to generate query");
2497 OCPayloadDestroy((OCPayload *) secPayload);
2498 res = OC_STACK_ERROR;
2501 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2503 OCProvisionDev_t * targetDev = PMCloneOCProvisionDev(pTargetDev);
2504 OCCallbackData cbData = { .context = NULL, .cb = NULL, .cd = NULL };
2505 cbData.cb = &SRPResetDeviceCB;
2506 cbData.context = (void *) targetDev;
2508 OCMethod method = OC_REST_POST;
2509 OCDoHandle handle = NULL;
2510 OIC_LOG(DEBUG, TAG, "Sending PSTAT info to resource server");
2511 res = OCDoResource(&handle, method, query,
2512 &targetDev->endpoint, (OCPayload *)secPayload,
2513 targetDev->connType, OC_LOW_QOS, &cbData, NULL, 0);\
2514 if (OC_STACK_OK != res)
2516 OIC_LOG(ERROR, TAG, "OCStack resource error");
2522 OIC_LOG(INFO, TAG, "OUT SRPResetDevice");
2527 * Internal Function to store results in result array during GetCredResourceCB.
2529 static void registerResultForGetCredResourceCB(GetSecData_t *GetSecData,
2530 OCStackResult stackresult)
2532 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetCredResourceCB "
2533 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2534 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2535 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2536 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2537 ++(GetSecData->numOfResults);
2541 * Callback handler of SRPGetCredResource.
2543 * @param[in] ctx ctx value passed to callback from calling function.
2544 * @param[in] UNUSED handle to an invocation
2545 * @param[in] clientResponse Response from queries to remote servers.
2546 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2547 * and OC_STACK_KEEP_TRANSACTION to keep it.
2549 static OCStackApplicationResult SRPGetCredResourceCB(void *ctx, OCDoHandle UNUSED,
2550 OCClientResponse *clientResponse)
2552 OIC_LOG_V(INFO, TAG, "Inside SRPGetCredResourceCB.");
2554 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2555 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2556 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2560 if(OC_STACK_OK == clientResponse->result)
2562 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2563 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2565 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2567 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2568 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2571 OICFree(GetSecData->resArr);
2572 OICFree(GetSecData);
2574 return OC_STACK_DELETE_TRANSACTION;
2577 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2578 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2581 OIC_LOG_V(ERROR, TAG, "SRPGetCredResourceCB received Null clientResponse");
2582 OICFree(GetSecData->resArr);
2583 OICFree(GetSecData);
2585 return OC_STACK_DELETE_TRANSACTION;
2589 OCStackResult SRPGetCredResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2590 OCProvisionResultCB resultCallback)
2592 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2593 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2595 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2596 if(!PMGenerateQuery(true,
2597 selectedDeviceInfo->endpoint.addr,
2598 selectedDeviceInfo->securePort,
2599 selectedDeviceInfo->connType,
2600 query, sizeof(query), OIC_RSRC_CRED_URI))
2602 OIC_LOG(ERROR, TAG, "SRPGetCredResource : Failed to generate query");
2603 return OC_STACK_ERROR;
2605 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2607 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2608 cbData.cb = &SRPGetCredResourceCB;
2609 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2610 if (NULL == GetSecData)
2612 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2613 return OC_STACK_NO_MEMORY;
2615 GetSecData->deviceInfo = selectedDeviceInfo;
2616 GetSecData->resultCallback = resultCallback;
2617 GetSecData->numOfResults=0;
2618 GetSecData->ctx = ctx;
2620 int noOfRiCalls = 1;
2621 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2622 if (NULL == GetSecData->resArr)
2624 OICFree(GetSecData);
2625 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2626 return OC_STACK_NO_MEMORY;
2628 cbData.context = (void *)GetSecData;
2630 OCMethod method = OC_REST_GET;
2631 OCDoHandle handle = NULL;
2632 OIC_LOG(DEBUG, TAG, "Sending Get Cred to resource server");
2633 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2634 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2635 if (OC_STACK_OK != ret)
2637 OIC_LOG(ERROR, TAG, "OCStack resource error");
2638 OICFree(GetSecData->resArr);
2639 OICFree(GetSecData);
2641 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2642 OIC_LOG(DEBUG, TAG, "OUT SRPGetCredResource");
2648 * Internal Function to store results in result array during GetACLResourceCB.
2650 static void registerResultForGetACLResourceCB(GetSecData_t *GetSecData,
2651 OCStackResult stackresult)
2653 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetACLResourceCB "
2654 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2655 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2656 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2657 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2658 ++(GetSecData->numOfResults);
2662 * Callback handler of SRPGetACLResource.
2664 * @param[in] ctx ctx value passed to callback from calling function.
2665 * @param[in] UNUSED handle to an invocation
2666 * @param[in] clientResponse Response from queries to remote servers.
2667 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2668 * and OC_STACK_KEEP_TRANSACTION to keep it.
2670 static OCStackApplicationResult SRPGetACLResourceCB(void *ctx, OCDoHandle UNUSED,
2671 OCClientResponse *clientResponse)
2673 OIC_LOG_V(INFO, TAG, "Inside SRPGetACLResourceCB.");
2675 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2676 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2677 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2681 if(OC_STACK_OK == clientResponse->result)
2683 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2684 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2686 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2688 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2689 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2692 OICFree(GetSecData->resArr);
2693 OICFree(GetSecData);
2695 return OC_STACK_DELETE_TRANSACTION;
2698 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2699 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2702 OIC_LOG_V(ERROR, TAG, "SRPGetACLResourceCB received Null clientResponse");
2703 OICFree(GetSecData->resArr);
2704 OICFree(GetSecData);
2706 return OC_STACK_DELETE_TRANSACTION;
2710 OCStackResult SRPGetACLResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2711 OCProvisionResultCB resultCallback)
2713 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2714 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2716 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2717 if(!PMGenerateQuery(true,
2718 selectedDeviceInfo->endpoint.addr,
2719 selectedDeviceInfo->securePort,
2720 selectedDeviceInfo->connType,
2721 query, sizeof(query), OIC_RSRC_ACL_URI))
2723 OIC_LOG(ERROR, TAG, "SRPGetACLResource : Failed to generate query");
2724 return OC_STACK_ERROR;
2726 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2728 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2729 cbData.cb = &SRPGetACLResourceCB;
2730 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2731 if (NULL == GetSecData)
2733 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2734 return OC_STACK_NO_MEMORY;
2736 GetSecData->deviceInfo = selectedDeviceInfo;
2737 GetSecData->resultCallback = resultCallback;
2738 GetSecData->numOfResults=0;
2739 GetSecData->ctx = ctx;
2741 int noOfRiCalls = 1;
2742 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2743 if (NULL == GetSecData->resArr)
2745 OICFree(GetSecData);
2746 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2747 return OC_STACK_NO_MEMORY;
2749 cbData.context = (void *)GetSecData;
2751 OCMethod method = OC_REST_GET;
2752 OCDoHandle handle = NULL;
2753 OIC_LOG(DEBUG, TAG, "Sending Get ACL to resource server");
2754 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2755 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2756 if (OC_STACK_OK != ret)
2758 OIC_LOG(ERROR, TAG, "OCStack resource error");
2759 OICFree(GetSecData->resArr);
2760 OICFree(GetSecData);
2762 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2763 OIC_LOG(DEBUG, TAG, "OUT SRPGetACLResource");