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 "oic_string.h"
33 #include "aclresource.h"
34 #include "pstatresource.h"
35 #include "srmresourcestrings.h"
36 #include "credresource.h"
37 #include "doxmresource.h"
38 #include "pconfresource.h"
39 #include "credentialgenerator.h"
40 #include "cainterface.h"
43 #include "pmutility.h"
44 #include "provisioningdatabasemanager.h"
47 #include "ocpayload.h"
48 #include "srmutility.h"
51 #include "crlresource.h"
54 #define TAG "OIC_SRPAPI"
57 * Macro to verify argument is not equal to NULL.
58 * eg: VERIFY_NON_NULL(TAG, ptrData, ERROR,OC_STACK_ERROR);
60 #define VERIFY_NON_NULL(tag, arg, logLevel, retValue) { if (NULL == (arg)) \
61 { OIC_LOG((logLevel), tag, #arg " is NULL"); return retValue; } }
64 * Macro to verify success of operation.
65 * eg: VERIFY_SUCCESS(TAG, OC_STACK_OK == foo(), ERROR, OC_STACK_ERROR);
67 #define VERIFY_SUCCESS(tag, op, logLevel, retValue) { if (!(op)) \
68 {OIC_LOG((logLevel), tag, #op " failed!!"); return retValue;} }
71 trustCertChainContext_t g_trustCertChainNotifier;
74 * Structure to carry credential data to callback.
76 typedef struct CredentialData CredentialData_t;
79 void *ctx; /**< Pointer to user context.**/
80 const OCProvisionDev_t *deviceInfo1; /**< Pointer to OCProvisionDev_t.**/
81 const OCProvisionDev_t *deviceInfo2; /**< Pointer to OCProvisionDev_t.**/
82 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
83 OicSecCred_t *credInfoFirst; /**< Pointer to OicSecCred_t.**/
84 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
85 OCProvisionResult_t *resArr; /**< Result array.**/
86 int numOfResults; /**< Number of results in result array.**/
90 * Structure to carry ACL provision API data to callback.
92 typedef struct ACLData ACLData_t;
95 void *ctx; /**< Pointer to user context.**/
96 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
97 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
98 OCProvisionResult_t *resArr; /**< Result array.**/
99 int numOfResults; /**< Number of results in result array.**/
102 // Structure to carry get security resource APIs data to callback.
103 typedef struct GetSecData GetSecData_t;
106 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
107 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
108 OCProvisionResult_t *resArr; /**< Result array.**/
109 int numOfResults; /**< Number of results in result array.**/
113 * Structure to carry PCONF provision API data to callback.
115 typedef struct PconfData PconfData_t;
118 void *ctx; /**< Pointer to user context.**/
119 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
120 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
121 OCProvisionResult_t *resArr; /**< Result array.**/
122 int numOfResults; /**< Number of results in result array.**/
125 // Enum type index for unlink callback.
127 IDX_FIRST_DEVICE_RES = 0, // index for resulf of the first device
128 IDX_SECOND_DEVICE_RES, // index for result of the second device
129 IDX_DB_UPDATE_RES // index for result of updating provisioning database.
132 // Structure to carry unlink APIs data to callback.
133 typedef struct UnlinkData UnlinkData_t;
136 OCProvisionDev_t* unlinkDev; /**< Pointer to OCProvisionDev_t to be unlinked.**/
137 OCProvisionResult_t* unlinkRes; /**< Result array.**/
138 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
139 int numOfResults; /**< Number of results in result array.**/
142 //Example of DELETE cred request -> coaps://0.0.0.0:5684/oic/sec/cred?sub=(BASE64 ENCODED UUID)
143 const char * SRP_FORM_DELETE_CREDENTIAL = "coaps://[%s]:%d%s?%s=%s";
144 const char * SRP_FORM_DELETE_CREDENTIAL_TCP = "coaps+tcp://[%s]:%d%s?%s=%s";
146 // Structure to carry remove APIs data to callback.
147 typedef struct RemoveData RemoveData_t;
150 OCProvisionDev_t* revokeTargetDev; /**< Device which is going to be revoked..**/
151 OCProvisionDev_t* linkedDevList; /**< A list of devices which have invalid credential.**/
152 OCProvisionResult_t* removeRes; /**< Result array.**/
153 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
154 size_t numOfResults; /**< Number of results in result array.**/
155 size_t sizeOfResArray;
162 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
163 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
164 OCClientResponseHandler responseHandler);
168 * Internal function to update result in result array.
170 static void registerResultForCredProvisioning(CredentialData_t *credData,
171 OCStackResult stackresult, int cause)
174 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",credData->numOfResults);
177 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
178 credData->deviceInfo1->doxm->deviceID.id,UUID_LENGTH);
182 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
183 credData->deviceInfo2->doxm->deviceID.id,UUID_LENGTH);
185 credData->resArr[(credData->numOfResults)].res = stackresult;
186 ++(credData->numOfResults);
190 * Callback handler for handling callback of provisioning device 2.
192 * @param[in] ctx ctx value passed to callback from calling function.
193 * @param[in] UNUSED handle to an invocation
194 * @param[in] clientResponse Response from queries to remote servers.
195 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
196 * and OC_STACK_KEEP_TRANSACTION to keep it.
198 static OCStackApplicationResult provisionCredentialCB2(void *ctx, OCDoHandle UNUSED,
199 OCClientResponse *clientResponse)
201 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
202 CredentialData_t *credData = (CredentialData_t *) ctx;
205 OCProvisionResultCB resultCallback = credData->resultCallback;
206 OIC_LOG(INFO, TAG, "provisionCredentialCB2 called");
209 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
211 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CHANGED, 2);
212 OCStackResult res = PDMLinkDevices(&credData->deviceInfo1->doxm->deviceID,
213 &credData->deviceInfo2->doxm->deviceID);
214 if (OC_STACK_OK != res)
216 OIC_LOG(ERROR, TAG, "Error occured on PDMLinkDevices");
217 return OC_STACK_DELETE_TRANSACTION;
219 OIC_LOG(INFO, TAG, "Link created successfully");
221 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
224 OICFree(credData->resArr);
226 return OC_STACK_DELETE_TRANSACTION;
230 OIC_LOG(INFO, TAG, "provisionCredentialCB2 received Null clientResponse");
231 registerResultForCredProvisioning(credData, OC_STACK_ERROR, 2);
232 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
235 OICFree(credData->resArr);
237 return OC_STACK_DELETE_TRANSACTION;
241 * Callback handler for handling callback of provisioning device 1.
243 * @param[in] ctx ctx value passed to callback from calling function.
244 * @param[in] UNUSED handle to an invocation
245 * @param[in] clientResponse Response from queries to remote servers.
246 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
247 * and OC_STACK_KEEP_TRANSACTION to keep it.
249 static OCStackApplicationResult provisionCredentialCB1(void *ctx, OCDoHandle UNUSED,
250 OCClientResponse *clientResponse)
252 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
254 CredentialData_t* credData = (CredentialData_t*) ctx;
255 OICFree(credData->credInfoFirst);
256 const OCProvisionDev_t *deviceInfo = credData->deviceInfo2;
257 OicSecCred_t *credInfo = credData->credInfo;
258 const OCProvisionResultCB resultCallback = credData->resultCallback;
261 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
263 // send credentials to second device
264 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CHANGED,1);
265 OCStackResult res = provisionCredentials(credInfo, deviceInfo, credData,
266 provisionCredentialCB2);
267 DeleteCredList(credInfo);
268 if (OC_STACK_OK != res)
270 registerResultForCredProvisioning(credData, res,2);
271 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
274 OICFree(credData->resArr);
281 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
282 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
285 OICFree(credData->resArr);
292 OIC_LOG(INFO, TAG, "provisionCredentialCB received Null clientResponse for first device");
293 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
294 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
297 DeleteCredList(credInfo);
298 OICFree(credData->resArr);
302 return OC_STACK_DELETE_TRANSACTION;
308 * Internal function for handling credential generation and sending credential to resource server.
310 * @param[in] cred Instance of cred resource.
311 * @param[in] deviceInfo information about device to which credential is to be provisioned.
312 * @param[in] responseHandler callbak called by OC stack when request API receives response.
313 * @return OC_STACK_OK in case of success and other value otherwise.
315 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
316 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
317 OCClientResponseHandler responseHandler)
319 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
322 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
323 return OC_STACK_NO_MEMORY;
325 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
327 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
328 &secPayload->payloadSize, secureFlag);
329 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
331 OCPayloadDestroy((OCPayload *)secPayload);
332 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
333 return OC_STACK_NO_MEMORY;
336 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
337 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
338 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
339 if(!PMGenerateQuery(true,
340 deviceInfo->endpoint.addr,
341 deviceInfo->securePort,
342 deviceInfo->connType,
343 query, sizeof(query), OIC_RSRC_CRED_URI))
345 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
346 OCPayloadDestroy((OCPayload *)secPayload);
347 return OC_STACK_ERROR;
349 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
351 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
352 cbData.cb = responseHandler;
353 cbData.context = (void *) credData;
356 OCDoHandle handle = NULL;
357 OCMethod method = OC_REST_POST;
358 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
359 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
360 OIC_LOG_V(INFO, TAG, "OCDoResource::Credential provisioning returned : %d",ret);
361 if (ret != OC_STACK_OK)
363 OIC_LOG(ERROR, TAG, "OCStack resource error");
369 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
371 * Structure to carry certificate data to callback.
373 typedef struct CertificateData CertData_t;
374 struct CertificateData
376 void *ctx; /**< Pointer to user context.**/
377 const OCProvisionDev_t *deviceInfo; /**< Pointer to OCProvisionDev_t.**/
378 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
379 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
380 OCProvisionResult_t *resArr; /**< Result array.**/
381 int numOfResults; /**< Number of results in result array.**/
385 * Internal function to update result in result array.
387 static void registerResultForCertProvisioning(CertData_t *certData,
388 OCStackResult stackresult)
391 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",certData->numOfResults);
392 memcpy(certData->resArr[(certData->numOfResults)].deviceId.id,
393 certData->deviceInfo->doxm->deviceID.id,UUID_LENGTH);
394 certData->resArr[(certData->numOfResults)].res = stackresult;
395 ++(certData->numOfResults);
399 * Internal function for handling credential generation and sending cretificate credential.
401 * @param[in] cred Instance of cred resource.
402 * @param[in] deviceInfo information about device to which credential is to be provisioned.
403 * @param[in] responseHandler callbak called by OC stack when request API receives response.
404 * @return OC_STACK_OK in case of success and other value otherwise.
406 static OCStackResult provisionCertCred(const OicSecCred_t *cred,
407 const OCProvisionDev_t *deviceInfo, CertData_t *certData,
408 OCClientResponseHandler responseHandler)
410 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
413 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
414 return OC_STACK_NO_MEMORY;
416 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
418 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
419 &secPayload->payloadSize, secureFlag);
421 if ((OC_STACK_OK != res) || (NULL == secPayload->securityData))
424 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
425 return OC_STACK_NO_MEMORY;
428 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
429 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
430 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
431 if(!PMGenerateQuery(true,
432 deviceInfo->endpoint.addr,
433 deviceInfo->securePort,
434 deviceInfo->connType,
435 query, sizeof(query), OIC_RSRC_CRED_URI))
437 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
438 OCPayloadDestroy((OCPayload *)secPayload);
439 return OC_STACK_ERROR;
441 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
443 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
444 cbData.cb = responseHandler;
445 cbData.context = (void *) certData;
448 OCDoHandle handle = NULL;
449 OCMethod method = OC_REST_POST;
450 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
451 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
452 OIC_LOG_V(INFO, TAG, "OCDoResource::Certificate provisioning returned : %d",ret);
453 if (ret != OC_STACK_OK)
455 OIC_LOG(ERROR, TAG, "OCStack resource error");
461 OCStackResult SRPRegisterTrustCertChainNotifier(void *ctx, TrustCertChainChangeCB callback)
463 if (g_trustCertChainNotifier.callback)
465 OIC_LOG(ERROR, TAG, "Can't register Notifier, Unregister previous one");
466 return OC_STACK_ERROR;
469 g_trustCertChainNotifier.callback = callback;
470 g_trustCertChainNotifier.context = ctx;
474 void SRPRemoveTrustCertChainNotifier()
476 g_trustCertChainNotifier.callback = NULL;
477 g_trustCertChainNotifier.context = NULL;
482 * Callback handler for handling callback of certificate provisioning device.
484 * @param[in] ctx ctx value passed to callback from calling function.
485 * @param[in] UNUSED handle to an invocation
486 * @param[in] clientResponse Response from queries to remote servers.
487 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
488 * and OC_STACK_KEEP_TRANSACTION to keep it.
490 static OCStackApplicationResult provisionCertCB(void *ctx, OCDoHandle UNUSED,
491 OCClientResponse *clientResponse)
493 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
494 CertData_t *certData = (CertData_t *) ctx;
497 OCProvisionResultCB resultCallback = certData->resultCallback;
498 OIC_LOG(INFO, TAG, "provisionCertCred called");
501 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
503 registerResultForCertProvisioning(certData, OC_STACK_RESOURCE_CHANGED);
504 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
507 OICFree(certData->resArr);
509 return OC_STACK_DELETE_TRANSACTION;
513 OIC_LOG(INFO, TAG, "provisionCertCredCB received Null clientResponse");
514 registerResultForCertProvisioning(certData, OC_STACK_ERROR);
515 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
518 OICFree(certData->resArr);
520 return OC_STACK_DELETE_TRANSACTION;
523 OCStackResult SRPProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint16_t credId,
524 const OCProvisionDev_t *selectedDeviceInfo, OCProvisionResultCB resultCallback)
526 OIC_LOG(INFO, TAG, "In SRPProvisionTrustCertChain");
527 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
528 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
529 if (SIGNED_ASYMMETRIC_KEY != type || NULL == type)
531 OIC_LOG(INFO, TAG, "Invalid key type");
532 return OC_STACK_INVALID_PARAM;
535 OicSecCred_t *trustCertChainCred = GetCredEntryByCredId(credId);
536 if(NULL == trustCertChainCred)
538 OIC_LOG(ERROR, TAG, "Can not find matched Trust Cert. Chain.");
539 return OC_STACK_NO_RESOURCE;
542 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
545 DeleteCredList(trustCertChainCred);
546 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
547 return OC_STACK_NO_MEMORY;
549 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
551 if(OC_STACK_OK != CredToCBORPayload(trustCertChainCred, &secPayload->securityData, &secPayload->payloadSize, secureFlag))
553 DeleteCredList(trustCertChainCred);
554 OCPayloadDestroy((OCPayload *)secPayload);
555 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
556 return OC_STACK_NO_MEMORY;
558 DeleteCredList(trustCertChainCred);
559 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
560 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
562 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
563 if(!PMGenerateQuery(true,
564 selectedDeviceInfo->endpoint.addr,
565 selectedDeviceInfo->securePort,
566 selectedDeviceInfo->connType,
567 query, sizeof(query), OIC_RSRC_CRED_URI))
569 OIC_LOG(ERROR, TAG, "SRPProvisionTrustCertChain : Failed to generate query");
570 OCPayloadDestroy((OCPayload *)secPayload);
571 return OC_STACK_ERROR;
573 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
575 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
576 cbData.cb = &provisionCertCB;
577 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
578 if (NULL == certData)
580 OIC_LOG(ERROR, TAG, "Memory allocation problem");
581 OCPayloadDestroy((OCPayload *)secPayload);
582 return OC_STACK_NO_MEMORY;
584 certData->deviceInfo = selectedDeviceInfo;
585 certData->resultCallback = resultCallback;
586 certData->credInfo = NULL; //credInfo not used in the response handler
587 certData->numOfResults=0;
591 certData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
592 if (certData->resArr == NULL)
595 OCPayloadDestroy((OCPayload *)secPayload);
596 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
597 return OC_STACK_NO_MEMORY;
599 cbData.context = (void *)certData;
601 OCMethod method = OC_REST_POST;
602 OCDoHandle handle = NULL;
603 OIC_LOG(DEBUG, TAG, "Sending Cred info to resource server");
604 OCStackResult ret = OCDoResource(&handle, method, query,
605 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
606 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
607 if (ret != OC_STACK_OK)
609 OICFree(certData->resArr);
613 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
617 OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
618 OicEncodingType_t encodingType, uint16_t *credId)
620 OIC_LOG(DEBUG, TAG, "IN SRPSaveTrustCertChain");
621 VERIFY_NON_NULL(TAG, trustCertChain, ERROR, OC_STACK_INVALID_PARAM);
622 VERIFY_NON_NULL(TAG, credId, ERROR, OC_STACK_INVALID_PARAM);
624 OCStackResult res = OC_STACK_ERROR;
626 OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(*cred));
627 VERIFY_NON_NULL(TAG, cred, ERROR, OC_STACK_NO_MEMORY);
629 memcpy(cred->subject.id, &WILDCARD_SUBJECT_ID, WILDCARD_SUBJECT_ID_LEN);
631 cred->credUsage= (char *)OICCalloc(1, strlen(TRUST_CA)+1 );
632 VERIFY_NON_NULL(TAG, cred->credUsage, ERROR, OC_STACK_NO_MEMORY);
633 OICStrcpy(cred->credUsage, strlen(TRUST_CA) + 1, TRUST_CA) ;
635 cred->credType = SIGNED_ASYMMETRIC_KEY;
637 if (encodingType == OIC_ENCODING_PEM)
639 cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize + 1);
640 VERIFY_NON_NULL(TAG, cred->optionalData.data, ERROR, OC_STACK_NO_MEMORY);
641 cred->optionalData.len = chainSize + 1;
645 cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize);
646 VERIFY_NON_NULL(TAG, cred->optionalData.data, ERROR, OC_STACK_NO_MEMORY);
647 cred->optionalData.len = chainSize;
649 memcpy(cred->optionalData.data, trustCertChain, chainSize);
650 cred->optionalData.encoding = encodingType;
652 res = AddCredential(cred);
653 if(res != OC_STACK_OK)
655 DeleteCredList(cred);
658 *credId = cred->credId;
660 if (g_trustCertChainNotifier.callback)
662 uint8_t *certChain = (uint8_t*)OICCalloc(1, sizeof(uint8_t) * chainSize);
663 VERIFY_NON_NULL(TAG, certChain, ERROR, OC_STACK_NO_MEMORY);
664 memcpy(certChain, trustCertChain, chainSize);
665 g_trustCertChainNotifier.callback(g_trustCertChainNotifier.context, credId,
666 certChain, chainSize);
670 OIC_LOG(DEBUG, TAG, "OUT SRPSaveTrustCertChain");
676 OCStackResult SRPSaveOwnCertChain(OicSecCert_t * cert, OicSecKey_t * key, uint16_t *credId)
678 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
679 VERIFY_NON_NULL(TAG, cert, ERROR, OC_STACK_INVALID_PARAM);
680 VERIFY_NON_NULL(TAG, cert->data, ERROR, OC_STACK_INVALID_PARAM);
681 VERIFY_NON_NULL(TAG, key, ERROR, OC_STACK_INVALID_PARAM);
682 VERIFY_NON_NULL(TAG, key->data, ERROR, OC_STACK_INVALID_PARAM);
683 VERIFY_NON_NULL(TAG, credId, ERROR, OC_STACK_INVALID_PARAM);
685 OCStackResult res = OC_STACK_ERROR;
687 OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(*cred));
688 VERIFY_NON_NULL(TAG, cred, ERROR, OC_STACK_NO_MEMORY);
690 OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
692 if (OC_STACK_OK != GetDoxmDeviceID(&cred->subject))
694 OIC_LOG(ERROR, TAG, "Cann't get the device id(GetDoxmDeviceID)");
697 cred->credUsage= (char *)OICCalloc(1, strlen(PRIMARY_CERT)+1 );
698 VERIFY_NON_NULL(TAG, cred->credUsage, ERROR, OC_STACK_NO_MEMORY);
699 OICStrcpy(cred->credUsage, strlen(PRIMARY_CERT) + 1, PRIMARY_CERT) ;
701 cred->credType = SIGNED_ASYMMETRIC_KEY;
703 OicSecCert_t *publicData = &cred->publicData;
704 publicData->data = (uint8_t *)OICCalloc(1, cert->len);
705 VERIFY_NON_NULL(TAG, publicData->data, ERROR, OC_STACK_NO_MEMORY);
706 memcpy(publicData->data, cert->data, cert->len);
707 publicData->len = cert->len;
709 OicSecKey_t *privateData = &cred->privateData;
710 privateData->data = (uint8_t *)OICCalloc(1, key->len);
711 VERIFY_NON_NULL(TAG, privateData->data, ERROR, OC_STACK_NO_MEMORY);
712 memcpy(privateData->data, key->data, key->len);
713 privateData->len = key->len;
714 privateData->encoding = key->encoding;
716 res = AddCredential(cred);
717 if(res != OC_STACK_OK)
719 DeleteCredList(cred);
722 *credId = cred->credId;
724 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
728 #endif // __WITH_DTLS__ || __WITH_TLS__
730 OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
731 const OCProvisionDev_t *pDev1,
732 const OCProvisionDev_t *pDev2,
733 OCProvisionResultCB resultCallback)
735 VERIFY_NON_NULL(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM);
736 if (SYMMETRIC_PAIR_WISE_KEY == type)
738 VERIFY_NON_NULL(TAG, pDev2, ERROR, OC_STACK_INVALID_PARAM);
742 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
743 return OC_STACK_INVALID_CALLBACK;
745 if (SYMMETRIC_PAIR_WISE_KEY == type &&
746 0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
748 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
749 return OC_STACK_INVALID_PARAM;
752 if (SYMMETRIC_PAIR_WISE_KEY == type &&
753 !(OWNER_PSK_LENGTH_128 == keySize || OWNER_PSK_LENGTH_256 == keySize))
755 OIC_LOG(INFO, TAG, "Invalid key size");
756 return OC_STACK_INVALID_PARAM;
759 OIC_LOG(INFO, TAG, "In SRPProvisionCredentials");
761 if (SYMMETRIC_PAIR_WISE_KEY == type)
763 bool linkExisits = true;
764 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExisits);
766 if (res != OC_STACK_OK)
768 OIC_LOG(ERROR, TAG, "Internal error occured");
773 OIC_LOG(ERROR, TAG, "Link already exists");
774 return OC_STACK_INVALID_PARAM;
778 OicUuid_t provTooldeviceID = {{0,}};
779 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
781 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
782 return OC_STACK_ERROR;
784 OIC_LOG(INFO, TAG, "retrieved deviceid");
787 case SYMMETRIC_PAIR_WISE_KEY:
789 const OCProvisionDev_t *firstDevice = pDev1;
790 const OCProvisionDev_t *secondDevice = pDev2;
792 OicSecCred_t *firstCred = NULL;
793 OicSecCred_t *secondCred = NULL;
794 OCStackResult res = PMGeneratePairWiseCredentials(type, keySize, &provTooldeviceID,
795 &firstDevice->doxm->deviceID, &secondDevice->doxm->deviceID,
796 &firstCred, &secondCred);
797 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
798 OIC_LOG(INFO, TAG, "Credentials generated successfully");
799 CredentialData_t *credData =
800 (CredentialData_t *) OICCalloc(1, sizeof(CredentialData_t));
801 if (NULL == credData)
805 OIC_LOG(ERROR, TAG, "Memory allocation problem");
806 return OC_STACK_NO_MEMORY;
808 credData->deviceInfo1 = firstDevice;
809 credData->deviceInfo2 = secondDevice;
810 credData->credInfo = secondCred;
812 credData->credInfoFirst = firstCred;
813 credData->numOfResults = 0;
814 credData->resultCallback = resultCallback;
815 // first call to provision creds to device1.
816 // second call to provision creds to device2.
819 (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
820 if (NULL == credData->resArr)
825 OIC_LOG(ERROR, TAG, "Memory allocation problem");
826 return OC_STACK_NO_MEMORY;
828 res = provisionCredentials(firstCred, firstDevice, credData, &provisionCredentialCB1);
829 if (OC_STACK_OK != res)
831 DeleteCredList(firstCred);
832 DeleteCredList(secondCred);
833 OICFree(credData->resArr);
836 OIC_LOG_V(INFO, TAG, "provisionCredentials returned: %d",res);
837 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
842 OIC_LOG(ERROR, TAG, "Invalid option.");
843 return OC_STACK_INVALID_PARAM;
846 return OC_STACK_ERROR;
850 * Internal Function to store results in result array during ACL provisioning.
852 static void registerResultForACLProvisioning(ACLData_t *aclData,
853 OCStackResult stackresult)
855 OIC_LOG_V(INFO, TAG, "Inside registerResultForACLProvisioning aclData->numOfResults is %d\n",
856 aclData->numOfResults);
857 memcpy(aclData->resArr[(aclData->numOfResults)].deviceId.id,
858 aclData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
859 aclData->resArr[(aclData->numOfResults)].res = stackresult;
860 ++(aclData->numOfResults);
864 * Callback handler of SRPProvisionACL.
866 * @param[in] ctx ctx value passed to callback from calling function.
867 * @param[in] UNUSED handle to an invocation
868 * @param[in] clientResponse Response from queries to remote servers.
869 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
870 * and OC_STACK_KEEP_TRANSACTION to keep it.
872 static OCStackApplicationResult SRPProvisionACLCB(void *ctx, OCDoHandle UNUSED,
873 OCClientResponse *clientResponse)
875 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionACLCB.");
877 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
878 ACLData_t *aclData = (ACLData_t*)ctx;
879 OCProvisionResultCB resultCallback = aclData->resultCallback;
883 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
885 registerResultForACLProvisioning(aclData, OC_STACK_RESOURCE_CHANGED);
886 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
889 OICFree(aclData->resArr);
891 return OC_STACK_DELETE_TRANSACTION;
894 registerResultForACLProvisioning(aclData, OC_STACK_ERROR);
895 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
898 OIC_LOG_V(ERROR, TAG, "SRPProvisionACLCB received Null clientResponse");
899 OICFree(aclData->resArr);
901 return OC_STACK_DELETE_TRANSACTION;
904 OCStackResult SRPProvisionACL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
905 OicSecAcl_t *acl, OCProvisionResultCB resultCallback)
907 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
908 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
909 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
911 // if rowneruuid is empty, set it to device ID
912 OicUuid_t emptyOwner = {.id = {0} };
913 if (memcmp(&(acl->rownerID.id), &emptyOwner, UUID_IDENTITY_SIZE) == 0)
915 OIC_LOG(DEBUG, TAG, "Set Rowner to PT's deviceId, because Rowner of ACL is empty");
918 if (OC_STACK_OK == GetDoxmDeviceID(&oicUuid))
920 memcpy(&(acl->rownerID.id), &oicUuid, UUID_IDENTITY_SIZE);
924 OIC_LOG(DEBUG, TAG, "Failed to set Rowner to PT's deviceID\
925 becuase it failed to retrieve Doxm DeviceID");
926 return OC_STACK_ERROR;
930 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
933 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
934 return OC_STACK_NO_MEMORY;
936 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
937 if(OC_STACK_OK != AclToCBORPayload(acl, &secPayload->securityData, &secPayload->payloadSize))
939 OCPayloadDestroy((OCPayload *)secPayload);
940 OIC_LOG(ERROR, TAG, "Failed to AclToCBORPayload");
941 return OC_STACK_NO_MEMORY;
943 OIC_LOG(DEBUG, TAG, "Created payload for ACL:");
944 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
946 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
947 if(!PMGenerateQuery(true,
948 selectedDeviceInfo->endpoint.addr,
949 selectedDeviceInfo->securePort,
950 selectedDeviceInfo->connType,
951 query, sizeof(query), OIC_RSRC_ACL_URI))
953 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
954 return OC_STACK_ERROR;
956 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
958 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
959 cbData.cb = &SRPProvisionACLCB;
960 ACLData_t *aclData = (ACLData_t *) OICCalloc(1, sizeof(ACLData_t));
963 OCPayloadDestroy((OCPayload *)secPayload);
964 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
965 return OC_STACK_NO_MEMORY;
967 aclData->deviceInfo = selectedDeviceInfo;
968 aclData->resultCallback = resultCallback;
969 aclData->numOfResults=0;
971 // call to provision ACL to device1.
973 aclData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
974 if (aclData->resArr == NULL)
977 OCPayloadDestroy((OCPayload *)secPayload);
978 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
979 return OC_STACK_NO_MEMORY;
981 cbData.context = (void *)aclData;
983 OCMethod method = OC_REST_POST;
984 OCDoHandle handle = NULL;
985 OIC_LOG(DEBUG, TAG, "Sending ACL info to resource server");
986 OCStackResult ret = OCDoResource(&handle, method, query,
987 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
988 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
989 if (ret != OC_STACK_OK)
991 OICFree(aclData->resArr);
994 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
998 OCStackResult SRPSaveACL(const OicSecAcl_t *acl)
1000 OIC_LOG(DEBUG, TAG, "IN SRPSaveACL");
1001 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
1003 OCStackResult res = InstallACL(acl);
1005 OIC_LOG(DEBUG, TAG, "OUT SRPSaveACL");
1010 * Internal Function to store results in result array during Direct-Pairing provisioning.
1012 static void registerResultForDirectPairingProvisioning(PconfData_t *pconfData,
1013 OCStackResult stackresult)
1015 OIC_LOG_V(INFO, TAG, "Inside registerResultForDirectPairingProvisioning "
1016 "pconfData->numOfResults is %d\n", pconfData->numOfResults);
1017 memcpy(pconfData->resArr[(pconfData->numOfResults)].deviceId.id,
1018 pconfData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
1019 pconfData->resArr[(pconfData->numOfResults)].res = stackresult;
1020 ++(pconfData->numOfResults);
1024 * Callback handler of SRPProvisionDirectPairing.
1026 * @param[in] ctx ctx value passed to callback from calling function.
1027 * @param[in] UNUSED handle to an invocation
1028 * @param[in] clientResponse Response from queries to remote servers.
1029 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1030 * and OC_STACK_KEEP_TRANSACTION to keep it.
1032 static OCStackApplicationResult SRPProvisionDirectPairingCB(void *ctx, OCDoHandle UNUSED,
1033 OCClientResponse *clientResponse)
1035 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionDirectPairingCB.");
1037 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
1038 PconfData_t *pconfData = (PconfData_t*)ctx;
1039 OCProvisionResultCB resultCallback = pconfData->resultCallback;
1043 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1045 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_OK);
1046 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
1049 OICFree(pconfData->resArr);
1051 return OC_STACK_DELETE_TRANSACTION;
1054 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_ERROR);
1055 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
1058 OIC_LOG_V(ERROR, TAG, "SRPProvisionDirectPairingCB received Null clientResponse");
1059 OICFree(pconfData->resArr);
1061 return OC_STACK_DELETE_TRANSACTION;
1064 OCStackResult SRPProvisionDirectPairing(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
1065 OicSecPconf_t *pconf, OCProvisionResultCB resultCallback)
1067 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
1068 VERIFY_NON_NULL(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
1069 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1071 // check direct-pairing capability
1072 if (true != selectedDeviceInfo->doxm->dpc)
1074 OIC_LOG(ERROR, TAG, "Resouce server does not have Direct-Pairing Capability ");
1075 return OC_STACK_UNAUTHORIZED_REQ;
1078 OicUuid_t provTooldeviceID = {.id={0}};
1079 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1081 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1082 return OC_STACK_ERROR;
1084 memcpy(&pconf->rownerID, &provTooldeviceID, sizeof(OicUuid_t));
1086 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1089 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1090 return OC_STACK_NO_MEMORY;
1092 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1094 if (OC_STACK_OK != PconfToCBORPayload(pconf, &(secPayload->securityData),
1095 &(secPayload->payloadSize)))
1097 OCPayloadDestroy((OCPayload*)secPayload);
1098 OIC_LOG(ERROR, TAG, "Failed to PconfToCborPayload");
1099 return OC_STACK_NO_MEMORY;
1101 OIC_LOG(DEBUG, TAG, "Created payload for pconf set");
1102 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1105 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1106 if(!PMGenerateQuery(true,
1107 selectedDeviceInfo->endpoint.addr,
1108 selectedDeviceInfo->securePort,
1109 selectedDeviceInfo->connType,
1110 query, sizeof(query), OIC_RSRC_PCONF_URI))
1112 OIC_LOG(ERROR, TAG, "SRPProvisionDirectPairing : Failed to generate query");
1113 return OC_STACK_ERROR;
1115 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1117 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1118 cbData.cb = &SRPProvisionDirectPairingCB;
1119 PconfData_t *pconfData = (PconfData_t *) OICCalloc(1, sizeof(PconfData_t));
1120 if (NULL == pconfData)
1122 OCPayloadDestroy((OCPayload*)secPayload);
1123 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1124 return OC_STACK_NO_MEMORY;
1126 pconfData->deviceInfo = selectedDeviceInfo;
1127 pconfData->resultCallback = resultCallback;
1128 pconfData->numOfResults=0;
1129 pconfData->ctx = ctx;
1130 // call to provision PCONF to device1.
1131 int noOfRiCalls = 1;
1132 pconfData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1133 if (NULL == pconfData->resArr)
1136 OCPayloadDestroy((OCPayload*)secPayload);
1137 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1138 return OC_STACK_NO_MEMORY;
1140 cbData.context = (void *)pconfData;
1142 OCMethod method = OC_REST_POST;
1143 OCDoHandle handle = NULL;
1144 OIC_LOG(DEBUG, TAG, "Sending PCONF info to resource server");
1145 OCStackResult ret = OCDoResource(&handle, method, query,
1146 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1147 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1148 if (OC_STACK_OK != ret)
1150 OICFree(pconfData->resArr);
1153 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1157 static void DeleteUnlinkData_t(UnlinkData_t *unlinkData)
1161 OICFree(unlinkData->unlinkDev);
1162 OICFree(unlinkData->unlinkRes);
1163 OICFree(unlinkData);
1167 static void registerResultForUnlinkDevices(UnlinkData_t *unlinkData, OCStackResult stackresult,
1170 if (NULL != unlinkData)
1172 OIC_LOG_V(INFO, TAG, "Inside registerResultForUnlinkDevices unlinkData->numOfResults is %d\n",
1173 unlinkData->numOfResults);
1174 OIC_LOG_V(INFO, TAG, "Stack result :: %d", stackresult);
1176 OicUuid_t *pUuid = &unlinkData->unlinkRes[(unlinkData->numOfResults)].deviceId;
1178 // Set result in the result array according to the position (devNum).
1179 if (idx != IDX_DB_UPDATE_RES)
1181 memcpy(pUuid->id, unlinkData->unlinkDev[idx].doxm->deviceID.id, sizeof(pUuid->id));
1184 { // When deivce ID is 000... this means it's the result of database update.
1185 memset(pUuid->id, 0, sizeof(pUuid->id));
1187 unlinkData->unlinkRes[(unlinkData->numOfResults)].res = stackresult;
1188 ++(unlinkData->numOfResults);
1189 OIC_LOG (INFO, TAG, "Out registerResultForUnlinkDevices");
1193 static OCStackResult SendDeleteCredentialRequest(void* ctx,
1194 OCClientResponseHandler respHandler,
1195 const OCProvisionDev_t* revokedDev,
1196 const OCProvisionDev_t* destDev)
1198 OIC_LOG(DEBUG, TAG, "IN SendDeleteCredentialRequest");
1200 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1202 return OC_STACK_INVALID_PARAM;
1206 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1207 if(OC_STACK_OK != ret)
1209 OIC_LOG(ERROR, TAG, "SendDeleteCredentialRequest : Failed to canonical UUID encoding");
1210 return OC_STACK_ERROR;
1213 char addressEncoded[CA_MAX_URI_LENGTH] = {0};
1214 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
1215 sizeof(addressEncoded),
1216 destDev->endpoint.addr);
1217 if (OC_STACK_OK != result)
1219 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : encoding error %d\n", result);
1220 return OC_STACK_ERROR;
1223 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1225 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1226 char *srpUri = SRP_FORM_DELETE_CREDENTIAL;
1228 if(CA_ADAPTER_TCP == destDev->endpoint.adapter)
1230 srpUri = SRP_FORM_DELETE_CREDENTIAL_TCP;
1234 snRet = snprintf(reqBuf, sizeof(reqBuf), srpUri, addressEncoded,
1235 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1239 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1240 return OC_STACK_ERROR;
1242 else if ((size_t)snRet >= sizeof(reqBuf))
1244 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1245 return OC_STACK_ERROR;
1248 OCCallbackData cbData;
1249 memset(&cbData, 0, sizeof(cbData));
1250 cbData.context = ctx;
1251 cbData.cb = respHandler;
1253 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1255 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1257 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1258 &destDev->endpoint, NULL,
1259 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1260 if (OC_STACK_OK != ret)
1262 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1264 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1269 static OCStackResult SendDeleteACLRequest(void* ctx,
1270 OCClientResponseHandler respHandler,
1271 const OCProvisionDev_t* revokedDev,
1272 const OCProvisionDev_t* destDev)
1274 OIC_LOG(DEBUG, TAG, "IN SendDeleteACLRequest");
1276 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1278 return OC_STACK_INVALID_PARAM;
1282 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1283 if(OC_STACK_OK != ret)
1285 OIC_LOG(ERROR, TAG, "SendDeleteACLRequest : Failed to canonical UUID encoding");
1286 return OC_STACK_ERROR;
1289 char addressEncoded[CA_MAX_URI_LENGTH] = {0};
1290 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
1291 sizeof(addressEncoded),
1292 destDev->endpoint.addr);
1293 if (OC_STACK_OK != result)
1295 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : encoding error %d\n", result);
1296 return OC_STACK_ERROR;
1300 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1302 //coaps://0.0.0.0:5684/oic/sec/acl?subjectuuid=(Canonical ENCODED UUID)
1303 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, addressEncoded,
1304 destDev->securePort, OIC_RSRC_ACL_URI, OIC_JSON_SUBJECTID_NAME, subID);
1308 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error (snprintf) %d\n", snRet);
1309 return OC_STACK_ERROR;
1311 else if ((size_t)snRet >= sizeof(reqBuf))
1313 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Truncated (snprintf) %d\n", snRet);
1314 return OC_STACK_ERROR;
1317 OCCallbackData cbData;
1318 memset(&cbData, 0, sizeof(cbData));
1319 cbData.context = ctx;
1320 cbData.cb = respHandler;
1322 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1324 OIC_LOG(DEBUG, TAG, "Sending remove ACL request to resource server");
1326 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1327 &destDev->endpoint, NULL,
1328 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1329 if (OC_STACK_OK != ret)
1331 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error in OCDoResource %d", ret);
1333 OIC_LOG(DEBUG, TAG, "OUT SendDeleteACLRequest");
1339 * Callback handler of unlink second device.
1341 * @param[in] ctx ctx value passed to callback from calling function.
1342 * @param[in] handle handle to an invocation
1343 * @param[in] clientResponse Response from queries to remote servers.
1344 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1345 * OC_STACK_KEEP_TRANSACTION to keep it.
1347 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1348 OCClientResponse *clientResponse)
1351 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1352 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1353 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1357 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1358 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1360 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1362 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1366 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1367 unlinkData->resultCallback(unlinkData->ctx,
1368 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1374 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1375 IDX_SECOND_DEVICE_RES);
1376 unlinkData->resultCallback(unlinkData->ctx,
1377 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1378 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1382 //Update provisioning DB when succes case.
1383 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1384 &unlinkData->unlinkDev[1].doxm->deviceID))
1386 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1387 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1388 unlinkData->resultCallback(unlinkData->ctx,
1389 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1392 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1396 DeleteUnlinkData_t(unlinkData);
1397 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1398 return OC_STACK_DELETE_TRANSACTION;
1403 * Callback handler of unlink first device.
1405 * @param[in] ctx ctx value passed to callback from calling function.
1406 * @param[in] handle handle to an invocation
1407 * @param[in] clientResponse Response from queries to remote servers.
1408 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1409 * OC_STACK_KEEP_TRANSACTION to keep it.
1411 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1412 OCClientResponse *clientResponse)
1414 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1415 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1416 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1421 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1422 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1424 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1426 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1428 // Second revocation request to second device.
1429 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1430 &unlinkData->unlinkDev[0],
1431 &unlinkData->unlinkDev[1] /*Dest*/);
1432 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1433 if (OC_STACK_OK != res)
1435 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1436 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1437 IDX_SECOND_DEVICE_RES);
1438 unlinkData->resultCallback(unlinkData->ctx,
1439 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1444 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1445 return OC_STACK_DELETE_TRANSACTION;
1450 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1452 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1453 unlinkData->unlinkRes, true);
1459 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1460 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1461 IDX_FIRST_DEVICE_RES );
1462 unlinkData->resultCallback(unlinkData->ctx,
1463 unlinkData->numOfResults, unlinkData->unlinkRes,
1465 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1469 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1470 DeleteUnlinkData_t(unlinkData);
1471 return OC_STACK_DELETE_TRANSACTION;
1475 * Function to unlink devices.
1476 * This function will remove the credential & relationship between the two devices.
1478 * @param[in] ctx Application context would be returned in result callback
1479 * @param[in] pTargetDev1 first device information to be unlinked.
1480 * @param[in] pTargetDev2 second device information to be unlinked.
1481 * @param[in] resultCallback callback provided by API user, callback will be called when
1482 * device unlink is finished.
1483 * @return OC_STACK_OK in case of success and other value otherwise.
1485 OCStackResult SRPUnlinkDevices(void* ctx,
1486 const OCProvisionDev_t* pTargetDev1,
1487 const OCProvisionDev_t* pTargetDev2,
1488 OCProvisionResultCB resultCallback)
1490 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1492 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1494 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1495 return OC_STACK_INVALID_PARAM;
1497 if (!resultCallback)
1499 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1500 return OC_STACK_INVALID_CALLBACK;
1502 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1504 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1505 return OC_STACK_INVALID_PARAM;
1508 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1509 PMPrintOCProvisionDev(pTargetDev1);
1510 PMPrintOCProvisionDev(pTargetDev2);
1512 // Mark the link status stale
1513 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1514 if (OC_STACK_OK != res)
1516 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1520 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1521 VERIFY_NON_NULL(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1523 //Initialize unlink data
1524 unlinkData->ctx = ctx;
1525 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1526 if (NULL == unlinkData->unlinkDev)
1528 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1529 res = OC_STACK_NO_MEMORY;
1533 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1534 if (NULL == unlinkData->unlinkRes)
1536 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1537 res = OC_STACK_NO_MEMORY;
1541 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1542 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1544 unlinkData->numOfResults = 0;
1545 unlinkData->resultCallback = resultCallback;
1547 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1548 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1549 if (OC_STACK_OK != res)
1551 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1558 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1559 DeleteUnlinkData_t(unlinkData);
1563 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1567 OICFree(pRemoveData->revokeTargetDev);
1568 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1569 OICFree(pRemoveData->removeRes);
1570 OICFree(pRemoveData);
1574 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1575 OCStackResult stackresult, bool hasError)
1577 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1578 removeData->numOfResults + 1);
1581 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1582 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1586 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1587 0, sizeof(pLinkedDevId->id) );
1589 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1590 removeData->hasError = hasError;
1591 ++(removeData->numOfResults);
1593 // If we get suffcient result from linked devices, we have to call user callback and do free
1594 if (removeData->sizeOfResArray == removeData->numOfResults)
1596 if(!removeData->hasError)
1598 // Remove device info from prvisioning database
1599 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1601 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1602 removeData->hasError = true;
1605 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1606 removeData->hasError);
1607 DeleteRemoveData_t(removeData);
1611 static void registerResultForResetDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1612 OCStackResult stackresult, bool hasError)
1614 OIC_LOG_V(INFO, TAG, "Inside registerResultForResetDevice removeData->numOfResults is %zu\n",
1615 removeData->numOfResults + 1);
1618 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1619 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1623 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1624 0, sizeof(pLinkedDevId->id) );
1626 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1627 removeData->hasError = hasError;
1628 ++(removeData->numOfResults);
1630 // If we get suffcient result from linked devices, we have to call user callback and do free
1631 if (removeData->sizeOfResArray == removeData->numOfResults)
1633 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1634 removeData->hasError);
1635 DeleteRemoveData_t(removeData);
1640 * Callback handler of unlink first device.
1642 * @param[in] ctx ctx value passed to callback from calling function.
1643 * @param[in] handle handle to an invocation
1644 * @param[in] clientResponse Response from queries to remote servers.
1645 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1646 * and OC_STACK_KEEP_TRANSACTION to keep it.
1648 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1649 OCClientResponse *clientResponse)
1651 //Update the delete credential into delete device context
1652 //Save the deleted status in delDevCtx
1654 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1655 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1656 OCStackResult res = OC_STACK_ERROR;
1658 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1662 OicUuid_t revDevUuid = {.id={0}};
1663 if(UUID_LENGTH == clientResponse->identity.id_length)
1665 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1666 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1668 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1669 if (OC_STACK_OK != res)
1671 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1672 registerResultForRemoveDevice(removeData, &revDevUuid,
1673 OC_STACK_INCONSISTENT_DB, true);
1675 return OC_STACK_DELETE_TRANSACTION;
1678 registerResultForRemoveDevice(removeData, &revDevUuid,
1679 OC_STACK_RESOURCE_DELETED, false);
1683 registerResultForRemoveDevice(removeData, &revDevUuid,
1684 clientResponse->result, true);
1685 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1690 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1691 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1693 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1696 * Since server's credential was deleted,
1697 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1699 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1700 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1704 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1710 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1711 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1715 return OC_STACK_DELETE_TRANSACTION;
1719 * Callback handler of reset device.
1721 * @param[in] ctx ctx value passed to callback from calling function.
1722 * @param[in] handle handle to an invocation
1723 * @param[in] clientResponse Response from queries to remote servers.
1724 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1725 * and OC_STACK_KEEP_TRANSACTION to keep it.
1727 static OCStackApplicationResult SRPSyncDeviceCredCB(void *delDevCtx, OCDoHandle handle,
1728 OCClientResponse *clientResponse)
1730 //Update the delete credential into delete device context
1731 //Save the deleted status in delDevCtx
1733 OIC_LOG_V(INFO, TAG, "Inside SRPSyncDeviceCredCB.");
1734 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1735 OCStackResult res = OC_STACK_ERROR;
1737 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1738 OCProvisionDev_t * pTargetDev = PMCloneOCProvisionDev(removeData->revokeTargetDev);
1739 OCProvisionResultCB resultCallback = removeData->resultCallback;
1742 OicUuid_t revDevUuid = {.id={0}};
1743 if(UUID_LENGTH == clientResponse->identity.id_length)
1745 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1746 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1748 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1749 if (OC_STACK_OK != res)
1751 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1752 registerResultForResetDevice(removeData, &revDevUuid,
1753 OC_STACK_INCONSISTENT_DB, true);
1755 return OC_STACK_DELETE_TRANSACTION;
1758 registerResultForResetDevice(removeData, &revDevUuid,
1759 OC_STACK_RESOURCE_DELETED, false);
1763 registerResultForResetDevice(removeData, &revDevUuid,
1764 clientResponse->result, false);
1765 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1770 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1771 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1773 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1776 * Since server's credential was deleted,
1777 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1779 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1780 registerResultForResetDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1784 registerResultForResetDevice(removeData, NULL, clientResponse->result, true);
1790 registerResultForResetDevice(removeData, NULL, OC_STACK_ERROR, true);
1791 OIC_LOG(ERROR, TAG, "SRPSyncDevice received Null clientResponse");
1794 SRPResetDevice(pTargetDev, resultCallback);
1796 return OC_STACK_DELETE_TRANSACTION;
1800 * Callback handler of reset device sync-up
1802 * @param[in] ctx ctx value passed to callback from calling function.
1803 * @param[in] handle handle to an invocation
1804 * @param[in] clientResponse Response from queries to remote servers.
1805 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1806 * and OC_STACK_KEEP_TRANSACTION to keep it.
1808 static OCStackApplicationResult SRPSyncDeviceACLCB(void *ctx, OCDoHandle handle,
1809 OCClientResponse *clientResponse)
1811 return OC_STACK_DELETE_TRANSACTION;
1815 * Callback handler of device remote reset.
1817 * @param[in] ctx ctx value passed to callback from calling function.
1818 * @param[in] handle handle to an invocation
1819 * @param[in] clientResponse Response from queries to remote servers.
1820 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1821 * and OC_STACK_KEEP_TRANSACTION to keep it.
1823 static OCStackApplicationResult SRPResetDeviceCB(void *ctx, OCDoHandle handle,
1824 OCClientResponse *clientResponse)
1826 OIC_LOG(DEBUG, TAG, "IN SRPResetDeviceCB");
1827 if(OC_STACK_OK == clientResponse->result)
1829 OIC_LOG(DEBUG, TAG, "Change Target Device Pstat Cm SUCCEEDED");
1832 // Delete Cred and ACL related to the target device.
1833 const OicSecCred_t *cred = NULL;
1834 OCProvisionDev_t * pTargetDev = (OCProvisionDev_t *)ctx;
1835 cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
1838 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to get credential of target device.");
1842 OCStackResult res = RemoveCredential(&cred->subject);
1843 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1845 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove credential.");
1849 res = RemoveACE(&cred->subject, NULL);
1850 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1852 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove ACL.");
1855 if (OC_STACK_OK != PDMDeleteDevice(&pTargetDev->doxm->deviceID))
1857 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to delete device from PDM");
1860 //Close the DTLS session of the reset device.
1861 CAEndpoint_t* endpoint = (CAEndpoint_t *)&clientResponse->devAddr;
1862 CAResult_t caResult = CAcloseSslSession(endpoint);
1863 if(CA_STATUS_OK != caResult)
1865 OIC_LOG_V(WARNING, TAG, "OCResetDevice : Failed to close DTLS session : %d", caResult);
1869 * If there is no linked device, PM does not send any request.
1870 * So we should directly invoke the result callback to inform the result of OCResetDevice.
1872 if(OC_STACK_NO_RESOURCE == res)
1878 OICFree(pTargetDev);
1879 return OC_STACK_DELETE_TRANSACTION;
1883 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1884 OCProvisionDev_t* pOwnedDevList,
1885 OCUuidList_t* pLinkedUuidList,
1886 OCProvisionDev_t** ppLinkedDevList,
1887 size_t *numOfLinkedDev)
1889 // pOwnedDevList could be NULL. It means no alived and owned device now.
1890 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1891 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1893 return OC_STACK_INVALID_PARAM;
1897 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1898 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1900 // Mark the link status stale.
1901 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1902 if (OC_STACK_OK != res)
1904 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1905 return OC_STACK_INCONSISTENT_DB;
1910 // If this linked device is alive (power-on), add the deivce to the list.
1911 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1912 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1914 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1916 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1917 if (NULL == targetDev)
1919 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1920 return OC_STACK_NO_MEMORY;
1923 LL_PREPEND(*ppLinkedDevList, targetDev);
1930 *numOfLinkedDev = cnt;
1935 * Function to device revocation
1936 * This function will remove credential of target device from all devices in subnet.
1938 * @param[in] ctx Application context would be returned in result callback
1939 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1940 * @param[in] pTargetDev Device information to be revoked.
1941 * @param[in] resultCallback callback provided by API user, callback will be called when
1942 * credential revocation is finished.
1943 * @return OC_STACK_OK in case of success and other value otherwise.
1944 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1945 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1947 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1948 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1950 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1952 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1954 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1955 return OC_STACK_INVALID_PARAM;
1957 if (!resultCallback)
1959 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
1960 return OC_STACK_INVALID_CALLBACK;
1963 // Declare variables in here to handle error cases with goto statement.
1964 OCProvisionDev_t* pOwnedDevList = NULL;
1965 OCProvisionDev_t* pLinkedDevList = NULL;
1966 RemoveData_t* removeData = NULL;
1968 //1. Find all devices that has a credential of the revoked device
1969 OCUuidList_t* pLinkedUuidList = NULL;
1970 size_t numOfDevices = 0;
1971 OCStackResult res = OC_STACK_ERROR;
1972 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
1973 if (OC_STACK_OK != res)
1975 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
1978 // if there is no related device, we can skip further process.
1979 if (0 == numOfDevices)
1981 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
1982 res = OC_STACK_CONTINUE;
1986 //2. Find owned device from the network
1987 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
1988 if (OC_STACK_OK != res)
1990 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
1994 //3. Make a list of devices to send DELETE credential request
1995 // by comparing owned devices from provisioning database with mutlicast discovery result.
1996 size_t numOfLinkedDev = 0;
1997 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
1998 &pLinkedDevList, &numOfLinkedDev);
1999 if (OC_STACK_OK != res)
2001 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
2004 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2005 { // So we don't have to send request message.
2006 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
2007 res = OC_STACK_CONTINUE;
2011 // 4. Prepare RemoveData Context data.
2012 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2015 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
2016 res = OC_STACK_NO_MEMORY;
2020 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2021 if (!removeData->revokeTargetDev)
2023 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
2024 res = OC_STACK_NO_MEMORY;
2028 removeData->removeRes =
2029 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2030 if (!removeData->removeRes)
2032 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
2033 res = OC_STACK_NO_MEMORY;
2037 removeData->ctx = ctx;
2038 removeData->linkedDevList = pLinkedDevList;
2039 removeData->resultCallback = resultCallback;
2040 removeData->numOfResults = 0;
2041 removeData->sizeOfResArray = numOfLinkedDev;
2042 removeData->hasError = false;
2044 // 5. Send DELETE credential request to linked devices.
2045 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2046 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2047 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2049 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2050 removeData->revokeTargetDev, curDev);
2051 if (OC_STACK_OK != res)
2053 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
2054 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2058 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2062 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2063 PMDeleteDeviceList(pOwnedDevList);
2064 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
2066 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2069 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2070 PMDeleteDeviceList(pOwnedDevList);
2071 PMDeleteDeviceList(pLinkedDevList);
2074 OICFree(removeData->revokeTargetDev);
2075 OICFree(removeData->removeRes);
2076 OICFree(removeData);
2078 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");
2083 * Function to device revocation
2084 * This function will remove credential of target device from all devices in subnet.
2086 * @param[in] ctx Application context would be returned in result callback
2087 * @param[in] pOwnedDevList List of owned devices
2088 * @param[in] pTargetDev Device information to be revoked.
2089 * @param[in] resultCallback callback provided by API user, callback will be called when
2090 * credential revocation is finished.
2091 * @return OC_STACK_OK in case of success and other value otherwise.
2092 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2093 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2095 OCStackResult SRPRemoveDeviceWithoutDiscovery(void* ctx, const OCProvisionDev_t* pOwnedDevList,
2096 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2098 OIC_LOG(INFO, TAG, "IN SRPRemoveDeviceWithoutDiscovery");
2102 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : Owned Device List is empty");
2103 return OC_STACK_CONTINUE;
2107 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL parameters");
2108 return OC_STACK_INVALID_PARAM;
2110 if (!resultCallback)
2112 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL Callback");
2113 return OC_STACK_INVALID_CALLBACK;
2116 // Declare variables in here to handle error cases with goto statement.
2117 OCProvisionDev_t* pLinkedDevList = NULL;
2118 RemoveData_t* removeData = NULL;
2120 //1. Find all devices that has a credential of the revoked device
2121 OCUuidList_t* pLinkedUuidList = NULL;
2122 size_t numOfDevices = 0;
2123 OCStackResult res = OC_STACK_ERROR;
2124 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2125 if (OC_STACK_OK != res)
2127 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to get linked devices information");
2128 return OC_STACK_CONTINUE;
2130 // if there is no related device, we can skip further process.
2131 if (0 == numOfDevices)
2133 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : No linked device found.");
2134 return OC_STACK_CONTINUE;
2137 //2. Make a list of devices to send DELETE credential request
2138 // by comparing owned devices from provisioning database with mutlicast discovery result.
2139 size_t numOfLinkedDev = 0;
2140 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2141 &pLinkedDevList, &numOfLinkedDev);
2142 if (OC_STACK_OK != res)
2144 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : GetListofDevToReqDeleteCred() failed");
2147 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2148 { // So we don't have to send request message.
2149 OIC_LOG(DEBUG, TAG, "SRPRemoveDeviceWithoutDiscovery : No alived & linked device found.");
2150 return OC_STACK_CONTINUE;
2153 // 3. Prepare RemoveData Context data.
2154 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2157 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2158 res = OC_STACK_NO_MEMORY;
2162 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2163 if (!removeData->revokeTargetDev)
2165 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : PMCloneOCProvisionDev Failed");
2166 res = OC_STACK_NO_MEMORY;
2170 removeData->removeRes =
2171 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2172 if (!removeData->removeRes)
2174 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2175 res = OC_STACK_NO_MEMORY;
2179 removeData->ctx = ctx;
2180 removeData->linkedDevList = pLinkedDevList;
2181 removeData->resultCallback = resultCallback;
2182 removeData->numOfResults = 0;
2183 removeData->sizeOfResArray = numOfLinkedDev;
2184 removeData->hasError = false;
2186 // 5. Send DELETE credential request to linked devices.
2187 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2188 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2189 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2191 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2192 removeData->revokeTargetDev, curDev);
2193 if (OC_STACK_OK != res)
2195 OIC_LOG_V(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Fail to send the DELETE credential request to\
2196 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2200 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2204 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2205 OIC_LOG(INFO, TAG, "OUT SRPRemoveDeviceWithoutDiscovery");
2207 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2210 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2211 PMDeleteDeviceList(pLinkedDevList);
2214 OICFree(removeData->revokeTargetDev);
2215 OICFree(removeData->removeRes);
2216 OICFree(removeData);
2218 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDeviceWithoutDiscovery");
2223 * Function to sync-up credential and ACL of the target device.
2224 * This function will remove credential and ACL of target device from all devices in subnet.
2226 * @param[in] ctx Application context would be returned in result callback
2227 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
2228 * @param[in] pTargetDev Device information to be revoked.
2229 * @param[in] resultCallback callback provided by API user, callback will be called when
2230 * credential revocation is finished.
2231 * when there is an error, this user callback is called immediately.
2232 * @return OC_STACK_OK in case of success and other value otherwise.
2233 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2234 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2236 OCStackResult SRPSyncDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
2237 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2239 OIC_LOG(INFO, TAG, "IN SRPSyncDevice");
2240 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
2242 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL parameters");
2243 return OC_STACK_INVALID_PARAM;
2245 if (!resultCallback)
2247 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL Callback");
2248 return OC_STACK_INVALID_CALLBACK;
2251 // Declare variables in here to handle error cases with goto statement.
2252 OCProvisionDev_t* pOwnedDevList = NULL;
2253 OCProvisionDev_t* pLinkedDevList = NULL;
2254 RemoveData_t* removeData = NULL;
2256 //1. Find all devices that has a credential of the revoked device
2257 OCUuidList_t* pLinkedUuidList = NULL;
2258 size_t numOfDevices = 0;
2259 OCStackResult res = OC_STACK_ERROR;
2260 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2261 if (OC_STACK_OK != res)
2263 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to get linked devices information");
2266 // if there is no related device, we can skip further process.
2267 if (0 == numOfDevices)
2269 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No linked device found.");
2270 res = OC_STACK_CONTINUE;
2274 //2. Find owned device from the network
2275 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
2276 if (OC_STACK_OK != res)
2278 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to PMDeviceDiscovery");
2282 //3. Make a list of devices to send DELETE credential request
2283 // by comparing owned devices from provisioning database with mutlicast discovery result.
2284 size_t numOfLinkedDev = 0;
2285 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2286 &pLinkedDevList, &numOfLinkedDev);
2287 if (OC_STACK_OK != res)
2289 OIC_LOG(ERROR, TAG, "SRPSyncDevice : GetListofDevToReqDeleteCred() failed");
2292 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2293 { // So we don't have to send request message.
2294 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No alived & linked device found.");
2295 res = OC_STACK_CONTINUE;
2299 // 4. Prepare RemoveData Context data.
2300 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2303 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to memory allocation");
2304 res = OC_STACK_NO_MEMORY;
2308 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2309 if (!removeData->revokeTargetDev)
2311 OIC_LOG(ERROR, TAG, "SRPSyncDevice : PMCloneOCProvisionDev Failed");
2312 res = OC_STACK_NO_MEMORY;
2316 removeData->removeRes =
2317 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2318 if (!removeData->removeRes)
2320 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to allocate memory");
2321 res = OC_STACK_NO_MEMORY;
2325 removeData->ctx = ctx;
2326 removeData->linkedDevList = pLinkedDevList;
2327 removeData->resultCallback = resultCallback;
2328 removeData->numOfResults = 0;
2329 removeData->sizeOfResArray = numOfLinkedDev;
2330 removeData->hasError = false;
2332 // 5. Send DELETE credential request to linked devices.
2333 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2334 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2335 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2337 res = SendDeleteACLRequest((void*)removeData, &SRPSyncDeviceACLCB,
2338 removeData->revokeTargetDev, curDev);
2339 if (OC_STACK_OK != res)
2341 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE ACL request to\
2342 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2345 res = SendDeleteCredentialRequest((void*)removeData, &SRPSyncDeviceCredCB,
2346 removeData->revokeTargetDev, curDev);
2347 if (OC_STACK_OK != res)
2349 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE credential request to\
2350 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2351 totalRes = OC_STACK_ERROR;
2355 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2359 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2360 PMDeleteDeviceList(pOwnedDevList);
2361 OIC_LOG(INFO, TAG, "OUT SRPSyncDevice");
2363 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2366 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2367 PMDeleteDeviceList(pOwnedDevList);
2368 PMDeleteDeviceList(pLinkedDevList);
2371 OICFree(removeData->revokeTargetDev);
2372 OICFree(removeData->removeRes);
2373 OICFree(removeData);
2375 OIC_LOG(INFO, TAG, "OUT ERROR case SRPSyncDevice");
2380 * Function for remote reset
2381 * This function will send pstat PUT message to the target device to initiate remote reset.
2383 * @param[in] pTargetDev Device information to be revoked.
2384 * @param[in] resultCallback callback provided by API user, callback will be called when
2385 * credential revocation is finished.
2386 * when there is an error, this user callback is called immediately.
2387 * @return OC_STACK_OK in case of success and other value otherwise.
2388 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2389 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2391 OCStackResult SRPResetDevice(const OCProvisionDev_t* pTargetDev,
2392 OCProvisionResultCB resultCallback)
2394 OIC_LOG(INFO, TAG, "IN SRPResetDevice");
2397 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL parameters");
2398 return OC_STACK_INVALID_PARAM;
2400 if (!resultCallback)
2402 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL Callback");
2403 return OC_STACK_INVALID_CALLBACK;
2406 OCStackResult res = OC_STACK_ERROR;
2407 OicSecPstat_t * pstat = (OicSecPstat_t *) OICCalloc(1, sizeof(OicSecPstat_t));
2410 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2411 return OC_STACK_NO_MEMORY;
2415 pstat->isOp = false;
2416 memcpy(pstat->deviceID.id, pTargetDev->doxm->deviceID.id, sizeof(OicUuid_t));
2417 pstat->tm = TAKE_OWNER;
2418 pstat->om = (OicSecDpom_t)(SINGLE_SERVICE_CLIENT_DRIVEN); // the only mode IoTivity supports currently
2420 pstat->sm = (OicSecDpom_t *) OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
2421 if (NULL == pstat->sm)
2423 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2425 return OC_STACK_NO_MEMORY;
2427 pstat->sm[0] = (OicSecDpom_t)(SINGLE_SERVICE_CLIENT_DRIVEN); // the only mode IoTivity supports currently
2429 OCSecurityPayload * secPayload = (OCSecurityPayload *) OICCalloc(1, sizeof(OCSecurityPayload));
2432 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2433 res = OC_STACK_NO_MEMORY;
2436 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2438 if (OC_STACK_OK != PstatToCBORPayload(pstat, &(secPayload->securityData),
2439 &(secPayload->payloadSize), true))
2441 OCPayloadDestroy((OCPayload *) secPayload);
2442 OIC_LOG(ERROR, TAG, "Failed to PstatToCBORPayload");
2443 res = OC_STACK_NO_MEMORY;
2446 OIC_LOG(DEBUG, TAG, "Created payload for pstat set");
2447 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2449 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2450 if (!PMGenerateQuery(true,
2451 pTargetDev->endpoint.addr,
2452 pTargetDev->securePort,
2453 pTargetDev->connType,
2454 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2456 OIC_LOG(ERROR, TAG, "SRPResetDevice : Failed to generate query");
2457 OCPayloadDestroy((OCPayload *) secPayload);
2458 res = OC_STACK_ERROR;
2461 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2463 OCCallbackData cbData = { .context = NULL, .cb = NULL, .cd = NULL };
2464 OCMethod method = OC_REST_POST;
2465 OCDoHandle handle = NULL;
2466 OCProvisionDev_t * targetDev = PMCloneOCProvisionDev(pTargetDev);
2467 if (NULL == targetDev)
2469 OIC_LOG(ERROR, TAG, "target dev is null");
2470 res = OC_STACK_ERROR;
2473 cbData.cb = &SRPResetDeviceCB;
2474 cbData.context = (void *) targetDev;
2476 OIC_LOG(DEBUG, TAG, "Sending PSTAT info to resource server");
2477 res = OCDoResource(&handle, method, query,
2478 &targetDev->endpoint, (OCPayload *)secPayload,
2479 targetDev->connType, OC_LOW_QOS, &cbData, NULL, 0);\
2480 if (OC_STACK_OK != res)
2482 OIC_LOG(ERROR, TAG, "OCStack resource error");
2488 OIC_LOG(INFO, TAG, "OUT SRPResetDevice");
2493 * Internal Function to store results in result array during GetCredResourceCB.
2495 static void registerResultForGetCredResourceCB(GetSecData_t *GetSecData,
2496 OCStackResult stackresult)
2498 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetCredResourceCB "
2499 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2500 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2501 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2502 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2503 ++(GetSecData->numOfResults);
2507 * Callback handler of SRPGetCredResource.
2509 * @param[in] ctx ctx value passed to callback from calling function.
2510 * @param[in] UNUSED handle to an invocation
2511 * @param[in] clientResponse Response from queries to remote servers.
2512 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2513 * and OC_STACK_KEEP_TRANSACTION to keep it.
2515 static OCStackApplicationResult SRPGetCredResourceCB(void *ctx, OCDoHandle UNUSED,
2516 OCClientResponse *clientResponse)
2518 OIC_LOG_V(INFO, TAG, "Inside SRPGetCredResourceCB.");
2520 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2521 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2522 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2526 if(OC_STACK_OK == clientResponse->result)
2528 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2529 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2531 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2533 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2534 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2537 OICFree(GetSecData->resArr);
2538 OICFree(GetSecData);
2540 return OC_STACK_DELETE_TRANSACTION;
2543 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2544 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2547 OIC_LOG_V(ERROR, TAG, "SRPGetCredResourceCB received Null clientResponse");
2548 OICFree(GetSecData->resArr);
2549 OICFree(GetSecData);
2551 return OC_STACK_DELETE_TRANSACTION;
2555 OCStackResult SRPGetCredResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2556 OCProvisionResultCB resultCallback)
2558 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2559 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2561 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2562 if(!PMGenerateQuery(true,
2563 selectedDeviceInfo->endpoint.addr,
2564 selectedDeviceInfo->securePort,
2565 selectedDeviceInfo->connType,
2566 query, sizeof(query), OIC_RSRC_CRED_URI))
2568 OIC_LOG(ERROR, TAG, "SRPGetCredResource : Failed to generate query");
2569 return OC_STACK_ERROR;
2571 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2573 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2574 cbData.cb = &SRPGetCredResourceCB;
2575 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2576 if (NULL == GetSecData)
2578 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2579 return OC_STACK_NO_MEMORY;
2581 GetSecData->deviceInfo = selectedDeviceInfo;
2582 GetSecData->resultCallback = resultCallback;
2583 GetSecData->numOfResults=0;
2584 GetSecData->ctx = ctx;
2586 int noOfRiCalls = 1;
2587 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2588 if (NULL == GetSecData->resArr)
2590 OICFree(GetSecData);
2591 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2592 return OC_STACK_NO_MEMORY;
2594 cbData.context = (void *)GetSecData;
2596 OCMethod method = OC_REST_GET;
2597 OCDoHandle handle = NULL;
2598 OIC_LOG(DEBUG, TAG, "Sending Get Cred to resource server");
2599 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2600 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2601 if (OC_STACK_OK != ret)
2603 OIC_LOG(ERROR, TAG, "OCStack resource error");
2604 OICFree(GetSecData->resArr);
2605 OICFree(GetSecData);
2607 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2608 OIC_LOG(DEBUG, TAG, "OUT SRPGetCredResource");
2614 * Internal Function to store results in result array during GetACLResourceCB.
2616 static void registerResultForGetACLResourceCB(GetSecData_t *GetSecData,
2617 OCStackResult stackresult)
2619 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetACLResourceCB "
2620 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2621 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2622 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2623 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2624 ++(GetSecData->numOfResults);
2628 * Callback handler of SRPGetACLResource.
2630 * @param[in] ctx ctx value passed to callback from calling function.
2631 * @param[in] UNUSED handle to an invocation
2632 * @param[in] clientResponse Response from queries to remote servers.
2633 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2634 * and OC_STACK_KEEP_TRANSACTION to keep it.
2636 static OCStackApplicationResult SRPGetACLResourceCB(void *ctx, OCDoHandle UNUSED,
2637 OCClientResponse *clientResponse)
2639 OIC_LOG_V(INFO, TAG, "Inside SRPGetACLResourceCB.");
2641 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2642 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2643 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2647 if(OC_STACK_OK == clientResponse->result)
2649 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2650 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2652 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2654 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2655 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2658 OICFree(GetSecData->resArr);
2659 OICFree(GetSecData);
2661 return OC_STACK_DELETE_TRANSACTION;
2664 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2665 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2668 OIC_LOG_V(ERROR, TAG, "SRPGetACLResourceCB received Null clientResponse");
2669 OICFree(GetSecData->resArr);
2670 OICFree(GetSecData);
2672 return OC_STACK_DELETE_TRANSACTION;
2676 OCStackResult SRPGetACLResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2677 OCProvisionResultCB resultCallback)
2679 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2680 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2682 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2683 if(!PMGenerateQuery(true,
2684 selectedDeviceInfo->endpoint.addr,
2685 selectedDeviceInfo->securePort,
2686 selectedDeviceInfo->connType,
2687 query, sizeof(query), OIC_RSRC_ACL_URI))
2689 OIC_LOG(ERROR, TAG, "SRPGetACLResource : Failed to generate query");
2690 return OC_STACK_ERROR;
2692 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2694 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2695 cbData.cb = &SRPGetACLResourceCB;
2696 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2697 if (NULL == GetSecData)
2699 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2700 return OC_STACK_NO_MEMORY;
2702 GetSecData->deviceInfo = selectedDeviceInfo;
2703 GetSecData->resultCallback = resultCallback;
2704 GetSecData->numOfResults=0;
2705 GetSecData->ctx = ctx;
2707 int noOfRiCalls = 1;
2708 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2709 if (NULL == GetSecData->resArr)
2711 OICFree(GetSecData);
2712 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2713 return OC_STACK_NO_MEMORY;
2715 cbData.context = (void *)GetSecData;
2717 OCMethod method = OC_REST_GET;
2718 OCDoHandle handle = NULL;
2719 OIC_LOG(DEBUG, TAG, "Sending Get ACL to resource server");
2720 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2721 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2722 if (OC_STACK_OK != ret)
2724 OIC_LOG(ERROR, TAG, "OCStack resource error");
2725 OICFree(GetSecData->resArr);
2726 OICFree(GetSecData);
2728 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2729 OIC_LOG(DEBUG, TAG, "OUT SRPGetACLResource");
2734 OCStackResult SRPReadTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
2737 OIC_LOG(DEBUG, TAG, "IN SRPReadTrustCertChain");
2739 OCStackResult res = OC_STACK_ERROR;
2741 OicSecCred_t* credData = GetCredEntryByCredId(credId);
2744 res = CredToCBORPayload((const OicSecCred_t*) credData, trustCertChain,
2745 chainSize, secureFlag);
2746 if(OC_STACK_OK != res)
2748 OIC_LOG(INFO, TAG, "CredToCBORPayload failed");
2751 DeleteCredList(credData);