1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * *****************************************************************/
20 #include "iotivity_config.h"
28 #include "ocprovisioningmanager.h"
29 #include "secureresourceprovider.h"
31 #include "oic_malloc.h"
32 #include "aclresource.h"
33 #include "pstatresource.h"
34 #include "srmresourcestrings.h"
35 #include "credresource.h"
36 #include "doxmresource.h"
37 #include "pconfresource.h"
38 #include "credentialgenerator.h"
39 #include "cainterface.h"
42 #include "pmutility.h"
43 #include "provisioningdatabasemanager.h"
46 #include "ocpayload.h"
49 #include "crlresource.h"
52 #define TAG "OIC_SRPAPI"
55 * Macro to verify argument is not equal to NULL.
56 * eg: VERIFY_NON_NULL(TAG, ptrData, ERROR,OC_STACK_ERROR);
58 #define VERIFY_NON_NULL(tag, arg, logLevel, retValue) { if (NULL == (arg)) \
59 { OIC_LOG((logLevel), tag, #arg " is NULL"); return retValue; } }
62 * Macro to verify success of operation.
63 * eg: VERIFY_SUCCESS(TAG, OC_STACK_OK == foo(), ERROR, OC_STACK_ERROR);
65 #define VERIFY_SUCCESS(tag, op, logLevel, retValue) { if (!(op)) \
66 {OIC_LOG((logLevel), tag, #op " failed!!"); return retValue;} }
69 trustCertChainContext_t g_trustCertChainNotifier;
72 * Structure to carry credential data to callback.
74 typedef struct CredentialData CredentialData_t;
77 void *ctx; /**< Pointer to user context.**/
78 const OCProvisionDev_t *deviceInfo1; /**< Pointer to OCProvisionDev_t.**/
79 const OCProvisionDev_t *deviceInfo2; /**< Pointer to OCProvisionDev_t.**/
80 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
81 OicSecCred_t *credInfoFirst; /**< Pointer to OicSecCred_t.**/
82 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
83 OCProvisionResult_t *resArr; /**< Result array.**/
84 int numOfResults; /**< Number of results in result array.**/
88 * Structure to carry ACL provision API data to callback.
90 typedef struct ACLData ACLData_t;
93 void *ctx; /**< Pointer to user context.**/
94 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
95 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
96 OCProvisionResult_t *resArr; /**< Result array.**/
97 int numOfResults; /**< Number of results in result array.**/
100 // Structure to carry get security resource APIs data to callback.
101 typedef struct GetSecData GetSecData_t;
104 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
105 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
106 OCProvisionResult_t *resArr; /**< Result array.**/
107 int numOfResults; /**< Number of results in result array.**/
111 * Structure to carry PCONF provision API data to callback.
113 typedef struct PconfData PconfData_t;
116 void *ctx; /**< Pointer to user context.**/
117 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
118 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
119 OCProvisionResult_t *resArr; /**< Result array.**/
120 int numOfResults; /**< Number of results in result array.**/
123 // Enum type index for unlink callback.
125 IDX_FIRST_DEVICE_RES = 0, // index for resulf of the first device
126 IDX_SECOND_DEVICE_RES, // index for result of the second device
127 IDX_DB_UPDATE_RES // index for result of updating provisioning database.
130 // Structure to carry unlink APIs data to callback.
131 typedef struct UnlinkData UnlinkData_t;
134 OCProvisionDev_t* unlinkDev; /**< Pointer to OCProvisionDev_t to be unlinked.**/
135 OCProvisionResult_t* unlinkRes; /**< Result array.**/
136 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
137 int numOfResults; /**< Number of results in result array.**/
140 //Example of DELETE cred request -> coaps://0.0.0.0:5684/oic/sec/cred?sub=(BASE64 ENCODED UUID)
141 const char * SRP_FORM_DELETE_CREDENTIAL = "coaps://[%s]:%d%s?%s=%s";
142 const char * SRP_FORM_DELETE_CREDENTIAL_TCP = "coaps+tcp://[%s]:%d%s?%s=%s";
144 // Structure to carry remove APIs data to callback.
145 typedef struct RemoveData RemoveData_t;
148 OCProvisionDev_t* revokeTargetDev; /**< Device which is going to be revoked..**/
149 OCProvisionDev_t* linkedDevList; /**< A list of devices which have invalid credential.**/
150 OCProvisionResult_t* removeRes; /**< Result array.**/
151 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
152 size_t numOfResults; /**< Number of results in result array.**/
153 size_t sizeOfResArray;
160 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
161 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
162 OCClientResponseHandler responseHandler);
166 * Internal function to update result in result array.
168 static void registerResultForCredProvisioning(CredentialData_t *credData,
169 OCStackResult stackresult, int cause)
172 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",credData->numOfResults);
175 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
176 credData->deviceInfo1->doxm->deviceID.id,UUID_LENGTH);
180 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
181 credData->deviceInfo2->doxm->deviceID.id,UUID_LENGTH);
183 credData->resArr[(credData->numOfResults)].res = stackresult;
184 ++(credData->numOfResults);
188 * Callback handler for handling callback of provisioning device 2.
190 * @param[in] ctx ctx value passed to callback from calling function.
191 * @param[in] UNUSED handle to an invocation
192 * @param[in] clientResponse Response from queries to remote servers.
193 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
194 * and OC_STACK_KEEP_TRANSACTION to keep it.
196 static OCStackApplicationResult provisionCredentialCB2(void *ctx, OCDoHandle UNUSED,
197 OCClientResponse *clientResponse)
199 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
200 CredentialData_t *credData = (CredentialData_t *) ctx;
203 OCProvisionResultCB resultCallback = credData->resultCallback;
204 OIC_LOG(INFO, TAG, "provisionCredentialCB2 called");
207 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
209 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CHANGED, 2);
210 OCStackResult res = PDMLinkDevices(&credData->deviceInfo1->doxm->deviceID,
211 &credData->deviceInfo2->doxm->deviceID);
212 if (OC_STACK_OK != res)
214 OIC_LOG(ERROR, TAG, "Error occured on PDMLinkDevices");
215 return OC_STACK_DELETE_TRANSACTION;
217 OIC_LOG(INFO, TAG, "Link created successfully");
219 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
222 OICFree(credData->resArr);
224 return OC_STACK_DELETE_TRANSACTION;
228 OIC_LOG(INFO, TAG, "provisionCredentialCB2 received Null clientResponse");
229 registerResultForCredProvisioning(credData, OC_STACK_ERROR, 2);
230 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
233 OICFree(credData->resArr);
235 return OC_STACK_DELETE_TRANSACTION;
239 * Callback handler for handling callback of provisioning device 1.
241 * @param[in] ctx ctx value passed to callback from calling function.
242 * @param[in] UNUSED handle to an invocation
243 * @param[in] clientResponse Response from queries to remote servers.
244 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
245 * and OC_STACK_KEEP_TRANSACTION to keep it.
247 static OCStackApplicationResult provisionCredentialCB1(void *ctx, OCDoHandle UNUSED,
248 OCClientResponse *clientResponse)
250 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
252 CredentialData_t* credData = (CredentialData_t*) ctx;
253 OICFree(credData->credInfoFirst);
254 const OCProvisionDev_t *deviceInfo = credData->deviceInfo2;
255 OicSecCred_t *credInfo = credData->credInfo;
256 const OCProvisionResultCB resultCallback = credData->resultCallback;
259 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
261 // send credentials to second device
262 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CHANGED,1);
263 OCStackResult res = provisionCredentials(credInfo, deviceInfo, credData,
264 provisionCredentialCB2);
265 DeleteCredList(credInfo);
266 if (OC_STACK_OK != res)
268 registerResultForCredProvisioning(credData, res,2);
269 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
272 OICFree(credData->resArr);
279 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
280 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
283 OICFree(credData->resArr);
290 OIC_LOG(INFO, TAG, "provisionCredentialCB received Null clientResponse for first device");
291 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
292 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
295 DeleteCredList(credInfo);
296 OICFree(credData->resArr);
300 return OC_STACK_DELETE_TRANSACTION;
306 * Internal function for handling credential generation and sending credential to resource server.
308 * @param[in] cred Instance of cred resource.
309 * @param[in] deviceInfo information about device to which credential is to be provisioned.
310 * @param[in] responseHandler callbak called by OC stack when request API receives response.
311 * @return OC_STACK_OK in case of success and other value otherwise.
313 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
314 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
315 OCClientResponseHandler responseHandler)
317 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
320 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
321 return OC_STACK_NO_MEMORY;
323 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
325 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
326 &secPayload->payloadSize, secureFlag);
327 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
329 OCPayloadDestroy((OCPayload *)secPayload);
330 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
331 return OC_STACK_NO_MEMORY;
334 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
335 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
336 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
337 if(!PMGenerateQuery(true,
338 deviceInfo->endpoint.addr,
339 deviceInfo->securePort,
340 deviceInfo->connType,
341 query, sizeof(query), OIC_RSRC_CRED_URI))
343 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
344 return OC_STACK_ERROR;
346 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
348 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
349 cbData.cb = responseHandler;
350 cbData.context = (void *) credData;
353 OCDoHandle handle = NULL;
354 OCMethod method = OC_REST_POST;
355 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
356 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
357 OIC_LOG_V(INFO, TAG, "OCDoResource::Credential provisioning returned : %d",ret);
358 if (ret != OC_STACK_OK)
360 OIC_LOG(ERROR, TAG, "OCStack resource error");
366 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
368 * Structure to carry certificate data to callback.
370 typedef struct CertificateData CertData_t;
371 struct CertificateData
373 void *ctx; /**< Pointer to user context.**/
374 const OCProvisionDev_t *deviceInfo; /**< Pointer to OCProvisionDev_t.**/
375 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
376 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
377 OCProvisionResult_t *resArr; /**< Result array.**/
378 int numOfResults; /**< Number of results in result array.**/
382 * Internal function to update result in result array.
384 static void registerResultForCertProvisioning(CertData_t *certData,
385 OCStackResult stackresult)
388 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",certData->numOfResults);
389 memcpy(certData->resArr[(certData->numOfResults)].deviceId.id,
390 certData->deviceInfo->doxm->deviceID.id,UUID_LENGTH);
391 certData->resArr[(certData->numOfResults)].res = stackresult;
392 ++(certData->numOfResults);
396 * Internal function for handling credential generation and sending cretificate credential.
398 * @param[in] cred Instance of cred resource.
399 * @param[in] deviceInfo information about device to which credential is to be provisioned.
400 * @param[in] responseHandler callbak called by OC stack when request API receives response.
401 * @return OC_STACK_OK in case of success and other value otherwise.
403 static OCStackResult provisionCertCred(const OicSecCred_t *cred,
404 const OCProvisionDev_t *deviceInfo, CertData_t *certData,
405 OCClientResponseHandler responseHandler)
407 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
410 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
411 return OC_STACK_NO_MEMORY;
413 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
415 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
416 &secPayload->payloadSize, secureFlag);
418 if ((OC_STACK_OK != res) || (NULL == secPayload->securityData))
421 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
422 return OC_STACK_NO_MEMORY;
425 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
426 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
427 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
428 if(!PMGenerateQuery(true,
429 deviceInfo->endpoint.addr,
430 deviceInfo->securePort,
431 deviceInfo->connType,
432 query, sizeof(query), OIC_RSRC_CRED_URI))
434 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
435 OCPayloadDestroy((OCPayload *)secPayload);
436 return OC_STACK_ERROR;
438 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
440 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
441 cbData.cb = responseHandler;
442 cbData.context = (void *) certData;
445 OCDoHandle handle = NULL;
446 OCMethod method = OC_REST_POST;
447 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
448 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
449 OIC_LOG_V(INFO, TAG, "OCDoResource::Certificate provisioning returned : %d",ret);
450 if (ret != OC_STACK_OK)
452 OIC_LOG(ERROR, TAG, "OCStack resource error");
458 OCStackResult SRPRegisterTrustCertChainNotifier(void *ctx, TrustCertChainChangeCB callback)
460 if (g_trustCertChainNotifier.callback)
462 OIC_LOG(ERROR, TAG, "Can't register Notifier, Unregister previous one");
463 return OC_STACK_ERROR;
466 g_trustCertChainNotifier.callback = callback;
467 g_trustCertChainNotifier.context = ctx;
471 void SRPRemoveTrustCertChainNotifier()
473 g_trustCertChainNotifier.callback = NULL;
474 g_trustCertChainNotifier.context = NULL;
479 * Callback handler for handling callback of certificate provisioning device.
481 * @param[in] ctx ctx value passed to callback from calling function.
482 * @param[in] UNUSED handle to an invocation
483 * @param[in] clientResponse Response from queries to remote servers.
484 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
485 * and OC_STACK_KEEP_TRANSACTION to keep it.
487 static OCStackApplicationResult provisionCertCB(void *ctx, OCDoHandle UNUSED,
488 OCClientResponse *clientResponse)
490 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
491 CertData_t *certData = (CertData_t *) ctx;
494 OCProvisionResultCB resultCallback = certData->resultCallback;
495 OIC_LOG(INFO, TAG, "provisionCertCred called");
498 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
500 registerResultForCertProvisioning(certData, OC_STACK_RESOURCE_CHANGED);
501 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
504 OICFree(certData->resArr);
506 return OC_STACK_DELETE_TRANSACTION;
510 OIC_LOG(INFO, TAG, "provisionCertCredCB received Null clientResponse");
511 registerResultForCertProvisioning(certData, OC_STACK_ERROR);
512 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
515 OICFree(certData->resArr);
517 return OC_STACK_DELETE_TRANSACTION;
520 OCStackResult SRPProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint16_t credId,
521 const OCProvisionDev_t *selectedDeviceInfo, OCProvisionResultCB resultCallback)
523 OIC_LOG(INFO, TAG, "In SRPProvisionTrustCertChain");
524 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
525 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
526 if (SIGNED_ASYMMETRIC_KEY != type || NULL == type)
528 OIC_LOG(INFO, TAG, "Invalid key type");
529 return OC_STACK_INVALID_PARAM;
532 OicSecCred_t *trustCertChainCred = GetCredEntryByCredId(credId);
533 if(NULL == trustCertChainCred)
535 OIC_LOG(ERROR, TAG, "Can not find matched Trust Cert. Chain.");
536 return OC_STACK_NO_RESOURCE;
539 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
542 DeleteCredList(trustCertChainCred);
543 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
544 return OC_STACK_NO_MEMORY;
546 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
548 if(OC_STACK_OK != CredToCBORPayload(trustCertChainCred, &secPayload->securityData, &secPayload->payloadSize, secureFlag))
550 DeleteCredList(trustCertChainCred);
551 OCPayloadDestroy((OCPayload *)secPayload);
552 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
553 return OC_STACK_NO_MEMORY;
555 DeleteCredList(trustCertChainCred);
556 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
557 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
559 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
560 if(!PMGenerateQuery(true,
561 selectedDeviceInfo->endpoint.addr,
562 selectedDeviceInfo->securePort,
563 selectedDeviceInfo->connType,
564 query, sizeof(query), OIC_RSRC_CRED_URI))
566 OIC_LOG(ERROR, TAG, "SRPProvisionTrustCertChain : Failed to generate query");
567 return OC_STACK_ERROR;
569 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
571 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
572 cbData.cb = &provisionCertCB;
573 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
574 if (NULL == certData)
576 OIC_LOG(ERROR, TAG, "Memory allocation problem");
577 return OC_STACK_NO_MEMORY;
579 certData->deviceInfo = selectedDeviceInfo;
580 certData->resultCallback = resultCallback;
581 certData->credInfo = NULL; //credInfo not used in the response handler
582 certData->numOfResults=0;
586 certData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
587 if (certData->resArr == NULL)
590 OCPayloadDestroy((OCPayload *)secPayload);
591 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
592 return OC_STACK_NO_MEMORY;
594 cbData.context = (void *)certData;
596 OCMethod method = OC_REST_POST;
597 OCDoHandle handle = NULL;
598 OIC_LOG(DEBUG, TAG, "Sending Cred info to resource server");
599 OCStackResult ret = OCDoResource(&handle, method, query,
600 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
601 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
602 if (ret != OC_STACK_OK)
604 OICFree(certData->resArr);
608 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
612 OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
613 OicEncodingType_t encodingType, uint16_t *credId)
615 OIC_LOG(DEBUG, TAG, "IN SRPSaveTrustCertChain");
616 VERIFY_NON_NULL(TAG, trustCertChain, ERROR, OC_STACK_INVALID_PARAM);
617 VERIFY_NON_NULL(TAG, credId, ERROR, OC_STACK_INVALID_PARAM);
619 OCStackResult res = OC_STACK_ERROR;
621 OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(*cred));
622 VERIFY_NON_NULL(TAG, cred, ERROR, OC_STACK_NO_MEMORY);
624 memcpy(cred->subject.id, &WILDCARD_SUBJECT_ID, WILDCARD_SUBJECT_ID_LEN);
626 cred->credUsage= (char *)OICCalloc(1, strlen(TRUST_CA)+1 );
627 VERIFY_NON_NULL(TAG, cred->credUsage, ERROR, OC_STACK_NO_MEMORY);
628 OICStrcpy(cred->credUsage, strlen(TRUST_CA) + 1, TRUST_CA) ;
630 cred->credType = SIGNED_ASYMMETRIC_KEY;
632 if (encodingType == OIC_ENCODING_PEM)
634 cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize + 1);
635 VERIFY_NON_NULL(TAG, cred->optionalData.data, ERROR, OC_STACK_NO_MEMORY);
636 cred->optionalData.len = chainSize + 1;
640 cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize);
641 VERIFY_NON_NULL(TAG, cred->optionalData.data, ERROR, OC_STACK_NO_MEMORY);
642 cred->optionalData.len = chainSize;
644 memcpy(cred->optionalData.data, trustCertChain, chainSize);
645 cred->optionalData.encoding = encodingType;
647 res = AddCredential(cred);
648 if(res != OC_STACK_OK)
650 DeleteCredList(cred);
653 *credId = cred->credId;
655 if (g_trustCertChainNotifier.callback)
657 uint8_t *certChain = (uint8_t*)OICCalloc(1, sizeof(uint8_t) * chainSize);
658 VERIFY_NON_NULL(TAG, certChain, ERROR, OC_STACK_NO_MEMORY);
659 memcpy(certChain, trustCertChain, chainSize);
660 g_trustCertChainNotifier.callback(g_trustCertChainNotifier.context, credId,
661 certChain, chainSize);
665 OIC_LOG(DEBUG, TAG, "OUT SRPSaveTrustCertChain");
671 OCStackResult SRPSaveOwnCertChain(OicSecCert_t * cert, OicSecKey_t * key, uint16_t *credId)
673 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
674 VERIFY_NON_NULL(TAG, cert, ERROR, OC_STACK_INVALID_PARAM);
675 VERIFY_NON_NULL(TAG, cert->data, ERROR, OC_STACK_INVALID_PARAM);
676 VERIFY_NON_NULL(TAG, key, ERROR, OC_STACK_INVALID_PARAM);
677 VERIFY_NON_NULL(TAG, key->data, ERROR, OC_STACK_INVALID_PARAM);
678 VERIFY_NON_NULL(TAG, credId, ERROR, OC_STACK_INVALID_PARAM);
680 OCStackResult res = OC_STACK_ERROR;
682 OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(*cred));
683 VERIFY_NON_NULL(TAG, cred, ERROR, OC_STACK_NO_MEMORY);
685 OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
687 if (OC_STACK_OK != GetDoxmDeviceID(&cred->subject))
689 OIC_LOG(ERROR, TAG, "Cann't get the device id(GetDoxmDeviceID)");
692 cred->credUsage= (char *)OICCalloc(1, strlen(PRIMARY_CERT)+1 );
693 VERIFY_NON_NULL(TAG, cred->credUsage, ERROR, OC_STACK_NO_MEMORY);
694 OICStrcpy(cred->credUsage, strlen(PRIMARY_CERT) + 1, PRIMARY_CERT) ;
696 cred->credType = SIGNED_ASYMMETRIC_KEY;
698 OicSecCert_t *publicData = &cred->publicData;
699 publicData->data = (uint8_t *)OICCalloc(1, cert->len);
700 VERIFY_NON_NULL(TAG, publicData->data, ERROR, OC_STACK_NO_MEMORY);
701 memcpy(publicData->data, cert->data, cert->len);
702 publicData->len = cert->len;
704 OicSecKey_t *privateData = &cred->privateData;
705 privateData->data = (uint8_t *)OICCalloc(1, key->len);
706 VERIFY_NON_NULL(TAG, privateData->data, ERROR, OC_STACK_NO_MEMORY);
707 memcpy(privateData->data, key->data, key->len);
708 privateData->len = key->len;
709 privateData->encoding = key->encoding;
711 res = AddCredential(cred);
712 if(res != OC_STACK_OK)
714 DeleteCredList(cred);
717 *credId = cred->credId;
719 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
723 #endif // __WITH_DTLS__ || __WITH_TLS__
725 OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
726 const OCProvisionDev_t *pDev1,
727 const OCProvisionDev_t *pDev2,
728 OCProvisionResultCB resultCallback)
730 VERIFY_NON_NULL(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM);
731 if (SYMMETRIC_PAIR_WISE_KEY == type)
733 VERIFY_NON_NULL(TAG, pDev2, ERROR, OC_STACK_INVALID_PARAM);
737 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
738 return OC_STACK_INVALID_CALLBACK;
740 if (SYMMETRIC_PAIR_WISE_KEY == type &&
741 0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
743 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
744 return OC_STACK_INVALID_PARAM;
747 if (SYMMETRIC_PAIR_WISE_KEY == type &&
748 !(OWNER_PSK_LENGTH_128 == keySize || OWNER_PSK_LENGTH_256 == keySize))
750 OIC_LOG(INFO, TAG, "Invalid key size");
751 return OC_STACK_INVALID_PARAM;
754 OIC_LOG(INFO, TAG, "In SRPProvisionCredentials");
756 if (SYMMETRIC_PAIR_WISE_KEY == type)
758 bool linkExisits = true;
759 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExisits);
761 if (res != OC_STACK_OK)
763 OIC_LOG(ERROR, TAG, "Internal error occured");
768 OIC_LOG(ERROR, TAG, "Link already exists");
769 return OC_STACK_INVALID_PARAM;
773 OicUuid_t provTooldeviceID = {{0,}};
774 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
776 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
777 return OC_STACK_ERROR;
779 OIC_LOG(INFO, TAG, "retrieved deviceid");
782 case SYMMETRIC_PAIR_WISE_KEY:
784 const OCProvisionDev_t *firstDevice = pDev1;
785 const OCProvisionDev_t *secondDevice = pDev2;
787 OicSecCred_t *firstCred = NULL;
788 OicSecCred_t *secondCred = NULL;
789 OCStackResult res = PMGeneratePairWiseCredentials(type, keySize, &provTooldeviceID,
790 &firstDevice->doxm->deviceID, &secondDevice->doxm->deviceID,
791 &firstCred, &secondCred);
792 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
793 OIC_LOG(INFO, TAG, "Credentials generated successfully");
794 CredentialData_t *credData =
795 (CredentialData_t *) OICCalloc(1, sizeof(CredentialData_t));
796 if (NULL == credData)
800 OIC_LOG(ERROR, TAG, "Memory allocation problem");
801 return OC_STACK_NO_MEMORY;
803 credData->deviceInfo1 = firstDevice;
804 credData->deviceInfo2 = secondDevice;
805 credData->credInfo = secondCred;
807 credData->credInfoFirst = firstCred;
808 credData->numOfResults = 0;
809 credData->resultCallback = resultCallback;
810 // first call to provision creds to device1.
811 // second call to provision creds to device2.
814 (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
815 if (NULL == credData->resArr)
820 OIC_LOG(ERROR, TAG, "Memory allocation problem");
821 return OC_STACK_NO_MEMORY;
823 res = provisionCredentials(firstCred, firstDevice, credData, &provisionCredentialCB1);
824 if (OC_STACK_OK != res)
826 DeleteCredList(firstCred);
827 DeleteCredList(secondCred);
828 OICFree(credData->resArr);
831 OIC_LOG_V(INFO, TAG, "provisionCredentials returned: %d",res);
832 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
837 OIC_LOG(ERROR, TAG, "Invalid option.");
838 return OC_STACK_INVALID_PARAM;
841 return OC_STACK_ERROR;
845 * Internal Function to store results in result array during ACL provisioning.
847 static void registerResultForACLProvisioning(ACLData_t *aclData,
848 OCStackResult stackresult)
850 OIC_LOG_V(INFO, TAG, "Inside registerResultForACLProvisioning aclData->numOfResults is %d\n",
851 aclData->numOfResults);
852 memcpy(aclData->resArr[(aclData->numOfResults)].deviceId.id,
853 aclData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
854 aclData->resArr[(aclData->numOfResults)].res = stackresult;
855 ++(aclData->numOfResults);
859 * Callback handler of SRPProvisionACL.
861 * @param[in] ctx ctx value passed to callback from calling function.
862 * @param[in] UNUSED handle to an invocation
863 * @param[in] clientResponse Response from queries to remote servers.
864 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
865 * and OC_STACK_KEEP_TRANSACTION to keep it.
867 static OCStackApplicationResult SRPProvisionACLCB(void *ctx, OCDoHandle UNUSED,
868 OCClientResponse *clientResponse)
870 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionACLCB.");
872 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
873 ACLData_t *aclData = (ACLData_t*)ctx;
874 OCProvisionResultCB resultCallback = aclData->resultCallback;
878 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
880 registerResultForACLProvisioning(aclData, OC_STACK_RESOURCE_CHANGED);
881 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
884 OICFree(aclData->resArr);
886 return OC_STACK_DELETE_TRANSACTION;
889 registerResultForACLProvisioning(aclData, OC_STACK_ERROR);
890 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
893 OIC_LOG_V(ERROR, TAG, "SRPProvisionACLCB received Null clientResponse");
894 OICFree(aclData->resArr);
896 return OC_STACK_DELETE_TRANSACTION;
899 OCStackResult SRPProvisionACL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
900 OicSecAcl_t *acl, OCProvisionResultCB resultCallback)
902 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
903 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
904 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
906 // if rowneruuid is empty, set it to device ID
907 OicUuid_t emptyOwner = {.id = {0} };
908 if (memcmp(&(acl->rownerID.id), &emptyOwner, UUID_IDENTITY_SIZE) == 0)
910 OIC_LOG(DEBUG, TAG, "Set Rowner to PT's deviceId, because Rowner of ACL is empty");
913 if (OC_STACK_OK == GetDoxmDeviceID(&oicUuid))
915 memcpy(&(acl->rownerID.id), &oicUuid, UUID_IDENTITY_SIZE);
919 OIC_LOG(DEBUG, TAG, "Failed to set Rowner to PT's deviceID\
920 becuase it failed to retrieve Doxm DeviceID");
921 return OC_STACK_ERROR;
925 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
928 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
929 return OC_STACK_NO_MEMORY;
931 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
932 if(OC_STACK_OK != AclToCBORPayload(acl, &secPayload->securityData, &secPayload->payloadSize))
934 OCPayloadDestroy((OCPayload *)secPayload);
935 OIC_LOG(ERROR, TAG, "Failed to AclToCBORPayload");
936 return OC_STACK_NO_MEMORY;
938 OIC_LOG(DEBUG, TAG, "Created payload for ACL:");
939 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
941 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
942 if(!PMGenerateQuery(true,
943 selectedDeviceInfo->endpoint.addr,
944 selectedDeviceInfo->securePort,
945 selectedDeviceInfo->connType,
946 query, sizeof(query), OIC_RSRC_ACL_URI))
948 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
949 return OC_STACK_ERROR;
951 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
953 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
954 cbData.cb = &SRPProvisionACLCB;
955 ACLData_t *aclData = (ACLData_t *) OICCalloc(1, sizeof(ACLData_t));
958 OCPayloadDestroy((OCPayload *)secPayload);
959 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
960 return OC_STACK_NO_MEMORY;
962 aclData->deviceInfo = selectedDeviceInfo;
963 aclData->resultCallback = resultCallback;
964 aclData->numOfResults=0;
966 // call to provision ACL to device1.
968 aclData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
969 if (aclData->resArr == NULL)
972 OCPayloadDestroy((OCPayload *)secPayload);
973 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
974 return OC_STACK_NO_MEMORY;
976 cbData.context = (void *)aclData;
978 OCMethod method = OC_REST_POST;
979 OCDoHandle handle = NULL;
980 OIC_LOG(DEBUG, TAG, "Sending ACL info to resource server");
981 OCStackResult ret = OCDoResource(&handle, method, query,
982 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
983 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
984 if (ret != OC_STACK_OK)
986 OICFree(aclData->resArr);
989 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
993 OCStackResult SRPSaveACL(const OicSecAcl_t *acl)
995 OIC_LOG(DEBUG, TAG, "IN SRPSaveACL");
996 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
998 OCStackResult res = InstallACL(acl);
1000 OIC_LOG(DEBUG, TAG, "OUT SRPSaveACL");
1005 * Internal Function to store results in result array during Direct-Pairing provisioning.
1007 static void registerResultForDirectPairingProvisioning(PconfData_t *pconfData,
1008 OCStackResult stackresult)
1010 OIC_LOG_V(INFO, TAG, "Inside registerResultForDirectPairingProvisioning "
1011 "pconfData->numOfResults is %d\n", pconfData->numOfResults);
1012 memcpy(pconfData->resArr[(pconfData->numOfResults)].deviceId.id,
1013 pconfData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
1014 pconfData->resArr[(pconfData->numOfResults)].res = stackresult;
1015 ++(pconfData->numOfResults);
1019 * Callback handler of SRPProvisionDirectPairing.
1021 * @param[in] ctx ctx value passed to callback from calling function.
1022 * @param[in] UNUSED handle to an invocation
1023 * @param[in] clientResponse Response from queries to remote servers.
1024 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1025 * and OC_STACK_KEEP_TRANSACTION to keep it.
1027 static OCStackApplicationResult SRPProvisionDirectPairingCB(void *ctx, OCDoHandle UNUSED,
1028 OCClientResponse *clientResponse)
1030 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionDirectPairingCB.");
1032 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
1033 PconfData_t *pconfData = (PconfData_t*)ctx;
1034 OCProvisionResultCB resultCallback = pconfData->resultCallback;
1038 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1040 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_OK);
1041 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
1044 OICFree(pconfData->resArr);
1046 return OC_STACK_DELETE_TRANSACTION;
1049 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_ERROR);
1050 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
1053 OIC_LOG_V(ERROR, TAG, "SRPProvisionDirectPairingCB received Null clientResponse");
1054 OICFree(pconfData->resArr);
1056 return OC_STACK_DELETE_TRANSACTION;
1059 OCStackResult SRPProvisionDirectPairing(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
1060 OicSecPconf_t *pconf, OCProvisionResultCB resultCallback)
1062 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
1063 VERIFY_NON_NULL(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
1064 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1066 // check direct-pairing capability
1067 if (true != selectedDeviceInfo->doxm->dpc)
1069 OIC_LOG(ERROR, TAG, "Resouce server does not have Direct-Pairing Capability ");
1070 return OC_STACK_UNAUTHORIZED_REQ;
1073 OicUuid_t provTooldeviceID = {.id={0}};
1074 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1076 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1077 return OC_STACK_ERROR;
1079 memcpy(&pconf->rownerID, &provTooldeviceID, sizeof(OicUuid_t));
1081 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1084 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1085 return OC_STACK_NO_MEMORY;
1087 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1089 if (OC_STACK_OK != PconfToCBORPayload(pconf, &(secPayload->securityData),
1090 &(secPayload->payloadSize)))
1092 OCPayloadDestroy((OCPayload*)secPayload);
1093 OIC_LOG(ERROR, TAG, "Failed to PconfToCborPayload");
1094 return OC_STACK_NO_MEMORY;
1096 OIC_LOG(DEBUG, TAG, "Created payload for pconf set");
1097 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1100 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1101 if(!PMGenerateQuery(true,
1102 selectedDeviceInfo->endpoint.addr,
1103 selectedDeviceInfo->securePort,
1104 selectedDeviceInfo->connType,
1105 query, sizeof(query), OIC_RSRC_PCONF_URI))
1107 OIC_LOG(ERROR, TAG, "SRPProvisionDirectPairing : Failed to generate query");
1108 return OC_STACK_ERROR;
1110 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1112 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1113 cbData.cb = &SRPProvisionDirectPairingCB;
1114 PconfData_t *pconfData = (PconfData_t *) OICCalloc(1, sizeof(PconfData_t));
1115 if (NULL == pconfData)
1117 OCPayloadDestroy((OCPayload*)secPayload);
1118 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1119 return OC_STACK_NO_MEMORY;
1121 pconfData->deviceInfo = selectedDeviceInfo;
1122 pconfData->resultCallback = resultCallback;
1123 pconfData->numOfResults=0;
1124 pconfData->ctx = ctx;
1125 // call to provision PCONF to device1.
1126 int noOfRiCalls = 1;
1127 pconfData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1128 if (NULL == pconfData->resArr)
1131 OCPayloadDestroy((OCPayload*)secPayload);
1132 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1133 return OC_STACK_NO_MEMORY;
1135 cbData.context = (void *)pconfData;
1137 OCMethod method = OC_REST_POST;
1138 OCDoHandle handle = NULL;
1139 OIC_LOG(DEBUG, TAG, "Sending PCONF info to resource server");
1140 OCStackResult ret = OCDoResource(&handle, method, query,
1141 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1142 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1143 if (OC_STACK_OK != ret)
1145 OICFree(pconfData->resArr);
1148 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1152 static void DeleteUnlinkData_t(UnlinkData_t *unlinkData)
1156 OICFree(unlinkData->unlinkDev);
1157 OICFree(unlinkData->unlinkRes);
1158 OICFree(unlinkData);
1162 static void registerResultForUnlinkDevices(UnlinkData_t *unlinkData, OCStackResult stackresult,
1165 if (NULL != unlinkData)
1167 OIC_LOG_V(INFO, TAG, "Inside registerResultForUnlinkDevices unlinkData->numOfResults is %d\n",
1168 unlinkData->numOfResults);
1169 OIC_LOG_V(INFO, TAG, "Stack result :: %d", stackresult);
1171 OicUuid_t *pUuid = &unlinkData->unlinkRes[(unlinkData->numOfResults)].deviceId;
1173 // Set result in the result array according to the position (devNum).
1174 if (idx != IDX_DB_UPDATE_RES)
1176 memcpy(pUuid->id, unlinkData->unlinkDev[idx].doxm->deviceID.id, sizeof(pUuid->id));
1179 { // When deivce ID is 000... this means it's the result of database update.
1180 memset(pUuid->id, 0, sizeof(pUuid->id));
1182 unlinkData->unlinkRes[(unlinkData->numOfResults)].res = stackresult;
1183 ++(unlinkData->numOfResults);
1184 OIC_LOG (INFO, TAG, "Out registerResultForUnlinkDevices");
1188 static OCStackResult SendDeleteCredentialRequest(void* ctx,
1189 OCClientResponseHandler respHandler,
1190 const OCProvisionDev_t* revokedDev,
1191 const OCProvisionDev_t* destDev)
1193 OIC_LOG(DEBUG, TAG, "IN SendDeleteCredentialRequest");
1195 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1197 return OC_STACK_INVALID_PARAM;
1201 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1202 if(OC_STACK_OK != ret)
1204 OIC_LOG(ERROR, TAG, "SendDeleteCredentialRequest : Failed to canonical UUID encoding");
1205 return OC_STACK_ERROR;
1208 char addressEncoded[CA_MAX_URI_LENGTH] = {0};
1209 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
1210 sizeof(addressEncoded),
1211 destDev->endpoint.addr);
1212 if (OC_STACK_OK != result)
1214 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : encoding error %d\n", result);
1215 return OC_STACK_ERROR;
1218 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1220 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1221 char *srpUri = SRP_FORM_DELETE_CREDENTIAL;
1223 if(CA_ADAPTER_TCP == destDev->endpoint.adapter)
1225 srpUri = SRP_FORM_DELETE_CREDENTIAL_TCP;
1229 snRet = snprintf(reqBuf, sizeof(reqBuf), srpUri, addressEncoded,
1230 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1234 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1235 return OC_STACK_ERROR;
1237 else if ((size_t)snRet >= sizeof(reqBuf))
1239 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1240 return OC_STACK_ERROR;
1243 OCCallbackData cbData;
1244 memset(&cbData, 0, sizeof(cbData));
1245 cbData.context = ctx;
1246 cbData.cb = respHandler;
1248 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1250 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1252 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1253 &destDev->endpoint, NULL,
1254 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1255 if (OC_STACK_OK != ret)
1257 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1259 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1264 static OCStackResult SendDeleteACLRequest(void* ctx,
1265 OCClientResponseHandler respHandler,
1266 const OCProvisionDev_t* revokedDev,
1267 const OCProvisionDev_t* destDev)
1269 OIC_LOG(DEBUG, TAG, "IN SendDeleteACLRequest");
1271 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1273 return OC_STACK_INVALID_PARAM;
1277 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1278 if(OC_STACK_OK != ret)
1280 OIC_LOG(ERROR, TAG, "SendDeleteACLRequest : Failed to canonical UUID encoding");
1281 return OC_STACK_ERROR;
1284 char addressEncoded[CA_MAX_URI_LENGTH] = {0};
1285 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
1286 sizeof(addressEncoded),
1287 destDev->endpoint.addr);
1288 if (OC_STACK_OK != result)
1290 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : encoding error %d\n", result);
1291 return OC_STACK_ERROR;
1295 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1297 //coaps://0.0.0.0:5684/oic/sec/acl?subjectuuid=(Canonical ENCODED UUID)
1298 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, addressEncoded,
1299 destDev->securePort, OIC_RSRC_ACL_URI, OIC_JSON_SUBJECTID_NAME, subID);
1303 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error (snprintf) %d\n", snRet);
1304 return OC_STACK_ERROR;
1306 else if ((size_t)snRet >= sizeof(reqBuf))
1308 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Truncated (snprintf) %d\n", snRet);
1309 return OC_STACK_ERROR;
1312 OCCallbackData cbData;
1313 memset(&cbData, 0, sizeof(cbData));
1314 cbData.context = ctx;
1315 cbData.cb = respHandler;
1317 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1319 OIC_LOG(DEBUG, TAG, "Sending remove ACL request to resource server");
1321 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1322 &destDev->endpoint, NULL,
1323 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1324 if (OC_STACK_OK != ret)
1326 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error in OCDoResource %d", ret);
1328 OIC_LOG(DEBUG, TAG, "OUT SendDeleteACLRequest");
1334 * Callback handler of unlink second device.
1336 * @param[in] ctx ctx value passed to callback from calling function.
1337 * @param[in] handle handle to an invocation
1338 * @param[in] clientResponse Response from queries to remote servers.
1339 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1340 * OC_STACK_KEEP_TRANSACTION to keep it.
1342 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1343 OCClientResponse *clientResponse)
1346 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1347 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1348 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1352 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1353 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1355 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1357 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1361 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1362 unlinkData->resultCallback(unlinkData->ctx,
1363 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1369 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1370 IDX_SECOND_DEVICE_RES);
1371 unlinkData->resultCallback(unlinkData->ctx,
1372 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1373 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1377 //Update provisioning DB when succes case.
1378 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1379 &unlinkData->unlinkDev[1].doxm->deviceID))
1381 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1382 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1383 unlinkData->resultCallback(unlinkData->ctx,
1384 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1387 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1391 DeleteUnlinkData_t(unlinkData);
1392 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1393 return OC_STACK_DELETE_TRANSACTION;
1398 * Callback handler of unlink first device.
1400 * @param[in] ctx ctx value passed to callback from calling function.
1401 * @param[in] handle handle to an invocation
1402 * @param[in] clientResponse Response from queries to remote servers.
1403 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1404 * OC_STACK_KEEP_TRANSACTION to keep it.
1406 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1407 OCClientResponse *clientResponse)
1409 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1410 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1411 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1416 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1417 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1419 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1421 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1423 // Second revocation request to second device.
1424 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1425 &unlinkData->unlinkDev[0],
1426 &unlinkData->unlinkDev[1] /*Dest*/);
1427 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1428 if (OC_STACK_OK != res)
1430 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1431 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1432 IDX_SECOND_DEVICE_RES);
1433 unlinkData->resultCallback(unlinkData->ctx,
1434 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1439 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1440 return OC_STACK_DELETE_TRANSACTION;
1445 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1447 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1448 unlinkData->unlinkRes, true);
1454 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1455 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1456 IDX_FIRST_DEVICE_RES );
1457 unlinkData->resultCallback(unlinkData->ctx,
1458 unlinkData->numOfResults, unlinkData->unlinkRes,
1460 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1464 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1465 DeleteUnlinkData_t(unlinkData);
1466 return OC_STACK_DELETE_TRANSACTION;
1470 * Function to unlink devices.
1471 * This function will remove the credential & relationship between the two devices.
1473 * @param[in] ctx Application context would be returned in result callback
1474 * @param[in] pTargetDev1 first device information to be unlinked.
1475 * @param[in] pTargetDev2 second device information to be unlinked.
1476 * @param[in] resultCallback callback provided by API user, callback will be called when
1477 * device unlink is finished.
1478 * @return OC_STACK_OK in case of success and other value otherwise.
1480 OCStackResult SRPUnlinkDevices(void* ctx,
1481 const OCProvisionDev_t* pTargetDev1,
1482 const OCProvisionDev_t* pTargetDev2,
1483 OCProvisionResultCB resultCallback)
1485 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1487 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1489 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1490 return OC_STACK_INVALID_PARAM;
1492 if (!resultCallback)
1494 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1495 return OC_STACK_INVALID_CALLBACK;
1497 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1499 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1500 return OC_STACK_INVALID_PARAM;
1503 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1504 PMPrintOCProvisionDev(pTargetDev1);
1505 PMPrintOCProvisionDev(pTargetDev2);
1507 // Mark the link status stale
1508 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1509 if (OC_STACK_OK != res)
1511 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1515 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1516 VERIFY_NON_NULL(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1518 //Initialize unlink data
1519 unlinkData->ctx = ctx;
1520 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1521 if (NULL == unlinkData->unlinkDev)
1523 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1524 res = OC_STACK_NO_MEMORY;
1528 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1529 if (NULL == unlinkData->unlinkRes)
1531 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1532 res = OC_STACK_NO_MEMORY;
1536 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1537 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1539 unlinkData->numOfResults = 0;
1540 unlinkData->resultCallback = resultCallback;
1542 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1543 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1544 if (OC_STACK_OK != res)
1546 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1553 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1554 DeleteUnlinkData_t(unlinkData);
1558 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1562 OICFree(pRemoveData->revokeTargetDev);
1563 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1564 OICFree(pRemoveData->removeRes);
1565 OICFree(pRemoveData);
1569 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1570 OCStackResult stackresult, bool hasError)
1572 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1573 removeData->numOfResults + 1);
1576 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1577 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1581 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1582 0, sizeof(pLinkedDevId->id) );
1584 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1585 removeData->hasError = hasError;
1586 ++(removeData->numOfResults);
1588 // If we get suffcient result from linked devices, we have to call user callback and do free
1589 if (removeData->sizeOfResArray == removeData->numOfResults)
1591 if(!removeData->hasError)
1593 // Remove device info from prvisioning database
1594 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1596 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1597 removeData->hasError = true;
1600 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1601 removeData->hasError);
1602 DeleteRemoveData_t(removeData);
1606 static void registerResultForResetDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1607 OCStackResult stackresult, bool hasError)
1609 OIC_LOG_V(INFO, TAG, "Inside registerResultForResetDevice removeData->numOfResults is %zu\n",
1610 removeData->numOfResults + 1);
1613 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1614 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1618 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1619 0, sizeof(pLinkedDevId->id) );
1621 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1622 removeData->hasError = hasError;
1623 ++(removeData->numOfResults);
1625 // If we get suffcient result from linked devices, we have to call user callback and do free
1626 if (removeData->sizeOfResArray == removeData->numOfResults)
1628 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1629 removeData->hasError);
1630 DeleteRemoveData_t(removeData);
1635 * Callback handler of unlink first device.
1637 * @param[in] ctx ctx value passed to callback from calling function.
1638 * @param[in] handle handle to an invocation
1639 * @param[in] clientResponse Response from queries to remote servers.
1640 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1641 * and OC_STACK_KEEP_TRANSACTION to keep it.
1643 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1644 OCClientResponse *clientResponse)
1646 //Update the delete credential into delete device context
1647 //Save the deleted status in delDevCtx
1649 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1650 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1651 OCStackResult res = OC_STACK_ERROR;
1653 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1657 OicUuid_t revDevUuid = {.id={0}};
1658 if(UUID_LENGTH == clientResponse->identity.id_length)
1660 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1661 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1663 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1664 if (OC_STACK_OK != res)
1666 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1667 registerResultForRemoveDevice(removeData, &revDevUuid,
1668 OC_STACK_INCONSISTENT_DB, true);
1670 return OC_STACK_DELETE_TRANSACTION;
1673 registerResultForRemoveDevice(removeData, &revDevUuid,
1674 OC_STACK_RESOURCE_DELETED, false);
1678 registerResultForRemoveDevice(removeData, &revDevUuid,
1679 clientResponse->result, true);
1680 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1685 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1686 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1688 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1691 * Since server's credential was deleted,
1692 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1694 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1695 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1699 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1705 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1706 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1710 return OC_STACK_DELETE_TRANSACTION;
1714 * Callback handler of reset device.
1716 * @param[in] ctx ctx value passed to callback from calling function.
1717 * @param[in] handle handle to an invocation
1718 * @param[in] clientResponse Response from queries to remote servers.
1719 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1720 * and OC_STACK_KEEP_TRANSACTION to keep it.
1722 static OCStackApplicationResult SRPSyncDeviceCredCB(void *delDevCtx, OCDoHandle handle,
1723 OCClientResponse *clientResponse)
1725 //Update the delete credential into delete device context
1726 //Save the deleted status in delDevCtx
1728 OIC_LOG_V(INFO, TAG, "Inside SRPSyncDeviceCredCB.");
1729 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1730 OCStackResult res = OC_STACK_ERROR;
1732 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1733 OCProvisionDev_t * pTargetDev = PMCloneOCProvisionDev(removeData->revokeTargetDev);
1734 OCProvisionResultCB resultCallback = removeData->resultCallback;
1737 OicUuid_t revDevUuid = {.id={0}};
1738 if(UUID_LENGTH == clientResponse->identity.id_length)
1740 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1741 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1743 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1744 if (OC_STACK_OK != res)
1746 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1747 registerResultForResetDevice(removeData, &revDevUuid,
1748 OC_STACK_INCONSISTENT_DB, true);
1750 return OC_STACK_DELETE_TRANSACTION;
1753 registerResultForResetDevice(removeData, &revDevUuid,
1754 OC_STACK_RESOURCE_DELETED, false);
1758 registerResultForResetDevice(removeData, &revDevUuid,
1759 clientResponse->result, false);
1760 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1765 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1766 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1768 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1771 * Since server's credential was deleted,
1772 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1774 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1775 registerResultForResetDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1779 registerResultForResetDevice(removeData, NULL, clientResponse->result, true);
1785 registerResultForResetDevice(removeData, NULL, OC_STACK_ERROR, true);
1786 OIC_LOG(ERROR, TAG, "SRPSyncDevice received Null clientResponse");
1789 SRPResetDevice(pTargetDev, resultCallback);
1791 return OC_STACK_DELETE_TRANSACTION;
1795 * Callback handler of reset device sync-up
1797 * @param[in] ctx ctx value passed to callback from calling function.
1798 * @param[in] handle handle to an invocation
1799 * @param[in] clientResponse Response from queries to remote servers.
1800 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1801 * and OC_STACK_KEEP_TRANSACTION to keep it.
1803 static OCStackApplicationResult SRPSyncDeviceACLCB(void *ctx, OCDoHandle handle,
1804 OCClientResponse *clientResponse)
1806 return OC_STACK_DELETE_TRANSACTION;
1810 * Callback handler of device remote reset.
1812 * @param[in] ctx ctx value passed to callback from calling function.
1813 * @param[in] handle handle to an invocation
1814 * @param[in] clientResponse Response from queries to remote servers.
1815 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1816 * and OC_STACK_KEEP_TRANSACTION to keep it.
1818 static OCStackApplicationResult SRPResetDeviceCB(void *ctx, OCDoHandle handle,
1819 OCClientResponse *clientResponse)
1821 OIC_LOG(DEBUG, TAG, "IN SRPResetDeviceCB");
1822 if(OC_STACK_OK == clientResponse->result)
1824 OIC_LOG(DEBUG, TAG, "Change Target Device Pstat Cm SUCCEEDED");
1827 // Delete Cred and ACL related to the target device.
1828 const OicSecCred_t *cred = NULL;
1829 OCProvisionDev_t * pTargetDev = (OCProvisionDev_t *)ctx;
1830 cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
1833 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to get credential of target device.");
1837 OCStackResult res = RemoveCredential(&cred->subject);
1838 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1840 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove credential.");
1844 res = RemoveACE(&cred->subject, NULL);
1845 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1847 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove ACL.");
1850 if (OC_STACK_OK != PDMDeleteDevice(&pTargetDev->doxm->deviceID))
1852 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to delete device from PDM");
1855 //Close the DTLS session of the reset device.
1856 CAEndpoint_t* endpoint = (CAEndpoint_t *)&clientResponse->devAddr;
1857 CAResult_t caResult = CAcloseSslSession(endpoint);
1858 if(CA_STATUS_OK != caResult)
1860 OIC_LOG_V(WARNING, TAG, "OCResetDevice : Failed to close DTLS session : %d", caResult);
1864 * If there is no linked device, PM does not send any request.
1865 * So we should directly invoke the result callback to inform the result of OCResetDevice.
1867 if(OC_STACK_NO_RESOURCE == res)
1873 OICFree(pTargetDev);
1874 return OC_STACK_DELETE_TRANSACTION;
1878 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1879 OCProvisionDev_t* pOwnedDevList,
1880 OCUuidList_t* pLinkedUuidList,
1881 OCProvisionDev_t** ppLinkedDevList,
1882 size_t *numOfLinkedDev)
1884 // pOwnedDevList could be NULL. It means no alived and owned device now.
1885 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1886 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1888 return OC_STACK_INVALID_PARAM;
1892 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1893 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1895 // Mark the link status stale.
1896 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1897 if (OC_STACK_OK != res)
1899 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1900 return OC_STACK_INCONSISTENT_DB;
1905 // If this linked device is alive (power-on), add the deivce to the list.
1906 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1907 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1909 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1911 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1912 if (NULL == targetDev)
1914 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1915 return OC_STACK_NO_MEMORY;
1918 LL_PREPEND(*ppLinkedDevList, targetDev);
1925 *numOfLinkedDev = cnt;
1930 * Function to device revocation
1931 * This function will remove credential of target device from all devices in subnet.
1933 * @param[in] ctx Application context would be returned in result callback
1934 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1935 * @param[in] pTargetDev Device information to be revoked.
1936 * @param[in] resultCallback callback provided by API user, callback will be called when
1937 * credential revocation is finished.
1938 * @return OC_STACK_OK in case of success and other value otherwise.
1939 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1940 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1942 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1943 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1945 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1947 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1949 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1950 return OC_STACK_INVALID_PARAM;
1952 if (!resultCallback)
1954 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
1955 return OC_STACK_INVALID_CALLBACK;
1958 // Declare variables in here to handle error cases with goto statement.
1959 OCProvisionDev_t* pOwnedDevList = NULL;
1960 OCProvisionDev_t* pLinkedDevList = NULL;
1961 RemoveData_t* removeData = NULL;
1963 //1. Find all devices that has a credential of the revoked device
1964 OCUuidList_t* pLinkedUuidList = NULL;
1965 size_t numOfDevices = 0;
1966 OCStackResult res = OC_STACK_ERROR;
1967 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
1968 if (OC_STACK_OK != res)
1970 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
1973 // if there is no related device, we can skip further process.
1974 if (0 == numOfDevices)
1976 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
1977 res = OC_STACK_CONTINUE;
1981 //2. Find owned device from the network
1982 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
1983 if (OC_STACK_OK != res)
1985 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
1989 //3. Make a list of devices to send DELETE credential request
1990 // by comparing owned devices from provisioning database with mutlicast discovery result.
1991 size_t numOfLinkedDev = 0;
1992 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
1993 &pLinkedDevList, &numOfLinkedDev);
1994 if (OC_STACK_OK != res)
1996 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
1999 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2000 { // So we don't have to send request message.
2001 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
2002 res = OC_STACK_CONTINUE;
2006 // 4. Prepare RemoveData Context data.
2007 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2010 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
2011 res = OC_STACK_NO_MEMORY;
2015 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2016 if (!removeData->revokeTargetDev)
2018 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
2019 res = OC_STACK_NO_MEMORY;
2023 removeData->removeRes =
2024 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2025 if (!removeData->removeRes)
2027 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
2028 res = OC_STACK_NO_MEMORY;
2032 removeData->ctx = ctx;
2033 removeData->linkedDevList = pLinkedDevList;
2034 removeData->resultCallback = resultCallback;
2035 removeData->numOfResults = 0;
2036 removeData->sizeOfResArray = numOfLinkedDev;
2037 removeData->hasError = false;
2039 // 5. Send DELETE credential request to linked devices.
2040 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2041 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2042 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2044 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2045 removeData->revokeTargetDev, curDev);
2046 if (OC_STACK_OK != res)
2048 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
2049 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2053 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2057 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2058 PMDeleteDeviceList(pOwnedDevList);
2059 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
2061 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2064 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2065 PMDeleteDeviceList(pOwnedDevList);
2066 PMDeleteDeviceList(pLinkedDevList);
2069 OICFree(removeData->revokeTargetDev);
2070 OICFree(removeData->removeRes);
2071 OICFree(removeData);
2073 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");
2078 * Function to device revocation
2079 * This function will remove credential of target device from all devices in subnet.
2081 * @param[in] ctx Application context would be returned in result callback
2082 * @param[in] pOwnedDevList List of owned devices
2083 * @param[in] pTargetDev Device information to be revoked.
2084 * @param[in] resultCallback callback provided by API user, callback will be called when
2085 * credential revocation is finished.
2086 * @return OC_STACK_OK in case of success and other value otherwise.
2087 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2088 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2090 OCStackResult SRPRemoveDeviceWithoutDiscovery(void* ctx, const OCProvisionDev_t* pOwnedDevList,
2091 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2093 OIC_LOG(INFO, TAG, "IN SRPRemoveDeviceWithoutDiscovery");
2097 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : Owned Device List is empty");
2098 return OC_STACK_CONTINUE;
2102 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL parameters");
2103 return OC_STACK_INVALID_PARAM;
2105 if (!resultCallback)
2107 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL Callback");
2108 return OC_STACK_INVALID_CALLBACK;
2111 // Declare variables in here to handle error cases with goto statement.
2112 OCProvisionDev_t* pLinkedDevList = NULL;
2113 RemoveData_t* removeData = NULL;
2115 //1. Find all devices that has a credential of the revoked device
2116 OCUuidList_t* pLinkedUuidList = NULL;
2117 size_t numOfDevices = 0;
2118 OCStackResult res = OC_STACK_ERROR;
2119 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2120 if (OC_STACK_OK != res)
2122 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to get linked devices information");
2123 return OC_STACK_CONTINUE;
2125 // if there is no related device, we can skip further process.
2126 if (0 == numOfDevices)
2128 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : No linked device found.");
2129 return OC_STACK_CONTINUE;
2132 //2. Make a list of devices to send DELETE credential request
2133 // by comparing owned devices from provisioning database with mutlicast discovery result.
2134 size_t numOfLinkedDev = 0;
2135 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2136 &pLinkedDevList, &numOfLinkedDev);
2137 if (OC_STACK_OK != res)
2139 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : GetListofDevToReqDeleteCred() failed");
2142 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2143 { // So we don't have to send request message.
2144 OIC_LOG(DEBUG, TAG, "SRPRemoveDeviceWithoutDiscovery : No alived & linked device found.");
2145 return OC_STACK_CONTINUE;
2148 // 3. Prepare RemoveData Context data.
2149 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2152 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2153 res = OC_STACK_NO_MEMORY;
2157 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2158 if (!removeData->revokeTargetDev)
2160 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : PMCloneOCProvisionDev Failed");
2161 res = OC_STACK_NO_MEMORY;
2165 removeData->removeRes =
2166 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2167 if (!removeData->removeRes)
2169 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2170 res = OC_STACK_NO_MEMORY;
2174 removeData->ctx = ctx;
2175 removeData->linkedDevList = pLinkedDevList;
2176 removeData->resultCallback = resultCallback;
2177 removeData->numOfResults = 0;
2178 removeData->sizeOfResArray = numOfLinkedDev;
2179 removeData->hasError = false;
2181 // 5. Send DELETE credential request to linked devices.
2182 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2183 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2184 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2186 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2187 removeData->revokeTargetDev, curDev);
2188 if (OC_STACK_OK != res)
2190 OIC_LOG_V(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Fail to send the DELETE credential request to\
2191 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2195 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2199 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2200 OIC_LOG(INFO, TAG, "OUT SRPRemoveDeviceWithoutDiscovery");
2202 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2205 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2206 PMDeleteDeviceList(pLinkedDevList);
2209 OICFree(removeData->revokeTargetDev);
2210 OICFree(removeData->removeRes);
2211 OICFree(removeData);
2213 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDeviceWithoutDiscovery");
2218 * Function to sync-up credential and ACL of the target device.
2219 * This function will remove credential and ACL of target device from all devices in subnet.
2221 * @param[in] ctx Application context would be returned in result callback
2222 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
2223 * @param[in] pTargetDev Device information to be revoked.
2224 * @param[in] resultCallback callback provided by API user, callback will be called when
2225 * credential revocation is finished.
2226 * when there is an error, this user callback is called immediately.
2227 * @return OC_STACK_OK in case of success and other value otherwise.
2228 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2229 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2231 OCStackResult SRPSyncDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
2232 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2234 OIC_LOG(INFO, TAG, "IN SRPSyncDevice");
2235 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
2237 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL parameters");
2238 return OC_STACK_INVALID_PARAM;
2240 if (!resultCallback)
2242 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL Callback");
2243 return OC_STACK_INVALID_CALLBACK;
2246 // Declare variables in here to handle error cases with goto statement.
2247 OCProvisionDev_t* pOwnedDevList = NULL;
2248 OCProvisionDev_t* pLinkedDevList = NULL;
2249 RemoveData_t* removeData = NULL;
2251 //1. Find all devices that has a credential of the revoked device
2252 OCUuidList_t* pLinkedUuidList = NULL;
2253 size_t numOfDevices = 0;
2254 OCStackResult res = OC_STACK_ERROR;
2255 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2256 if (OC_STACK_OK != res)
2258 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to get linked devices information");
2261 // if there is no related device, we can skip further process.
2262 if (0 == numOfDevices)
2264 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No linked device found.");
2265 res = OC_STACK_CONTINUE;
2269 //2. Find owned device from the network
2270 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
2271 if (OC_STACK_OK != res)
2273 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to PMDeviceDiscovery");
2277 //3. Make a list of devices to send DELETE credential request
2278 // by comparing owned devices from provisioning database with mutlicast discovery result.
2279 size_t numOfLinkedDev = 0;
2280 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2281 &pLinkedDevList, &numOfLinkedDev);
2282 if (OC_STACK_OK != res)
2284 OIC_LOG(ERROR, TAG, "SRPSyncDevice : GetListofDevToReqDeleteCred() failed");
2287 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2288 { // So we don't have to send request message.
2289 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No alived & linked device found.");
2290 res = OC_STACK_CONTINUE;
2294 // 4. Prepare RemoveData Context data.
2295 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2298 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to memory allocation");
2299 res = OC_STACK_NO_MEMORY;
2303 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2304 if (!removeData->revokeTargetDev)
2306 OIC_LOG(ERROR, TAG, "SRPSyncDevice : PMCloneOCProvisionDev Failed");
2307 res = OC_STACK_NO_MEMORY;
2311 removeData->removeRes =
2312 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2313 if (!removeData->removeRes)
2315 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to allocate memory");
2316 res = OC_STACK_NO_MEMORY;
2320 removeData->ctx = ctx;
2321 removeData->linkedDevList = pLinkedDevList;
2322 removeData->resultCallback = resultCallback;
2323 removeData->numOfResults = 0;
2324 removeData->sizeOfResArray = numOfLinkedDev;
2325 removeData->hasError = false;
2327 // 5. Send DELETE credential request to linked devices.
2328 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2329 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2330 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2332 res = SendDeleteACLRequest((void*)removeData, &SRPSyncDeviceACLCB,
2333 removeData->revokeTargetDev, curDev);
2334 if (OC_STACK_OK != res)
2336 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE ACL request to\
2337 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2340 res = SendDeleteCredentialRequest((void*)removeData, &SRPSyncDeviceCredCB,
2341 removeData->revokeTargetDev, curDev);
2342 if (OC_STACK_OK != res)
2344 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE credential request to\
2345 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2346 totalRes = OC_STACK_ERROR;
2350 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2354 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2355 PMDeleteDeviceList(pOwnedDevList);
2356 OIC_LOG(INFO, TAG, "OUT SRPSyncDevice");
2358 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2361 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2362 PMDeleteDeviceList(pOwnedDevList);
2363 PMDeleteDeviceList(pLinkedDevList);
2366 OICFree(removeData->revokeTargetDev);
2367 OICFree(removeData->removeRes);
2368 OICFree(removeData);
2370 OIC_LOG(INFO, TAG, "OUT ERROR case SRPSyncDevice");
2375 * Function for remote reset
2376 * This function will send pstat PUT message to the target device to initiate remote reset.
2378 * @param[in] pTargetDev Device information to be revoked.
2379 * @param[in] resultCallback callback provided by API user, callback will be called when
2380 * credential revocation is finished.
2381 * when there is an error, this user callback is called immediately.
2382 * @return OC_STACK_OK in case of success and other value otherwise.
2383 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2384 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2386 OCStackResult SRPResetDevice(const OCProvisionDev_t* pTargetDev,
2387 OCProvisionResultCB resultCallback)
2389 OIC_LOG(INFO, TAG, "IN SRPResetDevice");
2392 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL parameters");
2393 return OC_STACK_INVALID_PARAM;
2395 if (!resultCallback)
2397 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL Callback");
2398 return OC_STACK_INVALID_CALLBACK;
2401 OCStackResult res = OC_STACK_ERROR;
2402 OicSecPstat_t * pstat = (OicSecPstat_t *) OICCalloc(1, sizeof(OicSecPstat_t));
2405 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2406 return OC_STACK_NO_MEMORY;
2410 pstat->isOp = false;
2411 memcpy(pstat->deviceID.id, pTargetDev->doxm->deviceID.id, sizeof(OicUuid_t));
2412 pstat->tm = TAKE_OWNER;
2413 pstat->om = (OicSecDpom_t)(SINGLE_SERVICE_CLIENT_DRIVEN); // the only mode IoTivity supports currently
2415 pstat->sm = (OicSecDpom_t *) OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
2416 if (NULL == pstat->sm)
2418 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2420 return OC_STACK_NO_MEMORY;
2422 pstat->sm[0] = (OicSecDpom_t)(SINGLE_SERVICE_CLIENT_DRIVEN); // the only mode IoTivity supports currently
2424 OCSecurityPayload * secPayload = (OCSecurityPayload *) OICCalloc(1, sizeof(OCSecurityPayload));
2427 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2428 res = OC_STACK_NO_MEMORY;
2431 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2433 if (OC_STACK_OK != PstatToCBORPayload(pstat, &(secPayload->securityData),
2434 &(secPayload->payloadSize), true))
2436 OCPayloadDestroy((OCPayload *) secPayload);
2437 OIC_LOG(ERROR, TAG, "Failed to PstatToCBORPayload");
2438 res = OC_STACK_NO_MEMORY;
2441 OIC_LOG(DEBUG, TAG, "Created payload for pstat set");
2442 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2444 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2445 if (!PMGenerateQuery(true,
2446 pTargetDev->endpoint.addr,
2447 pTargetDev->securePort,
2448 pTargetDev->connType,
2449 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2451 OIC_LOG(ERROR, TAG, "SRPResetDevice : Failed to generate query");
2452 OCPayloadDestroy((OCPayload *) secPayload);
2453 res = OC_STACK_ERROR;
2456 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2458 OCCallbackData cbData = { .context = NULL, .cb = NULL, .cd = NULL };
2459 OCMethod method = OC_REST_POST;
2460 OCDoHandle handle = NULL;
2461 OCProvisionDev_t * targetDev = PMCloneOCProvisionDev(pTargetDev);
2462 if (NULL == targetDev)
2464 OIC_LOG(ERROR, TAG, "target dev is null");
2465 res = OC_STACK_ERROR;
2468 cbData.cb = &SRPResetDeviceCB;
2469 cbData.context = (void *) targetDev;
2471 OIC_LOG(DEBUG, TAG, "Sending PSTAT info to resource server");
2472 res = OCDoResource(&handle, method, query,
2473 &targetDev->endpoint, (OCPayload *)secPayload,
2474 targetDev->connType, OC_LOW_QOS, &cbData, NULL, 0);\
2475 if (OC_STACK_OK != res)
2477 OIC_LOG(ERROR, TAG, "OCStack resource error");
2483 OIC_LOG(INFO, TAG, "OUT SRPResetDevice");
2488 * Internal Function to store results in result array during GetCredResourceCB.
2490 static void registerResultForGetCredResourceCB(GetSecData_t *GetSecData,
2491 OCStackResult stackresult)
2493 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetCredResourceCB "
2494 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2495 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2496 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2497 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2498 ++(GetSecData->numOfResults);
2502 * Callback handler of SRPGetCredResource.
2504 * @param[in] ctx ctx value passed to callback from calling function.
2505 * @param[in] UNUSED handle to an invocation
2506 * @param[in] clientResponse Response from queries to remote servers.
2507 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2508 * and OC_STACK_KEEP_TRANSACTION to keep it.
2510 static OCStackApplicationResult SRPGetCredResourceCB(void *ctx, OCDoHandle UNUSED,
2511 OCClientResponse *clientResponse)
2513 OIC_LOG_V(INFO, TAG, "Inside SRPGetCredResourceCB.");
2515 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2516 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2517 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2521 if(OC_STACK_OK == clientResponse->result)
2523 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2524 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2526 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2528 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2529 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2532 OICFree(GetSecData->resArr);
2533 OICFree(GetSecData);
2535 return OC_STACK_DELETE_TRANSACTION;
2538 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2539 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2542 OIC_LOG_V(ERROR, TAG, "SRPGetCredResourceCB received Null clientResponse");
2543 OICFree(GetSecData->resArr);
2544 OICFree(GetSecData);
2546 return OC_STACK_DELETE_TRANSACTION;
2550 OCStackResult SRPGetCredResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2551 OCProvisionResultCB resultCallback)
2553 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2554 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2556 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2557 if(!PMGenerateQuery(true,
2558 selectedDeviceInfo->endpoint.addr,
2559 selectedDeviceInfo->securePort,
2560 selectedDeviceInfo->connType,
2561 query, sizeof(query), OIC_RSRC_CRED_URI))
2563 OIC_LOG(ERROR, TAG, "SRPGetCredResource : Failed to generate query");
2564 return OC_STACK_ERROR;
2566 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2568 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2569 cbData.cb = &SRPGetCredResourceCB;
2570 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2571 if (NULL == GetSecData)
2573 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2574 return OC_STACK_NO_MEMORY;
2576 GetSecData->deviceInfo = selectedDeviceInfo;
2577 GetSecData->resultCallback = resultCallback;
2578 GetSecData->numOfResults=0;
2579 GetSecData->ctx = ctx;
2581 int noOfRiCalls = 1;
2582 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2583 if (NULL == GetSecData->resArr)
2585 OICFree(GetSecData);
2586 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2587 return OC_STACK_NO_MEMORY;
2589 cbData.context = (void *)GetSecData;
2591 OCMethod method = OC_REST_GET;
2592 OCDoHandle handle = NULL;
2593 OIC_LOG(DEBUG, TAG, "Sending Get Cred to resource server");
2594 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2595 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2596 if (OC_STACK_OK != ret)
2598 OIC_LOG(ERROR, TAG, "OCStack resource error");
2599 OICFree(GetSecData->resArr);
2600 OICFree(GetSecData);
2602 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2603 OIC_LOG(DEBUG, TAG, "OUT SRPGetCredResource");
2609 * Internal Function to store results in result array during GetACLResourceCB.
2611 static void registerResultForGetACLResourceCB(GetSecData_t *GetSecData,
2612 OCStackResult stackresult)
2614 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetACLResourceCB "
2615 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2616 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2617 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2618 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2619 ++(GetSecData->numOfResults);
2623 * Callback handler of SRPGetACLResource.
2625 * @param[in] ctx ctx value passed to callback from calling function.
2626 * @param[in] UNUSED handle to an invocation
2627 * @param[in] clientResponse Response from queries to remote servers.
2628 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2629 * and OC_STACK_KEEP_TRANSACTION to keep it.
2631 static OCStackApplicationResult SRPGetACLResourceCB(void *ctx, OCDoHandle UNUSED,
2632 OCClientResponse *clientResponse)
2634 OIC_LOG_V(INFO, TAG, "Inside SRPGetACLResourceCB.");
2636 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2637 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2638 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2642 if(OC_STACK_OK == clientResponse->result)
2644 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2645 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2647 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2649 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2650 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2653 OICFree(GetSecData->resArr);
2654 OICFree(GetSecData);
2656 return OC_STACK_DELETE_TRANSACTION;
2659 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2660 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2663 OIC_LOG_V(ERROR, TAG, "SRPGetACLResourceCB received Null clientResponse");
2664 OICFree(GetSecData->resArr);
2665 OICFree(GetSecData);
2667 return OC_STACK_DELETE_TRANSACTION;
2671 OCStackResult SRPGetACLResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2672 OCProvisionResultCB resultCallback)
2674 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2675 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2677 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2678 if(!PMGenerateQuery(true,
2679 selectedDeviceInfo->endpoint.addr,
2680 selectedDeviceInfo->securePort,
2681 selectedDeviceInfo->connType,
2682 query, sizeof(query), OIC_RSRC_ACL_URI))
2684 OIC_LOG(ERROR, TAG, "SRPGetACLResource : Failed to generate query");
2685 return OC_STACK_ERROR;
2687 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2689 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2690 cbData.cb = &SRPGetACLResourceCB;
2691 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2692 if (NULL == GetSecData)
2694 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2695 return OC_STACK_NO_MEMORY;
2697 GetSecData->deviceInfo = selectedDeviceInfo;
2698 GetSecData->resultCallback = resultCallback;
2699 GetSecData->numOfResults=0;
2700 GetSecData->ctx = ctx;
2702 int noOfRiCalls = 1;
2703 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2704 if (NULL == GetSecData->resArr)
2706 OICFree(GetSecData);
2707 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2708 return OC_STACK_NO_MEMORY;
2710 cbData.context = (void *)GetSecData;
2712 OCMethod method = OC_REST_GET;
2713 OCDoHandle handle = NULL;
2714 OIC_LOG(DEBUG, TAG, "Sending Get ACL to resource server");
2715 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2716 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2717 if (OC_STACK_OK != ret)
2719 OIC_LOG(ERROR, TAG, "OCStack resource error");
2720 OICFree(GetSecData->resArr);
2721 OICFree(GetSecData);
2723 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2724 OIC_LOG(DEBUG, TAG, "OUT SRPGetACLResource");
2729 OCStackResult SRPReadTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
2732 OIC_LOG(DEBUG, TAG, "IN SRPReadTrustCertChain");
2734 OCStackResult res = OC_STACK_ERROR;
2736 OicSecCred_t* credData = GetCredEntryByCredId(credId);
2739 res = CredToCBORPayload((const OicSecCred_t*) credData, trustCertChain,
2740 chainSize, secureFlag);
2741 if(OC_STACK_OK != res)
2743 OIC_LOG(INFO, TAG, "CredToCBORPayload failed");
2746 DeleteCredList(credData);