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");
362 #if defined(__WITH_X509__) || defined(__WITH_TLS__)
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;
645 OCStackResult SRPProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint16_t credId,
646 const OCProvisionDev_t *selectedDeviceInfo, OCProvisionResultCB resultCallback)
648 OIC_LOG(INFO, TAG, "In SRPProvisionTrustCertChain");
649 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
650 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
651 if (SIGNED_ASYMMETRIC_KEY != type || NULL == type)
653 OIC_LOG(INFO, TAG, "Invalid key type");
654 return OC_STACK_INVALID_PARAM;
657 OicSecCred_t *trustCertChainCred = GetCredResourceDataByCredId(credId);
658 if(NULL == trustCertChainCred)
660 OIC_LOG(ERROR, TAG, "Can not find matched Trust Cert. Chain.");
661 return OC_STACK_NO_RESOURCE;
664 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
667 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
668 return OC_STACK_NO_MEMORY;
670 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
672 if(OC_STACK_OK != CredToCBORPayload(trustCertChainCred, &secPayload->securityData, &secPayload->payloadSize, secureFlag))
674 OCPayloadDestroy((OCPayload *)secPayload);
675 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
676 return OC_STACK_NO_MEMORY;
678 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
679 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
681 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
682 if(!PMGenerateQuery(true,
683 selectedDeviceInfo->endpoint.addr,
684 selectedDeviceInfo->securePort,
685 selectedDeviceInfo->connType,
686 query, sizeof(query), OIC_RSRC_CRED_URI))
688 OIC_LOG(ERROR, TAG, "SRPProvisionTrustCertChain : Failed to generate query");
689 return OC_STACK_ERROR;
691 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
693 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
694 cbData.cb = &provisionCertCB;
695 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
696 if (NULL == certData)
698 OICFree(trustCertChainCred);
699 OIC_LOG(ERROR, TAG, "Memory allocation problem");
700 return OC_STACK_NO_MEMORY;
702 certData->deviceInfo = selectedDeviceInfo;
703 certData->resultCallback = resultCallback;
704 certData->credInfo = trustCertChainCred;
705 certData->numOfResults=0;
709 certData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
710 if (certData->resArr == NULL)
712 DeleteCredList(trustCertChainCred);
714 OCPayloadDestroy((OCPayload *)secPayload);
715 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
716 return OC_STACK_NO_MEMORY;
718 cbData.context = (void *)certData;
720 OCMethod method = OC_REST_POST;
721 OCDoHandle handle = NULL;
722 OIC_LOG(DEBUG, TAG, "Sending Cred info to resource server");
723 OCStackResult ret = OCDoResource(&handle, method, query,
724 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
725 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
726 if (ret != OC_STACK_OK)
728 OICFree(certData->resArr);
731 DeleteCredList(trustCertChainCred);
733 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
737 OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
738 OicEncodingType_t encodingType, uint16_t *credId)
740 OIC_LOG(DEBUG, TAG, "IN SRPSaveTrustCertChain");
741 VERIFY_NON_NULL(TAG, trustCertChain, ERROR, OC_STACK_INVALID_PARAM);
742 VERIFY_NON_NULL(TAG, credId, ERROR, OC_STACK_INVALID_PARAM);
744 OCStackResult res = OC_STACK_ERROR;
746 OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(*cred));
747 VERIFY_NON_NULL(TAG, cred, ERROR, OC_STACK_NO_MEMORY);
749 memcpy(cred->subject.id, &WILDCARD_SUBJECT_ID, WILDCARD_SUBJECT_ID_LEN);
751 cred->credUsage= (char *)OICCalloc(1, strlen(TRUST_CA)+1 );
752 VERIFY_NON_NULL(TAG, cred->credUsage, ERROR, OC_STACK_NO_MEMORY);
753 OICStrcpy(cred->credUsage, strlen(TRUST_CA) + 1, TRUST_CA) ;
755 cred->credType = SIGNED_ASYMMETRIC_KEY;
757 cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize);
758 VERIFY_NON_NULL(TAG, cred->optionalData.data, ERROR, OC_STACK_NO_MEMORY);
759 memcpy(cred->optionalData.data, trustCertChain, chainSize);
760 cred->optionalData.len = chainSize;
761 cred->optionalData.encoding = encodingType;
763 res = AddCredential(cred);
764 if(res != OC_STACK_OK)
766 DeleteCredList(cred);
769 *credId = cred->credId;
771 OIC_LOG(DEBUG, TAG, "OUT SRPSaveTrustCertChain");
775 #endif // __WITH_X509__ || __WITH_TLS__
777 OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
778 const OCProvisionDev_t *pDev1,
779 const OCProvisionDev_t *pDev2,
780 OCProvisionResultCB resultCallback)
782 VERIFY_NON_NULL(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM);
783 if (SYMMETRIC_PAIR_WISE_KEY == type)
785 VERIFY_NON_NULL(TAG, pDev2, ERROR, OC_STACK_INVALID_PARAM);
789 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
790 return OC_STACK_INVALID_CALLBACK;
792 if (SYMMETRIC_PAIR_WISE_KEY == type &&
793 0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
795 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
796 return OC_STACK_INVALID_PARAM;
799 if (SYMMETRIC_PAIR_WISE_KEY == type &&
800 !(OWNER_PSK_LENGTH_128 == keySize || OWNER_PSK_LENGTH_256 == keySize))
802 OIC_LOG(INFO, TAG, "Invalid key size");
803 return OC_STACK_INVALID_PARAM;
806 OIC_LOG(INFO, TAG, "In SRPProvisionCredentials");
808 if (SYMMETRIC_PAIR_WISE_KEY == type)
810 bool linkExisits = true;
811 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExisits);
813 if (res != OC_STACK_OK)
815 OIC_LOG(ERROR, TAG, "Internal error occured");
820 OIC_LOG(ERROR, TAG, "Link already exists");
821 return OC_STACK_INVALID_PARAM;
825 OicUuid_t provTooldeviceID = {{0,}};
826 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
828 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
829 return OC_STACK_ERROR;
831 OIC_LOG(INFO, TAG, "retrieved deviceid");
834 case SYMMETRIC_PAIR_WISE_KEY:
836 const OCProvisionDev_t *firstDevice = pDev1;
837 const OCProvisionDev_t *secondDevice = pDev2;
839 OicSecCred_t *firstCred = NULL;
840 OicSecCred_t *secondCred = NULL;
841 OCStackResult res = PMGeneratePairWiseCredentials(type, keySize, &provTooldeviceID,
842 &firstDevice->doxm->deviceID, &secondDevice->doxm->deviceID,
843 &firstCred, &secondCred);
844 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
845 OIC_LOG(INFO, TAG, "Credentials generated successfully");
846 CredentialData_t *credData =
847 (CredentialData_t *) OICCalloc(1, sizeof(CredentialData_t));
848 if (NULL == credData)
852 OIC_LOG(ERROR, TAG, "Memory allocation problem");
853 return OC_STACK_NO_MEMORY;
855 credData->deviceInfo1 = firstDevice;
856 credData->deviceInfo2 = secondDevice;
857 credData->credInfo = secondCred;
859 credData->credInfoFirst = firstCred;
860 credData->numOfResults = 0;
861 credData->resultCallback = resultCallback;
862 // first call to provision creds to device1.
863 // second call to provision creds to device2.
866 (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
867 if (NULL == credData->resArr)
872 OIC_LOG(ERROR, TAG, "Memory allocation problem");
873 return OC_STACK_NO_MEMORY;
875 res = provisionCredentials(firstCred, firstDevice, credData, &provisionCredentialCB1);
876 if (OC_STACK_OK != res)
878 DeleteCredList(firstCred);
879 DeleteCredList(secondCred);
880 OICFree(credData->resArr);
883 OIC_LOG_V(INFO, TAG, "provisionCredentials returned: %d",res);
884 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
888 case SIGNED_ASYMMETRIC_KEY:
890 const OCProvisionDev_t *firstDevice = pDev1;
891 OicSecCred_t *cred = NULL;
892 OCStackResult res = PMGenerateCertificateCredentials(&provTooldeviceID,
893 &firstDevice->doxm->deviceID,&cred);
894 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
895 OIC_LOG(INFO, TAG, "Certificate credentials generated successfully");
896 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
897 if (NULL == certData)
900 OIC_LOG(ERROR, TAG, "Memory allocation problem");
901 return OC_STACK_NO_MEMORY;
904 certData->deviceInfo = firstDevice;
906 certData->credInfo = cred;
907 certData->numOfResults = 0;
908 certData->resultCallback = resultCallback;
910 certData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
911 if (NULL == certData->resArr)
913 DeleteCredList(cred);
915 OIC_LOG(ERROR, TAG, "Memory allocation problem");
916 return OC_STACK_NO_MEMORY;
919 res = provisionCertCred(cred, firstDevice, certData, &provisionCertCB);
920 if (OC_STACK_OK != res)
922 OICFree(certData->resArr);
925 DeleteCredList(cred);
926 OIC_LOG_V(INFO, TAG, "provisionCertCredentials returned: %d",res);
933 OIC_LOG(ERROR, TAG, "Invalid option.");
934 return OC_STACK_INVALID_PARAM;
937 return OC_STACK_ERROR;
941 * Internal Function to store results in result array during ACL provisioning.
943 static void registerResultForACLProvisioning(ACLData_t *aclData,
944 OCStackResult stackresult)
946 OIC_LOG_V(INFO, TAG, "Inside registerResultForACLProvisioning aclData->numOfResults is %d\n",
947 aclData->numOfResults);
948 memcpy(aclData->resArr[(aclData->numOfResults)].deviceId.id,
949 aclData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
950 aclData->resArr[(aclData->numOfResults)].res = stackresult;
951 ++(aclData->numOfResults);
955 * Callback handler of SRPProvisionACL.
957 * @param[in] ctx ctx value passed to callback from calling function.
958 * @param[in] UNUSED handle to an invocation
959 * @param[in] clientResponse Response from queries to remote servers.
960 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
961 * and OC_STACK_KEEP_TRANSACTION to keep it.
963 static OCStackApplicationResult SRPProvisionACLCB(void *ctx, OCDoHandle UNUSED,
964 OCClientResponse *clientResponse)
966 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionACLCB.");
968 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
969 ACLData_t *aclData = (ACLData_t*)ctx;
970 OCProvisionResultCB resultCallback = aclData->resultCallback;
974 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
976 registerResultForACLProvisioning(aclData, OC_STACK_RESOURCE_CHANGED);
977 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
980 OICFree(aclData->resArr);
982 return OC_STACK_DELETE_TRANSACTION;
985 registerResultForACLProvisioning(aclData, OC_STACK_ERROR);
986 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
989 OIC_LOG_V(ERROR, TAG, "SRPProvisionACLCB received Null clientResponse");
990 OICFree(aclData->resArr);
992 return OC_STACK_DELETE_TRANSACTION;
995 OCStackResult SRPProvisionACL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
996 OicSecAcl_t *acl, OCProvisionResultCB resultCallback)
998 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
999 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
1000 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1002 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1005 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1006 return OC_STACK_NO_MEMORY;
1008 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1009 if(OC_STACK_OK != AclToCBORPayload(acl, &secPayload->securityData, &secPayload->payloadSize))
1011 OCPayloadDestroy((OCPayload *)secPayload);
1012 OIC_LOG(ERROR, TAG, "Failed to AclToCBORPayload");
1013 return OC_STACK_NO_MEMORY;
1015 OIC_LOG(DEBUG, TAG, "Created payload for ACL:");
1016 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1018 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1019 if(!PMGenerateQuery(true,
1020 selectedDeviceInfo->endpoint.addr,
1021 selectedDeviceInfo->securePort,
1022 selectedDeviceInfo->connType,
1023 query, sizeof(query), OIC_RSRC_ACL_URI))
1025 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
1026 return OC_STACK_ERROR;
1028 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1030 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1031 cbData.cb = &SRPProvisionACLCB;
1032 ACLData_t *aclData = (ACLData_t *) OICCalloc(1, sizeof(ACLData_t));
1033 if (aclData == NULL)
1035 OCPayloadDestroy((OCPayload *)secPayload);
1036 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1037 return OC_STACK_NO_MEMORY;
1039 aclData->deviceInfo = selectedDeviceInfo;
1040 aclData->resultCallback = resultCallback;
1041 aclData->numOfResults=0;
1043 // call to provision ACL to device1.
1044 int noOfRiCalls = 1;
1045 aclData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1046 if (aclData->resArr == NULL)
1049 OCPayloadDestroy((OCPayload *)secPayload);
1050 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1051 return OC_STACK_NO_MEMORY;
1053 cbData.context = (void *)aclData;
1055 OCMethod method = OC_REST_POST;
1056 OCDoHandle handle = NULL;
1057 OIC_LOG(DEBUG, TAG, "Sending ACL info to resource server");
1058 OCStackResult ret = OCDoResource(&handle, method, query,
1059 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1060 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1061 if (ret != OC_STACK_OK)
1063 OICFree(aclData->resArr);
1066 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1071 * Internal Function to store results in result array during Direct-Pairing provisioning.
1073 static void registerResultForDirectPairingProvisioning(PconfData_t *pconfData,
1074 OCStackResult stackresult)
1076 OIC_LOG_V(INFO, TAG, "Inside registerResultForDirectPairingProvisioning "
1077 "pconfData->numOfResults is %d\n", pconfData->numOfResults);
1078 memcpy(pconfData->resArr[(pconfData->numOfResults)].deviceId.id,
1079 pconfData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
1080 pconfData->resArr[(pconfData->numOfResults)].res = stackresult;
1081 ++(pconfData->numOfResults);
1085 * Callback handler of SRPProvisionDirectPairing.
1087 * @param[in] ctx ctx value passed to callback from calling function.
1088 * @param[in] UNUSED handle to an invocation
1089 * @param[in] clientResponse Response from queries to remote servers.
1090 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1091 * and OC_STACK_KEEP_TRANSACTION to keep it.
1093 static OCStackApplicationResult SRPProvisionDirectPairingCB(void *ctx, OCDoHandle UNUSED,
1094 OCClientResponse *clientResponse)
1096 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionDirectPairingCB.");
1098 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
1099 PconfData_t *pconfData = (PconfData_t*)ctx;
1100 OCProvisionResultCB resultCallback = pconfData->resultCallback;
1104 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1106 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_OK);
1107 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
1110 OICFree(pconfData->resArr);
1112 return OC_STACK_DELETE_TRANSACTION;
1115 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_ERROR);
1116 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
1119 OIC_LOG_V(ERROR, TAG, "SRPProvisionDirectPairingCB received Null clientResponse");
1120 OICFree(pconfData->resArr);
1122 return OC_STACK_DELETE_TRANSACTION;
1125 OCStackResult SRPProvisionDirectPairing(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
1126 OicSecPconf_t *pconf, OCProvisionResultCB resultCallback)
1128 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
1129 VERIFY_NON_NULL(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
1130 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1132 // check direct-pairing capability
1133 if (true != selectedDeviceInfo->doxm->dpc)
1135 OIC_LOG(ERROR, TAG, "Resouce server does not have Direct-Pairing Capability ");
1136 return OC_STACK_UNAUTHORIZED_REQ;
1139 OicUuid_t provTooldeviceID = {.id={0}};
1140 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1142 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1143 return OC_STACK_ERROR;
1145 memcpy(&pconf->rownerID, &provTooldeviceID, sizeof(OicUuid_t));
1147 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1150 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1151 return OC_STACK_NO_MEMORY;
1153 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1155 if (OC_STACK_OK != PconfToCBORPayload(pconf, &(secPayload->securityData),
1156 &(secPayload->payloadSize)))
1158 OCPayloadDestroy((OCPayload*)secPayload);
1159 OIC_LOG(ERROR, TAG, "Failed to PconfToCborPayload");
1160 return OC_STACK_NO_MEMORY;
1162 OIC_LOG(DEBUG, TAG, "Created payload for pconf set");
1163 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1166 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1167 if(!PMGenerateQuery(true,
1168 selectedDeviceInfo->endpoint.addr,
1169 selectedDeviceInfo->securePort,
1170 selectedDeviceInfo->connType,
1171 query, sizeof(query), OIC_RSRC_PCONF_URI))
1173 OIC_LOG(ERROR, TAG, "SRPProvisionDirectPairing : Failed to generate query");
1174 return OC_STACK_ERROR;
1176 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1178 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1179 cbData.cb = &SRPProvisionDirectPairingCB;
1180 PconfData_t *pconfData = (PconfData_t *) OICCalloc(1, sizeof(PconfData_t));
1181 if (NULL == pconfData)
1183 OCPayloadDestroy((OCPayload*)secPayload);
1184 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1185 return OC_STACK_NO_MEMORY;
1187 pconfData->deviceInfo = selectedDeviceInfo;
1188 pconfData->resultCallback = resultCallback;
1189 pconfData->numOfResults=0;
1190 pconfData->ctx = ctx;
1191 // call to provision PCONF to device1.
1192 int noOfRiCalls = 1;
1193 pconfData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1194 if (NULL == pconfData->resArr)
1197 OCPayloadDestroy((OCPayload*)secPayload);
1198 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1199 return OC_STACK_NO_MEMORY;
1201 cbData.context = (void *)pconfData;
1203 OCMethod method = OC_REST_POST;
1204 OCDoHandle handle = NULL;
1205 OIC_LOG(DEBUG, TAG, "Sending PCONF info to resource server");
1206 OCStackResult ret = OCDoResource(&handle, method, query,
1207 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1208 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1209 if (OC_STACK_OK != ret)
1211 OICFree(pconfData->resArr);
1214 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1218 static void DeleteUnlinkData_t(UnlinkData_t *unlinkData)
1222 OICFree(unlinkData->unlinkDev);
1223 OICFree(unlinkData->unlinkRes);
1224 OICFree(unlinkData);
1228 static void registerResultForUnlinkDevices(UnlinkData_t *unlinkData, OCStackResult stackresult,
1231 if (NULL != unlinkData)
1233 OIC_LOG_V(INFO, TAG, "Inside registerResultForUnlinkDevices unlinkData->numOfResults is %d\n",
1234 unlinkData->numOfResults);
1235 OIC_LOG_V(INFO, TAG, "Stack result :: %d", stackresult);
1237 OicUuid_t *pUuid = &unlinkData->unlinkRes[(unlinkData->numOfResults)].deviceId;
1239 // Set result in the result array according to the position (devNum).
1240 if (idx != IDX_DB_UPDATE_RES)
1242 memcpy(pUuid->id, unlinkData->unlinkDev[idx].doxm->deviceID.id, sizeof(pUuid->id));
1245 { // When deivce ID is 000... this means it's the result of database update.
1246 memset(pUuid->id, 0, sizeof(pUuid->id));
1248 unlinkData->unlinkRes[(unlinkData->numOfResults)].res = stackresult;
1249 ++(unlinkData->numOfResults);
1250 OIC_LOG (INFO, TAG, "Out registerResultForUnlinkDevices");
1254 static OCStackResult SendDeleteCredentialRequest(void* ctx,
1255 OCClientResponseHandler respHandler,
1256 const OCProvisionDev_t* revokedDev,
1257 const OCProvisionDev_t* destDev)
1259 OIC_LOG(DEBUG, TAG, "IN SendDeleteCredentialRequest");
1261 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1263 return OC_STACK_INVALID_PARAM;
1267 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1268 if(OC_STACK_OK != ret)
1270 OIC_LOG(ERROR, TAG, "SendDeleteCredentialRequest : Failed to canonical UUID encoding");
1271 return OC_STACK_ERROR;
1274 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1276 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1277 char *srpUri = SRP_FORM_DELETE_CREDENTIAL;
1279 if(CA_ADAPTER_TCP == destDev->endpoint.adapter)
1281 srpUri = SRP_FORM_DELETE_CREDENTIAL_TCP;
1284 snRet = snprintf(reqBuf, sizeof(reqBuf), srpUri, destDev->endpoint.addr,
1285 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1289 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1290 return OC_STACK_ERROR;
1292 else if ((size_t)snRet >= sizeof(reqBuf))
1294 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1295 return OC_STACK_ERROR;
1298 OCCallbackData cbData;
1299 memset(&cbData, 0, sizeof(cbData));
1300 cbData.context = ctx;
1301 cbData.cb = respHandler;
1303 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1305 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1307 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1308 &destDev->endpoint, NULL,
1309 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1310 if (OC_STACK_OK != ret)
1312 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1314 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1319 static OCStackResult SendDeleteACLRequest(void* ctx,
1320 OCClientResponseHandler respHandler,
1321 const OCProvisionDev_t* revokedDev,
1322 const OCProvisionDev_t* destDev)
1324 OIC_LOG(DEBUG, TAG, "IN SendDeleteACLRequest");
1326 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1328 return OC_STACK_INVALID_PARAM;
1332 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1333 if(OC_STACK_OK != ret)
1335 OIC_LOG(ERROR, TAG, "SendDeleteACLRequest : Failed to canonical UUID encoding");
1336 return OC_STACK_ERROR;
1339 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1341 //coaps://0.0.0.0:5684/oic/sec/acl?subjectuuid=(Canonical ENCODED UUID)
1342 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, destDev->endpoint.addr,
1343 destDev->securePort, OIC_RSRC_ACL_URI, OIC_JSON_SUBJECTID_NAME, subID);
1347 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error (snprintf) %d\n", snRet);
1348 return OC_STACK_ERROR;
1350 else if ((size_t)snRet >= sizeof(reqBuf))
1352 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Truncated (snprintf) %d\n", snRet);
1353 return OC_STACK_ERROR;
1356 OCCallbackData cbData;
1357 memset(&cbData, 0, sizeof(cbData));
1358 cbData.context = ctx;
1359 cbData.cb = respHandler;
1361 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1363 OIC_LOG(DEBUG, TAG, "Sending remove ACL request to resource server");
1365 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1366 &destDev->endpoint, NULL,
1367 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1368 if (OC_STACK_OK != ret)
1370 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error in OCDoResource %d", ret);
1372 OIC_LOG(DEBUG, TAG, "OUT SendDeleteACLRequest");
1378 * Callback handler of unlink second device.
1380 * @param[in] ctx ctx value passed to callback from calling function.
1381 * @param[in] handle handle to an invocation
1382 * @param[in] clientResponse Response from queries to remote servers.
1383 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1384 * OC_STACK_KEEP_TRANSACTION to keep it.
1386 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1387 OCClientResponse *clientResponse)
1390 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1391 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1392 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1396 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1397 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1399 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1401 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1405 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1406 unlinkData->resultCallback(unlinkData->ctx,
1407 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1413 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1414 IDX_SECOND_DEVICE_RES);
1415 unlinkData->resultCallback(unlinkData->ctx,
1416 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1417 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1421 //Update provisioning DB when succes case.
1422 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1423 &unlinkData->unlinkDev[1].doxm->deviceID))
1425 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1426 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1427 unlinkData->resultCallback(unlinkData->ctx,
1428 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1431 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1435 DeleteUnlinkData_t(unlinkData);
1436 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1437 return OC_STACK_DELETE_TRANSACTION;
1442 * Callback handler of unlink first device.
1444 * @param[in] ctx ctx value passed to callback from calling function.
1445 * @param[in] handle handle to an invocation
1446 * @param[in] clientResponse Response from queries to remote servers.
1447 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1448 * OC_STACK_KEEP_TRANSACTION to keep it.
1450 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1451 OCClientResponse *clientResponse)
1453 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1454 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1455 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1460 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1461 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1463 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1465 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1467 // Second revocation request to second device.
1468 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1469 &unlinkData->unlinkDev[0],
1470 &unlinkData->unlinkDev[1] /*Dest*/);
1471 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1472 if (OC_STACK_OK != res)
1474 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1475 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1476 IDX_SECOND_DEVICE_RES);
1477 unlinkData->resultCallback(unlinkData->ctx,
1478 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1483 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1484 return OC_STACK_DELETE_TRANSACTION;
1489 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1491 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1492 unlinkData->unlinkRes, true);
1498 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1499 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1500 IDX_FIRST_DEVICE_RES );
1501 unlinkData->resultCallback(unlinkData->ctx,
1502 unlinkData->numOfResults, unlinkData->unlinkRes,
1504 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1508 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1509 DeleteUnlinkData_t(unlinkData);
1510 return OC_STACK_DELETE_TRANSACTION;
1514 * Function to unlink devices.
1515 * This function will remove the credential & relationship between the two devices.
1517 * @param[in] ctx Application context would be returned in result callback
1518 * @param[in] pTargetDev1 first device information to be unlinked.
1519 * @param[in] pTargetDev2 second device information to be unlinked.
1520 * @param[in] resultCallback callback provided by API user, callback will be called when
1521 * device unlink is finished.
1522 * @return OC_STACK_OK in case of success and other value otherwise.
1524 OCStackResult SRPUnlinkDevices(void* ctx,
1525 const OCProvisionDev_t* pTargetDev1,
1526 const OCProvisionDev_t* pTargetDev2,
1527 OCProvisionResultCB resultCallback)
1529 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1531 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1533 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1534 return OC_STACK_INVALID_PARAM;
1536 if (!resultCallback)
1538 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1539 return OC_STACK_INVALID_CALLBACK;
1541 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1543 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1544 return OC_STACK_INVALID_PARAM;
1547 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1548 PMPrintOCProvisionDev(pTargetDev1);
1549 PMPrintOCProvisionDev(pTargetDev2);
1551 // Mark the link status stale
1552 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1553 if (OC_STACK_OK != res)
1555 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1559 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1560 VERIFY_NON_NULL(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1562 //Initialize unlink data
1563 unlinkData->ctx = ctx;
1564 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1565 if (NULL == unlinkData->unlinkDev)
1567 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1568 res = OC_STACK_NO_MEMORY;
1572 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1573 if (NULL == unlinkData->unlinkRes)
1575 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1576 res = OC_STACK_NO_MEMORY;
1580 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1581 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1583 unlinkData->numOfResults = 0;
1584 unlinkData->resultCallback = resultCallback;
1586 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1587 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1588 if (OC_STACK_OK != res)
1590 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1597 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1598 DeleteUnlinkData_t(unlinkData);
1602 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1606 OICFree(pRemoveData->revokeTargetDev);
1607 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1608 OICFree(pRemoveData->removeRes);
1609 OICFree(pRemoveData);
1613 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1614 OCStackResult stackresult, bool hasError)
1616 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1617 removeData->numOfResults + 1);
1620 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1621 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1625 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1626 0, sizeof(pLinkedDevId->id) );
1628 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1629 removeData->hasError = hasError;
1630 ++(removeData->numOfResults);
1632 // If we get suffcient result from linked devices, we have to call user callback and do free
1633 if (removeData->sizeOfResArray == removeData->numOfResults)
1635 if(!removeData->hasError)
1637 // Remove device info from prvisioning database
1638 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1640 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1641 removeData->hasError = true;
1644 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1645 removeData->hasError);
1646 DeleteRemoveData_t(removeData);
1650 static void registerResultForResetDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1651 OCStackResult stackresult, bool hasError)
1653 OIC_LOG_V(INFO, TAG, "Inside registerResultForResetDevice removeData->numOfResults is %zu\n",
1654 removeData->numOfResults + 1);
1657 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1658 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1662 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1663 0, sizeof(pLinkedDevId->id) );
1665 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1666 removeData->hasError = hasError;
1667 ++(removeData->numOfResults);
1669 // If we get suffcient result from linked devices, we have to call user callback and do free
1670 if (removeData->sizeOfResArray == removeData->numOfResults)
1672 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1673 removeData->hasError);
1674 DeleteRemoveData_t(removeData);
1679 * Callback handler of unlink first device.
1681 * @param[in] ctx ctx value passed to callback from calling function.
1682 * @param[in] handle handle to an invocation
1683 * @param[in] clientResponse Response from queries to remote servers.
1684 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1685 * and OC_STACK_KEEP_TRANSACTION to keep it.
1687 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1688 OCClientResponse *clientResponse)
1690 //Update the delete credential into delete device context
1691 //Save the deleted status in delDevCtx
1693 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1694 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1695 OCStackResult res = OC_STACK_ERROR;
1697 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1701 OicUuid_t revDevUuid = {.id={0}};
1702 if(UUID_LENGTH == clientResponse->identity.id_length)
1704 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1705 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1707 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1708 if (OC_STACK_OK != res)
1710 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1711 registerResultForRemoveDevice(removeData, &revDevUuid,
1712 OC_STACK_INCONSISTENT_DB, true);
1714 return OC_STACK_DELETE_TRANSACTION;
1717 registerResultForRemoveDevice(removeData, &revDevUuid,
1718 OC_STACK_RESOURCE_DELETED, false);
1722 registerResultForRemoveDevice(removeData, &revDevUuid,
1723 clientResponse->result, true);
1724 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1729 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1730 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1732 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1735 * Since server's credential was deleted,
1736 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1738 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1739 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1743 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1749 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1750 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1754 return OC_STACK_DELETE_TRANSACTION;
1758 * Callback handler of reset device.
1760 * @param[in] ctx ctx value passed to callback from calling function.
1761 * @param[in] handle handle to an invocation
1762 * @param[in] clientResponse Response from queries to remote servers.
1763 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1764 * and OC_STACK_KEEP_TRANSACTION to keep it.
1766 static OCStackApplicationResult SRPSyncDeviceCredCB(void *delDevCtx, OCDoHandle handle,
1767 OCClientResponse *clientResponse)
1769 //Update the delete credential into delete device context
1770 //Save the deleted status in delDevCtx
1772 OIC_LOG_V(INFO, TAG, "Inside SRPSyncDeviceCredCB.");
1773 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1774 OCStackResult res = OC_STACK_ERROR;
1776 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1777 OCProvisionDev_t * pTargetDev = PMCloneOCProvisionDev(removeData->revokeTargetDev);
1778 OCProvisionResultCB resultCallback = removeData->resultCallback;
1781 OicUuid_t revDevUuid = {.id={0}};
1782 if(UUID_LENGTH == clientResponse->identity.id_length)
1784 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1785 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1787 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1788 if (OC_STACK_OK != res)
1790 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1791 registerResultForResetDevice(removeData, &revDevUuid,
1792 OC_STACK_INCONSISTENT_DB, true);
1794 return OC_STACK_DELETE_TRANSACTION;
1797 registerResultForResetDevice(removeData, &revDevUuid,
1798 OC_STACK_RESOURCE_DELETED, false);
1802 registerResultForResetDevice(removeData, &revDevUuid,
1803 clientResponse->result, false);
1804 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1809 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1810 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1812 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1815 * Since server's credential was deleted,
1816 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1818 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1819 registerResultForResetDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1823 registerResultForResetDevice(removeData, NULL, clientResponse->result, true);
1829 registerResultForResetDevice(removeData, NULL, OC_STACK_ERROR, true);
1830 OIC_LOG(ERROR, TAG, "SRPSyncDevice received Null clientResponse");
1833 SRPResetDevice(pTargetDev, resultCallback);
1835 return OC_STACK_DELETE_TRANSACTION;
1839 * Callback handler of reset device sync-up
1841 * @param[in] ctx ctx value passed to callback from calling function.
1842 * @param[in] handle handle to an invocation
1843 * @param[in] clientResponse Response from queries to remote servers.
1844 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1845 * and OC_STACK_KEEP_TRANSACTION to keep it.
1847 static OCStackApplicationResult SRPSyncDeviceACLCB(void *ctx, OCDoHandle handle,
1848 OCClientResponse *clientResponse)
1850 return OC_STACK_DELETE_TRANSACTION;
1854 * Callback handler of device remote reset.
1856 * @param[in] ctx ctx value passed to callback from calling function.
1857 * @param[in] handle handle to an invocation
1858 * @param[in] clientResponse Response from queries to remote servers.
1859 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1860 * and OC_STACK_KEEP_TRANSACTION to keep it.
1862 static OCStackApplicationResult SRPResetDeviceCB(void *ctx, OCDoHandle handle,
1863 OCClientResponse *clientResponse)
1865 OIC_LOG(DEBUG, TAG, "IN SRPResetDeviceCB");
1866 if(OC_STACK_OK == clientResponse->result)
1868 OIC_LOG(DEBUG, TAG, "Change Target Device Pstat Cm SUCCEEDED");
1871 // Delete Cred and ACL related to the target device.
1872 const OicSecCred_t *cred = NULL;
1873 OCProvisionDev_t * pTargetDev = (OCProvisionDev_t *)ctx;
1874 cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
1877 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to get credential of target device.");
1881 OCStackResult res = RemoveCredential(&cred->subject);
1882 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1884 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove credential.");
1888 res = RemoveACE(&cred->subject, NULL);
1889 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1891 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove ACL.");
1894 if (OC_STACK_OK != PDMDeleteDevice(&pTargetDev->doxm->deviceID))
1896 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to delete device from PDM");
1899 //Close the DTLS session of the reset device.
1900 CAEndpoint_t* endpoint = (CAEndpoint_t *)&clientResponse->devAddr;
1901 CAResult_t caResult = CACloseDtlsSession(endpoint);
1902 if(CA_STATUS_OK != caResult)
1904 OIC_LOG_V(WARNING, TAG, "OCResetDevice : Failed to close DTLS session : %d", caResult);
1908 * If there is no linked device, PM does not send any request.
1909 * So we should directly invoke the result callback to inform the result of OCResetDevice.
1911 if(OC_STACK_NO_RESOURCE == res)
1917 OICFree(pTargetDev);
1918 return OC_STACK_DELETE_TRANSACTION;
1922 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1923 OCProvisionDev_t* pOwnedDevList,
1924 OCUuidList_t* pLinkedUuidList,
1925 OCProvisionDev_t** ppLinkedDevList,
1926 size_t *numOfLinkedDev)
1928 // pOwnedDevList could be NULL. It means no alived and owned device now.
1929 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1930 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1932 return OC_STACK_INVALID_PARAM;
1936 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1937 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1939 // Mark the link status stale.
1940 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1941 if (OC_STACK_OK != res)
1943 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1944 return OC_STACK_INCONSISTENT_DB;
1949 // If this linked device is alive (power-on), add the deivce to the list.
1950 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1951 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1953 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1955 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1956 if (NULL == targetDev)
1958 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1959 return OC_STACK_NO_MEMORY;
1962 LL_PREPEND(*ppLinkedDevList, targetDev);
1969 *numOfLinkedDev = cnt;
1974 * Function to device revocation
1975 * This function will remove credential of target device from all devices in subnet.
1977 * @param[in] ctx Application context would be returned in result callback
1978 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1979 * @param[in] pTargetDev Device information to be revoked.
1980 * @param[in] resultCallback callback provided by API user, callback will be called when
1981 * credential revocation is finished.
1982 * @return OC_STACK_OK in case of success and other value otherwise.
1983 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1984 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1986 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1987 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1989 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1991 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1993 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1994 return OC_STACK_INVALID_PARAM;
1996 if (!resultCallback)
1998 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
1999 return OC_STACK_INVALID_CALLBACK;
2002 // Declare variables in here to handle error cases with goto statement.
2003 OCProvisionDev_t* pOwnedDevList = NULL;
2004 OCProvisionDev_t* pLinkedDevList = NULL;
2005 RemoveData_t* removeData = NULL;
2007 //1. Find all devices that has a credential of the revoked device
2008 OCUuidList_t* pLinkedUuidList = NULL;
2009 size_t numOfDevices = 0;
2010 OCStackResult res = OC_STACK_ERROR;
2011 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2012 if (OC_STACK_OK != res)
2014 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
2017 // if there is no related device, we can skip further process.
2018 if (0 == numOfDevices)
2020 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
2021 res = OC_STACK_CONTINUE;
2025 //2. Find owned device from the network
2026 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
2027 if (OC_STACK_OK != res)
2029 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
2033 //3. Make a list of devices to send DELETE credential request
2034 // by comparing owned devices from provisioning database with mutlicast discovery result.
2035 size_t numOfLinkedDev = 0;
2036 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2037 &pLinkedDevList, &numOfLinkedDev);
2038 if (OC_STACK_OK != res)
2040 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
2043 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2044 { // So we don't have to send request message.
2045 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
2046 res = OC_STACK_CONTINUE;
2050 // 4. Prepare RemoveData Context data.
2051 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2054 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
2055 res = OC_STACK_NO_MEMORY;
2059 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2060 if (!removeData->revokeTargetDev)
2062 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
2063 res = OC_STACK_NO_MEMORY;
2067 removeData->removeRes =
2068 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2069 if (!removeData->removeRes)
2071 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
2072 res = OC_STACK_NO_MEMORY;
2076 removeData->ctx = ctx;
2077 removeData->linkedDevList = pLinkedDevList;
2078 removeData->resultCallback = resultCallback;
2079 removeData->numOfResults = 0;
2080 removeData->sizeOfResArray = numOfLinkedDev;
2081 removeData->hasError = false;
2083 // 5. Send DELETE credential request to linked devices.
2084 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2085 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2086 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2088 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2089 removeData->revokeTargetDev, curDev);
2090 if (OC_STACK_OK != res)
2092 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
2093 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2097 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2101 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2102 PMDeleteDeviceList(pOwnedDevList);
2103 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
2105 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2108 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2109 PMDeleteDeviceList(pOwnedDevList);
2110 PMDeleteDeviceList(pLinkedDevList);
2113 OICFree(removeData->revokeTargetDev);
2114 OICFree(removeData->removeRes);
2115 OICFree(removeData);
2117 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");
2122 * Function to device revocation
2123 * This function will remove credential of target device from all devices in subnet.
2125 * @param[in] ctx Application context would be returned in result callback
2126 * @param[in] pOwnedDevList List of owned devices
2127 * @param[in] pTargetDev Device information to be revoked.
2128 * @param[in] resultCallback callback provided by API user, callback will be called when
2129 * credential revocation is finished.
2130 * @return OC_STACK_OK in case of success and other value otherwise.
2131 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2132 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2134 OCStackResult SRPRemoveDeviceWithoutDiscovery(void* ctx, const OCProvisionDev_t* pOwnedDevList,
2135 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2137 OIC_LOG(INFO, TAG, "IN SRPRemoveDeviceWithoutDiscovery");
2139 if (!pTargetDev || !pOwnedDevList)
2141 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL parameters");
2142 return OC_STACK_INVALID_PARAM;
2144 if (!resultCallback)
2146 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL Callback");
2147 return OC_STACK_INVALID_CALLBACK;
2150 // Declare variables in here to handle error cases with goto statement.
2151 OCProvisionDev_t* pLinkedDevList = NULL;
2152 RemoveData_t* removeData = NULL;
2154 //1. Find all devices that has a credential of the revoked device
2155 OCUuidList_t* pLinkedUuidList = NULL;
2156 size_t numOfDevices = 0;
2157 OCStackResult res = OC_STACK_ERROR;
2158 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2159 if (OC_STACK_OK != res)
2161 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to get linked devices information");
2164 // if there is no related device, we can skip further process.
2165 if (0 == numOfDevices)
2167 OIC_LOG(DEBUG, TAG, "SRPRemoveDeviceWithoutDiscovery : No linked device found.");
2168 res = OC_STACK_CONTINUE;
2172 //2. Make a list of devices to send DELETE credential request
2173 // by comparing owned devices from provisioning database with mutlicast discovery result.
2174 size_t numOfLinkedDev = 0;
2175 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2176 &pLinkedDevList, &numOfLinkedDev);
2177 if (OC_STACK_OK != res)
2179 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : GetListofDevToReqDeleteCred() failed");
2182 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2183 { // So we don't have to send request message.
2184 OIC_LOG(DEBUG, TAG, "SRPRemoveDeviceWithoutDiscovery : No alived & linked device found.");
2185 res = OC_STACK_CONTINUE;
2189 // 3. Prepare RemoveData Context data.
2190 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2193 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2194 res = OC_STACK_NO_MEMORY;
2198 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2199 if (!removeData->revokeTargetDev)
2201 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : PMCloneOCProvisionDev Failed");
2202 res = OC_STACK_NO_MEMORY;
2206 removeData->removeRes =
2207 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2208 if (!removeData->removeRes)
2210 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2211 res = OC_STACK_NO_MEMORY;
2215 removeData->ctx = ctx;
2216 removeData->linkedDevList = pLinkedDevList;
2217 removeData->resultCallback = resultCallback;
2218 removeData->numOfResults = 0;
2219 removeData->sizeOfResArray = numOfLinkedDev;
2220 removeData->hasError = false;
2222 // 5. Send DELETE credential request to linked devices.
2223 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2224 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2225 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2227 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2228 removeData->revokeTargetDev, curDev);
2229 if (OC_STACK_OK != res)
2231 OIC_LOG_V(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Fail to send the DELETE credential request to\
2232 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2236 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2240 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2241 OIC_LOG(INFO, TAG, "OUT SRPRemoveDeviceWithoutDiscovery");
2243 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2246 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2247 PMDeleteDeviceList(pLinkedDevList);
2250 OICFree(removeData->revokeTargetDev);
2251 OICFree(removeData->removeRes);
2252 OICFree(removeData);
2254 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDeviceWithoutDiscovery");
2259 * Function to sync-up credential and ACL of the target device.
2260 * This function will remove credential and ACL of target device from all devices in subnet.
2262 * @param[in] ctx Application context would be returned in result callback
2263 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
2264 * @param[in] pTargetDev Device information to be revoked.
2265 * @param[in] resultCallback callback provided by API user, callback will be called when
2266 * credential revocation is finished.
2267 * when there is an error, this user callback is called immediately.
2268 * @return OC_STACK_OK in case of success and other value otherwise.
2269 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2270 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2272 OCStackResult SRPSyncDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
2273 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2275 OIC_LOG(INFO, TAG, "IN SRPSyncDevice");
2276 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
2278 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL parameters");
2279 return OC_STACK_INVALID_PARAM;
2281 if (!resultCallback)
2283 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL Callback");
2284 return OC_STACK_INVALID_CALLBACK;
2287 // Declare variables in here to handle error cases with goto statement.
2288 OCProvisionDev_t* pOwnedDevList = NULL;
2289 OCProvisionDev_t* pLinkedDevList = NULL;
2290 RemoveData_t* removeData = NULL;
2292 //1. Find all devices that has a credential of the revoked device
2293 OCUuidList_t* pLinkedUuidList = NULL;
2294 size_t numOfDevices = 0;
2295 OCStackResult res = OC_STACK_ERROR;
2296 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2297 if (OC_STACK_OK != res)
2299 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to get linked devices information");
2302 // if there is no related device, we can skip further process.
2303 if (0 == numOfDevices)
2305 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No linked device found.");
2306 res = OC_STACK_CONTINUE;
2310 //2. Find owned device from the network
2311 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
2312 if (OC_STACK_OK != res)
2314 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to PMDeviceDiscovery");
2318 //3. Make a list of devices to send DELETE credential request
2319 // by comparing owned devices from provisioning database with mutlicast discovery result.
2320 size_t numOfLinkedDev = 0;
2321 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2322 &pLinkedDevList, &numOfLinkedDev);
2323 if (OC_STACK_OK != res)
2325 OIC_LOG(ERROR, TAG, "SRPSyncDevice : GetListofDevToReqDeleteCred() failed");
2328 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2329 { // So we don't have to send request message.
2330 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No alived & linked device found.");
2331 res = OC_STACK_CONTINUE;
2335 // 4. Prepare RemoveData Context data.
2336 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2339 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to memory allocation");
2340 res = OC_STACK_NO_MEMORY;
2344 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2345 if (!removeData->revokeTargetDev)
2347 OIC_LOG(ERROR, TAG, "SRPSyncDevice : PMCloneOCProvisionDev Failed");
2348 res = OC_STACK_NO_MEMORY;
2352 removeData->removeRes =
2353 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2354 if (!removeData->removeRes)
2356 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to allocate memory");
2357 res = OC_STACK_NO_MEMORY;
2361 removeData->ctx = ctx;
2362 removeData->linkedDevList = pLinkedDevList;
2363 removeData->resultCallback = resultCallback;
2364 removeData->numOfResults = 0;
2365 removeData->sizeOfResArray = numOfLinkedDev;
2366 removeData->hasError = false;
2368 // 5. Send DELETE credential request to linked devices.
2369 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2370 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2371 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2373 res = SendDeleteACLRequest((void*)removeData, &SRPSyncDeviceACLCB,
2374 removeData->revokeTargetDev, curDev);
2375 if (OC_STACK_OK != res)
2377 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE ACL request to\
2378 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2381 res = SendDeleteCredentialRequest((void*)removeData, &SRPSyncDeviceCredCB,
2382 removeData->revokeTargetDev, curDev);
2383 if (OC_STACK_OK != res)
2385 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE credential request to\
2386 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2387 totalRes = OC_STACK_ERROR;
2391 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2395 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2396 PMDeleteDeviceList(pOwnedDevList);
2397 OIC_LOG(INFO, TAG, "OUT SRPSyncDevice");
2399 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2402 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2403 PMDeleteDeviceList(pOwnedDevList);
2404 PMDeleteDeviceList(pLinkedDevList);
2407 OICFree(removeData->revokeTargetDev);
2408 OICFree(removeData->removeRes);
2409 OICFree(removeData);
2411 OIC_LOG(INFO, TAG, "OUT ERROR case SRPSyncDevice");
2416 * Function for remote reset
2417 * This function will send pstat PUT message to the target device to initiate remote reset.
2419 * @param[in] pTargetDev Device information to be revoked.
2420 * @param[in] resultCallback callback provided by API user, callback will be called when
2421 * credential revocation is finished.
2422 * when there is an error, this user callback is called immediately.
2423 * @return OC_STACK_OK in case of success and other value otherwise.
2424 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2425 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2427 OCStackResult SRPResetDevice(const OCProvisionDev_t* pTargetDev,
2428 OCProvisionResultCB resultCallback)
2430 OIC_LOG(INFO, TAG, "IN SRPResetDevice");
2433 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL parameters");
2434 return OC_STACK_INVALID_PARAM;
2436 if (!resultCallback)
2438 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL Callback");
2439 return OC_STACK_INVALID_CALLBACK;
2442 OCStackResult res = OC_STACK_ERROR;
2443 OicSecPstat_t * pstat = (OicSecPstat_t *) OICCalloc(1, sizeof(OicSecPstat_t));
2446 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2447 return OC_STACK_NO_MEMORY;
2451 pstat->isOp = false;
2452 memcpy(pstat->deviceID.id, pTargetDev->doxm->deviceID.id, sizeof(OicUuid_t));
2453 pstat->tm = TAKE_OWNER;
2454 pstat->om = (OicSecDpom_t)(SINGLE_SERVICE_SERVER_DRIVEN | MULTIPLE_SERVICE_CLIENT_DRIVEN);
2456 pstat->sm = (OicSecDpom_t *) OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
2457 if (NULL == pstat->sm)
2459 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2461 return OC_STACK_NO_MEMORY;
2463 pstat->sm[0] = (OicSecDpom_t)(SINGLE_SERVICE_SERVER_DRIVEN | MULTIPLE_SERVICE_CLIENT_DRIVEN);
2465 OCSecurityPayload * secPayload = (OCSecurityPayload *) OICCalloc(1, sizeof(OCSecurityPayload));
2468 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2469 res = OC_STACK_NO_MEMORY;
2472 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2474 if (OC_STACK_OK != PstatToCBORPayload(pstat, &(secPayload->securityData),
2475 &(secPayload->payloadSize), true))
2477 OCPayloadDestroy((OCPayload *) secPayload);
2478 OIC_LOG(ERROR, TAG, "Failed to PstatToCBORPayload");
2479 res = OC_STACK_NO_MEMORY;
2482 OIC_LOG(DEBUG, TAG, "Created payload for pstat set");
2483 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2485 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2486 if (!PMGenerateQuery(true,
2487 pTargetDev->endpoint.addr,
2488 pTargetDev->securePort,
2489 pTargetDev->connType,
2490 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2492 OIC_LOG(ERROR, TAG, "SRPResetDevice : Failed to generate query");
2493 OCPayloadDestroy((OCPayload *) secPayload);
2494 res = OC_STACK_ERROR;
2497 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2499 OCProvisionDev_t * targetDev = PMCloneOCProvisionDev(pTargetDev);
2500 OCCallbackData cbData = { .context = NULL, .cb = NULL, .cd = NULL };
2501 cbData.cb = &SRPResetDeviceCB;
2502 cbData.context = (void *) targetDev;
2504 OCMethod method = OC_REST_POST;
2505 OCDoHandle handle = NULL;
2506 OIC_LOG(DEBUG, TAG, "Sending PSTAT info to resource server");
2507 res = OCDoResource(&handle, method, query,
2508 &targetDev->endpoint, (OCPayload *)secPayload,
2509 targetDev->connType, OC_LOW_QOS, &cbData, NULL, 0);\
2510 if (OC_STACK_OK != res)
2512 OIC_LOG(ERROR, TAG, "OCStack resource error");
2518 OIC_LOG(INFO, TAG, "OUT SRPResetDevice");
2523 * Internal Function to store results in result array during GetCredResourceCB.
2525 static void registerResultForGetCredResourceCB(GetSecData_t *GetSecData,
2526 OCStackResult stackresult)
2528 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetCredResourceCB "
2529 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2530 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2531 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2532 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2533 ++(GetSecData->numOfResults);
2537 * Callback handler of SRPGetCredResource.
2539 * @param[in] ctx ctx value passed to callback from calling function.
2540 * @param[in] UNUSED handle to an invocation
2541 * @param[in] clientResponse Response from queries to remote servers.
2542 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2543 * and OC_STACK_KEEP_TRANSACTION to keep it.
2545 static OCStackApplicationResult SRPGetCredResourceCB(void *ctx, OCDoHandle UNUSED,
2546 OCClientResponse *clientResponse)
2548 OIC_LOG_V(INFO, TAG, "Inside SRPGetCredResourceCB.");
2550 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2551 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2552 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2556 if(OC_STACK_OK == clientResponse->result)
2558 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2559 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2561 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2563 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2564 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2567 OICFree(GetSecData->resArr);
2568 OICFree(GetSecData);
2570 return OC_STACK_DELETE_TRANSACTION;
2573 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2574 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2577 OIC_LOG_V(ERROR, TAG, "SRPGetCredResourceCB received Null clientResponse");
2578 OICFree(GetSecData->resArr);
2579 OICFree(GetSecData);
2581 return OC_STACK_DELETE_TRANSACTION;
2585 OCStackResult SRPGetCredResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2586 OCProvisionResultCB resultCallback)
2588 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2589 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2591 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2592 if(!PMGenerateQuery(true,
2593 selectedDeviceInfo->endpoint.addr,
2594 selectedDeviceInfo->securePort,
2595 selectedDeviceInfo->connType,
2596 query, sizeof(query), OIC_RSRC_CRED_URI))
2598 OIC_LOG(ERROR, TAG, "SRPGetCredResource : Failed to generate query");
2599 return OC_STACK_ERROR;
2601 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2603 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2604 cbData.cb = &SRPGetCredResourceCB;
2605 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2606 if (NULL == GetSecData)
2608 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2609 return OC_STACK_NO_MEMORY;
2611 GetSecData->deviceInfo = selectedDeviceInfo;
2612 GetSecData->resultCallback = resultCallback;
2613 GetSecData->numOfResults=0;
2614 GetSecData->ctx = ctx;
2616 int noOfRiCalls = 1;
2617 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2618 if (NULL == GetSecData->resArr)
2620 OICFree(GetSecData);
2621 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2622 return OC_STACK_NO_MEMORY;
2624 cbData.context = (void *)GetSecData;
2626 OCMethod method = OC_REST_GET;
2627 OCDoHandle handle = NULL;
2628 OIC_LOG(DEBUG, TAG, "Sending Get Cred to resource server");
2629 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2630 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2631 if (OC_STACK_OK != ret)
2633 OIC_LOG(ERROR, TAG, "OCStack resource error");
2634 OICFree(GetSecData->resArr);
2635 OICFree(GetSecData);
2637 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2638 OIC_LOG(DEBUG, TAG, "OUT SRPGetCredResource");
2644 * Internal Function to store results in result array during GetACLResourceCB.
2646 static void registerResultForGetACLResourceCB(GetSecData_t *GetSecData,
2647 OCStackResult stackresult)
2649 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetACLResourceCB "
2650 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2651 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2652 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2653 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2654 ++(GetSecData->numOfResults);
2658 * Callback handler of SRPGetACLResource.
2660 * @param[in] ctx ctx value passed to callback from calling function.
2661 * @param[in] UNUSED handle to an invocation
2662 * @param[in] clientResponse Response from queries to remote servers.
2663 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2664 * and OC_STACK_KEEP_TRANSACTION to keep it.
2666 static OCStackApplicationResult SRPGetACLResourceCB(void *ctx, OCDoHandle UNUSED,
2667 OCClientResponse *clientResponse)
2669 OIC_LOG_V(INFO, TAG, "Inside SRPGetACLResourceCB.");
2671 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2672 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2673 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2677 if(OC_STACK_OK == clientResponse->result)
2679 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2680 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2682 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2684 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2685 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2688 OICFree(GetSecData->resArr);
2689 OICFree(GetSecData);
2691 return OC_STACK_DELETE_TRANSACTION;
2694 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2695 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2698 OIC_LOG_V(ERROR, TAG, "SRPGetACLResourceCB received Null clientResponse");
2699 OICFree(GetSecData->resArr);
2700 OICFree(GetSecData);
2702 return OC_STACK_DELETE_TRANSACTION;
2706 OCStackResult SRPGetACLResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2707 OCProvisionResultCB resultCallback)
2709 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2710 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2712 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2713 if(!PMGenerateQuery(true,
2714 selectedDeviceInfo->endpoint.addr,
2715 selectedDeviceInfo->securePort,
2716 selectedDeviceInfo->connType,
2717 query, sizeof(query), OIC_RSRC_ACL_URI))
2719 OIC_LOG(ERROR, TAG, "SRPGetACLResource : Failed to generate query");
2720 return OC_STACK_ERROR;
2722 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2724 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2725 cbData.cb = &SRPGetACLResourceCB;
2726 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2727 if (NULL == GetSecData)
2729 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2730 return OC_STACK_NO_MEMORY;
2732 GetSecData->deviceInfo = selectedDeviceInfo;
2733 GetSecData->resultCallback = resultCallback;
2734 GetSecData->numOfResults=0;
2735 GetSecData->ctx = ctx;
2737 int noOfRiCalls = 1;
2738 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2739 if (NULL == GetSecData->resArr)
2741 OICFree(GetSecData);
2742 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2743 return OC_STACK_NO_MEMORY;
2745 cbData.context = (void *)GetSecData;
2747 OCMethod method = OC_REST_GET;
2748 OCDoHandle handle = NULL;
2749 OIC_LOG(DEBUG, TAG, "Sending Get ACL to resource server");
2750 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2751 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2752 if (OC_STACK_OK != ret)
2754 OIC_LOG(ERROR, TAG, "OCStack resource error");
2755 OICFree(GetSecData->resArr);
2756 OICFree(GetSecData);
2758 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2759 OIC_LOG(DEBUG, TAG, "OUT SRPGetACLResource");