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 OCPayloadDestroy((OCPayload *)secPayload);
345 return OC_STACK_ERROR;
347 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
349 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
350 cbData.cb = responseHandler;
351 cbData.context = (void *) credData;
354 OCDoHandle handle = NULL;
355 OCMethod method = OC_REST_POST;
356 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
357 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
358 OIC_LOG_V(INFO, TAG, "OCDoResource::Credential provisioning returned : %d",ret);
359 if (ret != OC_STACK_OK)
361 OIC_LOG(ERROR, TAG, "OCStack resource error");
367 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
369 * Structure to carry certificate data to callback.
371 typedef struct CertificateData CertData_t;
372 struct CertificateData
374 void *ctx; /**< Pointer to user context.**/
375 const OCProvisionDev_t *deviceInfo; /**< Pointer to OCProvisionDev_t.**/
376 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
377 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
378 OCProvisionResult_t *resArr; /**< Result array.**/
379 int numOfResults; /**< Number of results in result array.**/
383 * Internal function to update result in result array.
385 static void registerResultForCertProvisioning(CertData_t *certData,
386 OCStackResult stackresult)
389 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",certData->numOfResults);
390 memcpy(certData->resArr[(certData->numOfResults)].deviceId.id,
391 certData->deviceInfo->doxm->deviceID.id,UUID_LENGTH);
392 certData->resArr[(certData->numOfResults)].res = stackresult;
393 ++(certData->numOfResults);
397 * Internal function for handling credential generation and sending cretificate credential.
399 * @param[in] cred Instance of cred resource.
400 * @param[in] deviceInfo information about device to which credential is to be provisioned.
401 * @param[in] responseHandler callbak called by OC stack when request API receives response.
402 * @return OC_STACK_OK in case of success and other value otherwise.
404 static OCStackResult provisionCertCred(const OicSecCred_t *cred,
405 const OCProvisionDev_t *deviceInfo, CertData_t *certData,
406 OCClientResponseHandler responseHandler)
408 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
411 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
412 return OC_STACK_NO_MEMORY;
414 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
416 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
417 &secPayload->payloadSize, secureFlag);
419 if ((OC_STACK_OK != res) || (NULL == secPayload->securityData))
422 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
423 return OC_STACK_NO_MEMORY;
426 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
427 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
428 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
429 if(!PMGenerateQuery(true,
430 deviceInfo->endpoint.addr,
431 deviceInfo->securePort,
432 deviceInfo->connType,
433 query, sizeof(query), OIC_RSRC_CRED_URI))
435 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
436 OCPayloadDestroy((OCPayload *)secPayload);
437 return OC_STACK_ERROR;
439 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
441 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
442 cbData.cb = responseHandler;
443 cbData.context = (void *) certData;
446 OCDoHandle handle = NULL;
447 OCMethod method = OC_REST_POST;
448 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
449 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
450 OIC_LOG_V(INFO, TAG, "OCDoResource::Certificate provisioning returned : %d",ret);
451 if (ret != OC_STACK_OK)
453 OIC_LOG(ERROR, TAG, "OCStack resource error");
459 OCStackResult SRPRegisterTrustCertChainNotifier(void *ctx, TrustCertChainChangeCB callback)
461 if (g_trustCertChainNotifier.callback)
463 OIC_LOG(ERROR, TAG, "Can't register Notifier, Unregister previous one");
464 return OC_STACK_ERROR;
467 g_trustCertChainNotifier.callback = callback;
468 g_trustCertChainNotifier.context = ctx;
472 void SRPRemoveTrustCertChainNotifier()
474 g_trustCertChainNotifier.callback = NULL;
475 g_trustCertChainNotifier.context = NULL;
480 * Callback handler for handling callback of certificate provisioning device.
482 * @param[in] ctx ctx value passed to callback from calling function.
483 * @param[in] UNUSED handle to an invocation
484 * @param[in] clientResponse Response from queries to remote servers.
485 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
486 * and OC_STACK_KEEP_TRANSACTION to keep it.
488 static OCStackApplicationResult provisionCertCB(void *ctx, OCDoHandle UNUSED,
489 OCClientResponse *clientResponse)
491 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
492 CertData_t *certData = (CertData_t *) ctx;
495 OCProvisionResultCB resultCallback = certData->resultCallback;
496 OIC_LOG(INFO, TAG, "provisionCertCred called");
499 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
501 registerResultForCertProvisioning(certData, OC_STACK_RESOURCE_CHANGED);
502 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
505 OICFree(certData->resArr);
507 return OC_STACK_DELETE_TRANSACTION;
511 OIC_LOG(INFO, TAG, "provisionCertCredCB received Null clientResponse");
512 registerResultForCertProvisioning(certData, OC_STACK_ERROR);
513 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
516 OICFree(certData->resArr);
518 return OC_STACK_DELETE_TRANSACTION;
521 OCStackResult SRPProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint16_t credId,
522 const OCProvisionDev_t *selectedDeviceInfo, OCProvisionResultCB resultCallback)
524 OIC_LOG(INFO, TAG, "In SRPProvisionTrustCertChain");
525 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
526 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
527 if (SIGNED_ASYMMETRIC_KEY != type || NULL == type)
529 OIC_LOG(INFO, TAG, "Invalid key type");
530 return OC_STACK_INVALID_PARAM;
533 OicSecCred_t *trustCertChainCred = GetCredEntryByCredId(credId);
534 if(NULL == trustCertChainCred)
536 OIC_LOG(ERROR, TAG, "Can not find matched Trust Cert. Chain.");
537 return OC_STACK_NO_RESOURCE;
540 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
543 DeleteCredList(trustCertChainCred);
544 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
545 return OC_STACK_NO_MEMORY;
547 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
549 if(OC_STACK_OK != CredToCBORPayload(trustCertChainCred, &secPayload->securityData, &secPayload->payloadSize, secureFlag))
551 DeleteCredList(trustCertChainCred);
552 OCPayloadDestroy((OCPayload *)secPayload);
553 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
554 return OC_STACK_NO_MEMORY;
556 DeleteCredList(trustCertChainCred);
557 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
558 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
560 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
561 if(!PMGenerateQuery(true,
562 selectedDeviceInfo->endpoint.addr,
563 selectedDeviceInfo->securePort,
564 selectedDeviceInfo->connType,
565 query, sizeof(query), OIC_RSRC_CRED_URI))
567 OIC_LOG(ERROR, TAG, "SRPProvisionTrustCertChain : Failed to generate query");
568 OCPayloadDestroy((OCPayload *)secPayload);
569 return OC_STACK_ERROR;
571 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
573 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
574 cbData.cb = &provisionCertCB;
575 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
576 if (NULL == certData)
578 OIC_LOG(ERROR, TAG, "Memory allocation problem");
579 OCPayloadDestroy((OCPayload *)secPayload);
580 return OC_STACK_NO_MEMORY;
582 certData->deviceInfo = selectedDeviceInfo;
583 certData->resultCallback = resultCallback;
584 certData->credInfo = NULL; //credInfo not used in the response handler
585 certData->numOfResults=0;
589 certData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
590 if (certData->resArr == NULL)
593 OCPayloadDestroy((OCPayload *)secPayload);
594 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
595 return OC_STACK_NO_MEMORY;
597 cbData.context = (void *)certData;
599 OCMethod method = OC_REST_POST;
600 OCDoHandle handle = NULL;
601 OIC_LOG(DEBUG, TAG, "Sending Cred info to resource server");
602 OCStackResult ret = OCDoResource(&handle, method, query,
603 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
604 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
605 if (ret != OC_STACK_OK)
607 OICFree(certData->resArr);
611 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
615 OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
616 OicEncodingType_t encodingType, uint16_t *credId)
618 OIC_LOG(DEBUG, TAG, "IN SRPSaveTrustCertChain");
619 VERIFY_NON_NULL(TAG, trustCertChain, ERROR, OC_STACK_INVALID_PARAM);
620 VERIFY_NON_NULL(TAG, credId, ERROR, OC_STACK_INVALID_PARAM);
622 OCStackResult res = OC_STACK_ERROR;
624 OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(*cred));
625 VERIFY_NON_NULL(TAG, cred, ERROR, OC_STACK_NO_MEMORY);
627 memcpy(cred->subject.id, &WILDCARD_SUBJECT_ID, WILDCARD_SUBJECT_ID_LEN);
629 cred->credUsage= (char *)OICCalloc(1, strlen(TRUST_CA)+1 );
630 VERIFY_NON_NULL(TAG, cred->credUsage, ERROR, OC_STACK_NO_MEMORY);
631 OICStrcpy(cred->credUsage, strlen(TRUST_CA) + 1, TRUST_CA) ;
633 cred->credType = SIGNED_ASYMMETRIC_KEY;
635 if (encodingType == OIC_ENCODING_PEM)
637 cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize + 1);
638 VERIFY_NON_NULL(TAG, cred->optionalData.data, ERROR, OC_STACK_NO_MEMORY);
639 cred->optionalData.len = chainSize + 1;
643 cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize);
644 VERIFY_NON_NULL(TAG, cred->optionalData.data, ERROR, OC_STACK_NO_MEMORY);
645 cred->optionalData.len = chainSize;
647 memcpy(cred->optionalData.data, trustCertChain, chainSize);
648 cred->optionalData.encoding = encodingType;
650 res = AddCredential(cred);
651 if(res != OC_STACK_OK)
653 DeleteCredList(cred);
656 *credId = cred->credId;
658 if (g_trustCertChainNotifier.callback)
660 uint8_t *certChain = (uint8_t*)OICCalloc(1, sizeof(uint8_t) * chainSize);
661 VERIFY_NON_NULL(TAG, certChain, ERROR, OC_STACK_NO_MEMORY);
662 memcpy(certChain, trustCertChain, chainSize);
663 g_trustCertChainNotifier.callback(g_trustCertChainNotifier.context, credId,
664 certChain, chainSize);
668 OIC_LOG(DEBUG, TAG, "OUT SRPSaveTrustCertChain");
674 OCStackResult SRPSaveOwnCertChain(OicSecCert_t * cert, OicSecKey_t * key, uint16_t *credId)
676 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
677 VERIFY_NON_NULL(TAG, cert, ERROR, OC_STACK_INVALID_PARAM);
678 VERIFY_NON_NULL(TAG, cert->data, ERROR, OC_STACK_INVALID_PARAM);
679 VERIFY_NON_NULL(TAG, key, ERROR, OC_STACK_INVALID_PARAM);
680 VERIFY_NON_NULL(TAG, key->data, ERROR, OC_STACK_INVALID_PARAM);
681 VERIFY_NON_NULL(TAG, credId, ERROR, OC_STACK_INVALID_PARAM);
683 OCStackResult res = OC_STACK_ERROR;
685 OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(*cred));
686 VERIFY_NON_NULL(TAG, cred, ERROR, OC_STACK_NO_MEMORY);
688 OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
690 if (OC_STACK_OK != GetDoxmDeviceID(&cred->subject))
692 OIC_LOG(ERROR, TAG, "Cann't get the device id(GetDoxmDeviceID)");
695 cred->credUsage= (char *)OICCalloc(1, strlen(PRIMARY_CERT)+1 );
696 VERIFY_NON_NULL(TAG, cred->credUsage, ERROR, OC_STACK_NO_MEMORY);
697 OICStrcpy(cred->credUsage, strlen(PRIMARY_CERT) + 1, PRIMARY_CERT) ;
699 cred->credType = SIGNED_ASYMMETRIC_KEY;
701 OicSecCert_t *publicData = &cred->publicData;
702 publicData->data = (uint8_t *)OICCalloc(1, cert->len);
703 VERIFY_NON_NULL(TAG, publicData->data, ERROR, OC_STACK_NO_MEMORY);
704 memcpy(publicData->data, cert->data, cert->len);
705 publicData->len = cert->len;
707 OicSecKey_t *privateData = &cred->privateData;
708 privateData->data = (uint8_t *)OICCalloc(1, key->len);
709 VERIFY_NON_NULL(TAG, privateData->data, ERROR, OC_STACK_NO_MEMORY);
710 memcpy(privateData->data, key->data, key->len);
711 privateData->len = key->len;
712 privateData->encoding = key->encoding;
714 res = AddCredential(cred);
715 if(res != OC_STACK_OK)
717 DeleteCredList(cred);
720 *credId = cred->credId;
722 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
726 #endif // __WITH_DTLS__ || __WITH_TLS__
728 OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
729 const OCProvisionDev_t *pDev1,
730 const OCProvisionDev_t *pDev2,
731 OCProvisionResultCB resultCallback)
733 VERIFY_NON_NULL(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM);
734 if (SYMMETRIC_PAIR_WISE_KEY == type)
736 VERIFY_NON_NULL(TAG, pDev2, ERROR, OC_STACK_INVALID_PARAM);
740 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
741 return OC_STACK_INVALID_CALLBACK;
743 if (SYMMETRIC_PAIR_WISE_KEY == type &&
744 0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
746 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
747 return OC_STACK_INVALID_PARAM;
750 if (SYMMETRIC_PAIR_WISE_KEY == type &&
751 !(OWNER_PSK_LENGTH_128 == keySize || OWNER_PSK_LENGTH_256 == keySize))
753 OIC_LOG(INFO, TAG, "Invalid key size");
754 return OC_STACK_INVALID_PARAM;
757 OIC_LOG(INFO, TAG, "In SRPProvisionCredentials");
759 if (SYMMETRIC_PAIR_WISE_KEY == type)
761 bool linkExisits = true;
762 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExisits);
764 if (res != OC_STACK_OK)
766 OIC_LOG(ERROR, TAG, "Internal error occured");
771 OIC_LOG(ERROR, TAG, "Link already exists");
772 return OC_STACK_INVALID_PARAM;
776 OicUuid_t provTooldeviceID = {{0,}};
777 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
779 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
780 return OC_STACK_ERROR;
782 OIC_LOG(INFO, TAG, "retrieved deviceid");
785 case SYMMETRIC_PAIR_WISE_KEY:
787 const OCProvisionDev_t *firstDevice = pDev1;
788 const OCProvisionDev_t *secondDevice = pDev2;
790 OicSecCred_t *firstCred = NULL;
791 OicSecCred_t *secondCred = NULL;
792 OCStackResult res = PMGeneratePairWiseCredentials(type, keySize, &provTooldeviceID,
793 &firstDevice->doxm->deviceID, &secondDevice->doxm->deviceID,
794 &firstCred, &secondCred);
795 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
796 OIC_LOG(INFO, TAG, "Credentials generated successfully");
797 CredentialData_t *credData =
798 (CredentialData_t *) OICCalloc(1, sizeof(CredentialData_t));
799 if (NULL == credData)
803 OIC_LOG(ERROR, TAG, "Memory allocation problem");
804 return OC_STACK_NO_MEMORY;
806 credData->deviceInfo1 = firstDevice;
807 credData->deviceInfo2 = secondDevice;
808 credData->credInfo = secondCred;
810 credData->credInfoFirst = firstCred;
811 credData->numOfResults = 0;
812 credData->resultCallback = resultCallback;
813 // first call to provision creds to device1.
814 // second call to provision creds to device2.
817 (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
818 if (NULL == credData->resArr)
823 OIC_LOG(ERROR, TAG, "Memory allocation problem");
824 return OC_STACK_NO_MEMORY;
826 res = provisionCredentials(firstCred, firstDevice, credData, &provisionCredentialCB1);
827 if (OC_STACK_OK != res)
829 DeleteCredList(firstCred);
830 DeleteCredList(secondCred);
831 OICFree(credData->resArr);
834 OIC_LOG_V(INFO, TAG, "provisionCredentials returned: %d",res);
835 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
840 OIC_LOG(ERROR, TAG, "Invalid option.");
841 return OC_STACK_INVALID_PARAM;
844 return OC_STACK_ERROR;
848 * Internal Function to store results in result array during ACL provisioning.
850 static void registerResultForACLProvisioning(ACLData_t *aclData,
851 OCStackResult stackresult)
853 OIC_LOG_V(INFO, TAG, "Inside registerResultForACLProvisioning aclData->numOfResults is %d\n",
854 aclData->numOfResults);
855 memcpy(aclData->resArr[(aclData->numOfResults)].deviceId.id,
856 aclData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
857 aclData->resArr[(aclData->numOfResults)].res = stackresult;
858 ++(aclData->numOfResults);
862 * Callback handler of SRPProvisionACL.
864 * @param[in] ctx ctx value passed to callback from calling function.
865 * @param[in] UNUSED handle to an invocation
866 * @param[in] clientResponse Response from queries to remote servers.
867 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
868 * and OC_STACK_KEEP_TRANSACTION to keep it.
870 static OCStackApplicationResult SRPProvisionACLCB(void *ctx, OCDoHandle UNUSED,
871 OCClientResponse *clientResponse)
873 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionACLCB.");
875 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
876 ACLData_t *aclData = (ACLData_t*)ctx;
877 OCProvisionResultCB resultCallback = aclData->resultCallback;
881 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
883 registerResultForACLProvisioning(aclData, OC_STACK_RESOURCE_CHANGED);
884 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
887 OICFree(aclData->resArr);
889 return OC_STACK_DELETE_TRANSACTION;
892 registerResultForACLProvisioning(aclData, OC_STACK_ERROR);
893 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
896 OIC_LOG_V(ERROR, TAG, "SRPProvisionACLCB received Null clientResponse");
897 OICFree(aclData->resArr);
899 return OC_STACK_DELETE_TRANSACTION;
902 OCStackResult SRPProvisionACL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
903 OicSecAcl_t *acl, OCProvisionResultCB resultCallback)
905 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
906 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
907 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
909 // if rowneruuid is empty, set it to device ID
910 OicUuid_t emptyOwner = {.id = {0} };
911 if (memcmp(&(acl->rownerID.id), &emptyOwner, UUID_IDENTITY_SIZE) == 0)
913 OIC_LOG(DEBUG, TAG, "Set Rowner to PT's deviceId, because Rowner of ACL is empty");
916 if (OC_STACK_OK == GetDoxmDeviceID(&oicUuid))
918 memcpy(&(acl->rownerID.id), &oicUuid, UUID_IDENTITY_SIZE);
922 OIC_LOG(DEBUG, TAG, "Failed to set Rowner to PT's deviceID\
923 becuase it failed to retrieve Doxm DeviceID");
924 return OC_STACK_ERROR;
928 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
931 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
932 return OC_STACK_NO_MEMORY;
934 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
935 if(OC_STACK_OK != AclToCBORPayload(acl, &secPayload->securityData, &secPayload->payloadSize))
937 OCPayloadDestroy((OCPayload *)secPayload);
938 OIC_LOG(ERROR, TAG, "Failed to AclToCBORPayload");
939 return OC_STACK_NO_MEMORY;
941 OIC_LOG(DEBUG, TAG, "Created payload for ACL:");
942 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
944 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
945 if(!PMGenerateQuery(true,
946 selectedDeviceInfo->endpoint.addr,
947 selectedDeviceInfo->securePort,
948 selectedDeviceInfo->connType,
949 query, sizeof(query), OIC_RSRC_ACL_URI))
951 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
952 return OC_STACK_ERROR;
954 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
956 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
957 cbData.cb = &SRPProvisionACLCB;
958 ACLData_t *aclData = (ACLData_t *) OICCalloc(1, sizeof(ACLData_t));
961 OCPayloadDestroy((OCPayload *)secPayload);
962 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
963 return OC_STACK_NO_MEMORY;
965 aclData->deviceInfo = selectedDeviceInfo;
966 aclData->resultCallback = resultCallback;
967 aclData->numOfResults=0;
969 // call to provision ACL to device1.
971 aclData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
972 if (aclData->resArr == NULL)
975 OCPayloadDestroy((OCPayload *)secPayload);
976 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
977 return OC_STACK_NO_MEMORY;
979 cbData.context = (void *)aclData;
981 OCMethod method = OC_REST_POST;
982 OCDoHandle handle = NULL;
983 OIC_LOG(DEBUG, TAG, "Sending ACL info to resource server");
984 OCStackResult ret = OCDoResource(&handle, method, query,
985 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
986 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
987 if (ret != OC_STACK_OK)
989 OICFree(aclData->resArr);
992 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
996 OCStackResult SRPSaveACL(const OicSecAcl_t *acl)
998 OIC_LOG(DEBUG, TAG, "IN SRPSaveACL");
999 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
1001 OCStackResult res = InstallACL(acl);
1003 OIC_LOG(DEBUG, TAG, "OUT SRPSaveACL");
1008 * Internal Function to store results in result array during Direct-Pairing provisioning.
1010 static void registerResultForDirectPairingProvisioning(PconfData_t *pconfData,
1011 OCStackResult stackresult)
1013 OIC_LOG_V(INFO, TAG, "Inside registerResultForDirectPairingProvisioning "
1014 "pconfData->numOfResults is %d\n", pconfData->numOfResults);
1015 memcpy(pconfData->resArr[(pconfData->numOfResults)].deviceId.id,
1016 pconfData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
1017 pconfData->resArr[(pconfData->numOfResults)].res = stackresult;
1018 ++(pconfData->numOfResults);
1022 * Callback handler of SRPProvisionDirectPairing.
1024 * @param[in] ctx ctx value passed to callback from calling function.
1025 * @param[in] UNUSED handle to an invocation
1026 * @param[in] clientResponse Response from queries to remote servers.
1027 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1028 * and OC_STACK_KEEP_TRANSACTION to keep it.
1030 static OCStackApplicationResult SRPProvisionDirectPairingCB(void *ctx, OCDoHandle UNUSED,
1031 OCClientResponse *clientResponse)
1033 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionDirectPairingCB.");
1035 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
1036 PconfData_t *pconfData = (PconfData_t*)ctx;
1037 OCProvisionResultCB resultCallback = pconfData->resultCallback;
1041 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
1043 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_OK);
1044 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
1047 OICFree(pconfData->resArr);
1049 return OC_STACK_DELETE_TRANSACTION;
1052 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_ERROR);
1053 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
1056 OIC_LOG_V(ERROR, TAG, "SRPProvisionDirectPairingCB received Null clientResponse");
1057 OICFree(pconfData->resArr);
1059 return OC_STACK_DELETE_TRANSACTION;
1062 OCStackResult SRPProvisionDirectPairing(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
1063 OicSecPconf_t *pconf, OCProvisionResultCB resultCallback)
1065 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
1066 VERIFY_NON_NULL(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
1067 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
1069 // check direct-pairing capability
1070 if (true != selectedDeviceInfo->doxm->dpc)
1072 OIC_LOG(ERROR, TAG, "Resouce server does not have Direct-Pairing Capability ");
1073 return OC_STACK_UNAUTHORIZED_REQ;
1076 OicUuid_t provTooldeviceID = {.id={0}};
1077 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1079 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1080 return OC_STACK_ERROR;
1082 memcpy(&pconf->rownerID, &provTooldeviceID, sizeof(OicUuid_t));
1084 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
1087 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
1088 return OC_STACK_NO_MEMORY;
1090 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1092 if (OC_STACK_OK != PconfToCBORPayload(pconf, &(secPayload->securityData),
1093 &(secPayload->payloadSize)))
1095 OCPayloadDestroy((OCPayload*)secPayload);
1096 OIC_LOG(ERROR, TAG, "Failed to PconfToCborPayload");
1097 return OC_STACK_NO_MEMORY;
1099 OIC_LOG(DEBUG, TAG, "Created payload for pconf set");
1100 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
1103 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1104 if(!PMGenerateQuery(true,
1105 selectedDeviceInfo->endpoint.addr,
1106 selectedDeviceInfo->securePort,
1107 selectedDeviceInfo->connType,
1108 query, sizeof(query), OIC_RSRC_PCONF_URI))
1110 OIC_LOG(ERROR, TAG, "SRPProvisionDirectPairing : Failed to generate query");
1111 return OC_STACK_ERROR;
1113 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1115 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1116 cbData.cb = &SRPProvisionDirectPairingCB;
1117 PconfData_t *pconfData = (PconfData_t *) OICCalloc(1, sizeof(PconfData_t));
1118 if (NULL == pconfData)
1120 OCPayloadDestroy((OCPayload*)secPayload);
1121 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1122 return OC_STACK_NO_MEMORY;
1124 pconfData->deviceInfo = selectedDeviceInfo;
1125 pconfData->resultCallback = resultCallback;
1126 pconfData->numOfResults=0;
1127 pconfData->ctx = ctx;
1128 // call to provision PCONF to device1.
1129 int noOfRiCalls = 1;
1130 pconfData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1131 if (NULL == pconfData->resArr)
1134 OCPayloadDestroy((OCPayload*)secPayload);
1135 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1136 return OC_STACK_NO_MEMORY;
1138 cbData.context = (void *)pconfData;
1140 OCMethod method = OC_REST_POST;
1141 OCDoHandle handle = NULL;
1142 OIC_LOG(DEBUG, TAG, "Sending PCONF info to resource server");
1143 OCStackResult ret = OCDoResource(&handle, method, query,
1144 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1145 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1146 if (OC_STACK_OK != ret)
1148 OICFree(pconfData->resArr);
1151 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1155 static void DeleteUnlinkData_t(UnlinkData_t *unlinkData)
1159 OICFree(unlinkData->unlinkDev);
1160 OICFree(unlinkData->unlinkRes);
1161 OICFree(unlinkData);
1165 static void registerResultForUnlinkDevices(UnlinkData_t *unlinkData, OCStackResult stackresult,
1168 if (NULL != unlinkData)
1170 OIC_LOG_V(INFO, TAG, "Inside registerResultForUnlinkDevices unlinkData->numOfResults is %d\n",
1171 unlinkData->numOfResults);
1172 OIC_LOG_V(INFO, TAG, "Stack result :: %d", stackresult);
1174 OicUuid_t *pUuid = &unlinkData->unlinkRes[(unlinkData->numOfResults)].deviceId;
1176 // Set result in the result array according to the position (devNum).
1177 if (idx != IDX_DB_UPDATE_RES)
1179 memcpy(pUuid->id, unlinkData->unlinkDev[idx].doxm->deviceID.id, sizeof(pUuid->id));
1182 { // When deivce ID is 000... this means it's the result of database update.
1183 memset(pUuid->id, 0, sizeof(pUuid->id));
1185 unlinkData->unlinkRes[(unlinkData->numOfResults)].res = stackresult;
1186 ++(unlinkData->numOfResults);
1187 OIC_LOG (INFO, TAG, "Out registerResultForUnlinkDevices");
1191 static OCStackResult SendDeleteCredentialRequest(void* ctx,
1192 OCClientResponseHandler respHandler,
1193 const OCProvisionDev_t* revokedDev,
1194 const OCProvisionDev_t* destDev)
1196 OIC_LOG(DEBUG, TAG, "IN SendDeleteCredentialRequest");
1198 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1200 return OC_STACK_INVALID_PARAM;
1204 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1205 if(OC_STACK_OK != ret)
1207 OIC_LOG(ERROR, TAG, "SendDeleteCredentialRequest : Failed to canonical UUID encoding");
1208 return OC_STACK_ERROR;
1211 char addressEncoded[CA_MAX_URI_LENGTH] = {0};
1212 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
1213 sizeof(addressEncoded),
1214 destDev->endpoint.addr);
1215 if (OC_STACK_OK != result)
1217 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : encoding error %d\n", result);
1218 return OC_STACK_ERROR;
1221 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1223 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1224 char *srpUri = SRP_FORM_DELETE_CREDENTIAL;
1226 if(CA_ADAPTER_TCP == destDev->endpoint.adapter)
1228 srpUri = SRP_FORM_DELETE_CREDENTIAL_TCP;
1232 snRet = snprintf(reqBuf, sizeof(reqBuf), srpUri, addressEncoded,
1233 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1237 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1238 return OC_STACK_ERROR;
1240 else if ((size_t)snRet >= sizeof(reqBuf))
1242 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1243 return OC_STACK_ERROR;
1246 OCCallbackData cbData;
1247 memset(&cbData, 0, sizeof(cbData));
1248 cbData.context = ctx;
1249 cbData.cb = respHandler;
1251 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1253 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1255 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1256 &destDev->endpoint, NULL,
1257 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1258 if (OC_STACK_OK != ret)
1260 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1262 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1267 static OCStackResult SendDeleteACLRequest(void* ctx,
1268 OCClientResponseHandler respHandler,
1269 const OCProvisionDev_t* revokedDev,
1270 const OCProvisionDev_t* destDev)
1272 OIC_LOG(DEBUG, TAG, "IN SendDeleteACLRequest");
1274 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1276 return OC_STACK_INVALID_PARAM;
1280 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1281 if(OC_STACK_OK != ret)
1283 OIC_LOG(ERROR, TAG, "SendDeleteACLRequest : Failed to canonical UUID encoding");
1284 return OC_STACK_ERROR;
1287 char addressEncoded[CA_MAX_URI_LENGTH] = {0};
1288 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
1289 sizeof(addressEncoded),
1290 destDev->endpoint.addr);
1291 if (OC_STACK_OK != result)
1293 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : encoding error %d\n", result);
1294 return OC_STACK_ERROR;
1298 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1300 //coaps://0.0.0.0:5684/oic/sec/acl?subjectuuid=(Canonical ENCODED UUID)
1301 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, addressEncoded,
1302 destDev->securePort, OIC_RSRC_ACL_URI, OIC_JSON_SUBJECTID_NAME, subID);
1306 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error (snprintf) %d\n", snRet);
1307 return OC_STACK_ERROR;
1309 else if ((size_t)snRet >= sizeof(reqBuf))
1311 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Truncated (snprintf) %d\n", snRet);
1312 return OC_STACK_ERROR;
1315 OCCallbackData cbData;
1316 memset(&cbData, 0, sizeof(cbData));
1317 cbData.context = ctx;
1318 cbData.cb = respHandler;
1320 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1322 OIC_LOG(DEBUG, TAG, "Sending remove ACL request to resource server");
1324 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1325 &destDev->endpoint, NULL,
1326 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1327 if (OC_STACK_OK != ret)
1329 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error in OCDoResource %d", ret);
1331 OIC_LOG(DEBUG, TAG, "OUT SendDeleteACLRequest");
1337 * Callback handler of unlink second device.
1339 * @param[in] ctx ctx value passed to callback from calling function.
1340 * @param[in] handle handle to an invocation
1341 * @param[in] clientResponse Response from queries to remote servers.
1342 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1343 * OC_STACK_KEEP_TRANSACTION to keep it.
1345 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1346 OCClientResponse *clientResponse)
1349 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1350 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1351 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1355 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1356 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1358 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1360 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1364 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1365 unlinkData->resultCallback(unlinkData->ctx,
1366 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1372 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1373 IDX_SECOND_DEVICE_RES);
1374 unlinkData->resultCallback(unlinkData->ctx,
1375 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1376 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1380 //Update provisioning DB when succes case.
1381 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1382 &unlinkData->unlinkDev[1].doxm->deviceID))
1384 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1385 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1386 unlinkData->resultCallback(unlinkData->ctx,
1387 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1390 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1394 DeleteUnlinkData_t(unlinkData);
1395 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1396 return OC_STACK_DELETE_TRANSACTION;
1401 * Callback handler of unlink first device.
1403 * @param[in] ctx ctx value passed to callback from calling function.
1404 * @param[in] handle handle to an invocation
1405 * @param[in] clientResponse Response from queries to remote servers.
1406 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1407 * OC_STACK_KEEP_TRANSACTION to keep it.
1409 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1410 OCClientResponse *clientResponse)
1412 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1413 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1414 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1419 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1420 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1422 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1424 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1426 // Second revocation request to second device.
1427 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1428 &unlinkData->unlinkDev[0],
1429 &unlinkData->unlinkDev[1] /*Dest*/);
1430 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1431 if (OC_STACK_OK != res)
1433 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1434 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1435 IDX_SECOND_DEVICE_RES);
1436 unlinkData->resultCallback(unlinkData->ctx,
1437 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1442 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1443 return OC_STACK_DELETE_TRANSACTION;
1448 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1450 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1451 unlinkData->unlinkRes, true);
1457 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1458 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1459 IDX_FIRST_DEVICE_RES );
1460 unlinkData->resultCallback(unlinkData->ctx,
1461 unlinkData->numOfResults, unlinkData->unlinkRes,
1463 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1467 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1468 DeleteUnlinkData_t(unlinkData);
1469 return OC_STACK_DELETE_TRANSACTION;
1473 * Function to unlink devices.
1474 * This function will remove the credential & relationship between the two devices.
1476 * @param[in] ctx Application context would be returned in result callback
1477 * @param[in] pTargetDev1 first device information to be unlinked.
1478 * @param[in] pTargetDev2 second device information to be unlinked.
1479 * @param[in] resultCallback callback provided by API user, callback will be called when
1480 * device unlink is finished.
1481 * @return OC_STACK_OK in case of success and other value otherwise.
1483 OCStackResult SRPUnlinkDevices(void* ctx,
1484 const OCProvisionDev_t* pTargetDev1,
1485 const OCProvisionDev_t* pTargetDev2,
1486 OCProvisionResultCB resultCallback)
1488 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1490 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1492 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1493 return OC_STACK_INVALID_PARAM;
1495 if (!resultCallback)
1497 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1498 return OC_STACK_INVALID_CALLBACK;
1500 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1502 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1503 return OC_STACK_INVALID_PARAM;
1506 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1507 PMPrintOCProvisionDev(pTargetDev1);
1508 PMPrintOCProvisionDev(pTargetDev2);
1510 // Mark the link status stale
1511 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1512 if (OC_STACK_OK != res)
1514 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1518 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1519 VERIFY_NON_NULL(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1521 //Initialize unlink data
1522 unlinkData->ctx = ctx;
1523 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1524 if (NULL == unlinkData->unlinkDev)
1526 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1527 res = OC_STACK_NO_MEMORY;
1531 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1532 if (NULL == unlinkData->unlinkRes)
1534 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1535 res = OC_STACK_NO_MEMORY;
1539 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1540 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1542 unlinkData->numOfResults = 0;
1543 unlinkData->resultCallback = resultCallback;
1545 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1546 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1547 if (OC_STACK_OK != res)
1549 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1556 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1557 DeleteUnlinkData_t(unlinkData);
1561 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1565 OICFree(pRemoveData->revokeTargetDev);
1566 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1567 OICFree(pRemoveData->removeRes);
1568 OICFree(pRemoveData);
1572 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1573 OCStackResult stackresult, bool hasError)
1575 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1576 removeData->numOfResults + 1);
1579 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1580 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1584 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1585 0, sizeof(pLinkedDevId->id) );
1587 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1588 removeData->hasError = hasError;
1589 ++(removeData->numOfResults);
1591 // If we get suffcient result from linked devices, we have to call user callback and do free
1592 if (removeData->sizeOfResArray == removeData->numOfResults)
1594 if(!removeData->hasError)
1596 // Remove device info from prvisioning database
1597 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1599 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1600 removeData->hasError = true;
1603 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1604 removeData->hasError);
1605 DeleteRemoveData_t(removeData);
1609 static void registerResultForResetDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1610 OCStackResult stackresult, bool hasError)
1612 OIC_LOG_V(INFO, TAG, "Inside registerResultForResetDevice removeData->numOfResults is %zu\n",
1613 removeData->numOfResults + 1);
1616 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1617 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1621 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1622 0, sizeof(pLinkedDevId->id) );
1624 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1625 removeData->hasError = hasError;
1626 ++(removeData->numOfResults);
1628 // If we get suffcient result from linked devices, we have to call user callback and do free
1629 if (removeData->sizeOfResArray == removeData->numOfResults)
1631 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1632 removeData->hasError);
1633 DeleteRemoveData_t(removeData);
1638 * Callback handler of unlink first device.
1640 * @param[in] ctx ctx value passed to callback from calling function.
1641 * @param[in] handle handle to an invocation
1642 * @param[in] clientResponse Response from queries to remote servers.
1643 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1644 * and OC_STACK_KEEP_TRANSACTION to keep it.
1646 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1647 OCClientResponse *clientResponse)
1649 //Update the delete credential into delete device context
1650 //Save the deleted status in delDevCtx
1652 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1653 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1654 OCStackResult res = OC_STACK_ERROR;
1656 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1660 OicUuid_t revDevUuid = {.id={0}};
1661 if(UUID_LENGTH == clientResponse->identity.id_length)
1663 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1664 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1666 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1667 if (OC_STACK_OK != res)
1669 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1670 registerResultForRemoveDevice(removeData, &revDevUuid,
1671 OC_STACK_INCONSISTENT_DB, true);
1673 return OC_STACK_DELETE_TRANSACTION;
1676 registerResultForRemoveDevice(removeData, &revDevUuid,
1677 OC_STACK_RESOURCE_DELETED, false);
1681 registerResultForRemoveDevice(removeData, &revDevUuid,
1682 clientResponse->result, true);
1683 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1688 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1689 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1691 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1694 * Since server's credential was deleted,
1695 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1697 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1698 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1702 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1708 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1709 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1713 return OC_STACK_DELETE_TRANSACTION;
1717 * Callback handler of reset device.
1719 * @param[in] ctx ctx value passed to callback from calling function.
1720 * @param[in] handle handle to an invocation
1721 * @param[in] clientResponse Response from queries to remote servers.
1722 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1723 * and OC_STACK_KEEP_TRANSACTION to keep it.
1725 static OCStackApplicationResult SRPSyncDeviceCredCB(void *delDevCtx, OCDoHandle handle,
1726 OCClientResponse *clientResponse)
1728 //Update the delete credential into delete device context
1729 //Save the deleted status in delDevCtx
1731 OIC_LOG_V(INFO, TAG, "Inside SRPSyncDeviceCredCB.");
1732 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1733 OCStackResult res = OC_STACK_ERROR;
1735 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1736 OCProvisionDev_t * pTargetDev = PMCloneOCProvisionDev(removeData->revokeTargetDev);
1737 OCProvisionResultCB resultCallback = removeData->resultCallback;
1740 OicUuid_t revDevUuid = {.id={0}};
1741 if(UUID_LENGTH == clientResponse->identity.id_length)
1743 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1744 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1746 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1747 if (OC_STACK_OK != res)
1749 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1750 registerResultForResetDevice(removeData, &revDevUuid,
1751 OC_STACK_INCONSISTENT_DB, true);
1753 return OC_STACK_DELETE_TRANSACTION;
1756 registerResultForResetDevice(removeData, &revDevUuid,
1757 OC_STACK_RESOURCE_DELETED, false);
1761 registerResultForResetDevice(removeData, &revDevUuid,
1762 clientResponse->result, false);
1763 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1768 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1769 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1771 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1774 * Since server's credential was deleted,
1775 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1777 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1778 registerResultForResetDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1782 registerResultForResetDevice(removeData, NULL, clientResponse->result, true);
1788 registerResultForResetDevice(removeData, NULL, OC_STACK_ERROR, true);
1789 OIC_LOG(ERROR, TAG, "SRPSyncDevice received Null clientResponse");
1792 SRPResetDevice(pTargetDev, resultCallback);
1794 return OC_STACK_DELETE_TRANSACTION;
1798 * Callback handler of reset device sync-up
1800 * @param[in] ctx ctx value passed to callback from calling function.
1801 * @param[in] handle handle to an invocation
1802 * @param[in] clientResponse Response from queries to remote servers.
1803 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1804 * and OC_STACK_KEEP_TRANSACTION to keep it.
1806 static OCStackApplicationResult SRPSyncDeviceACLCB(void *ctx, OCDoHandle handle,
1807 OCClientResponse *clientResponse)
1809 return OC_STACK_DELETE_TRANSACTION;
1813 * Callback handler of device remote reset.
1815 * @param[in] ctx ctx value passed to callback from calling function.
1816 * @param[in] handle handle to an invocation
1817 * @param[in] clientResponse Response from queries to remote servers.
1818 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1819 * and OC_STACK_KEEP_TRANSACTION to keep it.
1821 static OCStackApplicationResult SRPResetDeviceCB(void *ctx, OCDoHandle handle,
1822 OCClientResponse *clientResponse)
1824 OIC_LOG(DEBUG, TAG, "IN SRPResetDeviceCB");
1825 if(OC_STACK_OK == clientResponse->result)
1827 OIC_LOG(DEBUG, TAG, "Change Target Device Pstat Cm SUCCEEDED");
1830 // Delete Cred and ACL related to the target device.
1831 const OicSecCred_t *cred = NULL;
1832 OCProvisionDev_t * pTargetDev = (OCProvisionDev_t *)ctx;
1833 cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
1836 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to get credential of target device.");
1840 OCStackResult res = RemoveCredential(&cred->subject);
1841 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1843 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove credential.");
1847 res = RemoveACE(&cred->subject, NULL);
1848 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1850 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove ACL.");
1853 if (OC_STACK_OK != PDMDeleteDevice(&pTargetDev->doxm->deviceID))
1855 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to delete device from PDM");
1858 //Close the DTLS session of the reset device.
1859 CAEndpoint_t* endpoint = (CAEndpoint_t *)&clientResponse->devAddr;
1860 CAResult_t caResult = CAcloseSslSession(endpoint);
1861 if(CA_STATUS_OK != caResult)
1863 OIC_LOG_V(WARNING, TAG, "OCResetDevice : Failed to close DTLS session : %d", caResult);
1867 * If there is no linked device, PM does not send any request.
1868 * So we should directly invoke the result callback to inform the result of OCResetDevice.
1870 if(OC_STACK_NO_RESOURCE == res)
1876 OICFree(pTargetDev);
1877 return OC_STACK_DELETE_TRANSACTION;
1881 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1882 OCProvisionDev_t* pOwnedDevList,
1883 OCUuidList_t* pLinkedUuidList,
1884 OCProvisionDev_t** ppLinkedDevList,
1885 size_t *numOfLinkedDev)
1887 // pOwnedDevList could be NULL. It means no alived and owned device now.
1888 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1889 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1891 return OC_STACK_INVALID_PARAM;
1895 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1896 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1898 // Mark the link status stale.
1899 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1900 if (OC_STACK_OK != res)
1902 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1903 return OC_STACK_INCONSISTENT_DB;
1908 // If this linked device is alive (power-on), add the deivce to the list.
1909 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1910 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1912 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1914 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1915 if (NULL == targetDev)
1917 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1918 return OC_STACK_NO_MEMORY;
1921 LL_PREPEND(*ppLinkedDevList, targetDev);
1928 *numOfLinkedDev = cnt;
1933 * Function to device revocation
1934 * This function will remove credential of target device from all devices in subnet.
1936 * @param[in] ctx Application context would be returned in result callback
1937 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1938 * @param[in] pTargetDev Device information to be revoked.
1939 * @param[in] resultCallback callback provided by API user, callback will be called when
1940 * credential revocation is finished.
1941 * @return OC_STACK_OK in case of success and other value otherwise.
1942 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1943 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1945 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1946 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1948 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1950 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1952 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1953 return OC_STACK_INVALID_PARAM;
1955 if (!resultCallback)
1957 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
1958 return OC_STACK_INVALID_CALLBACK;
1961 // Declare variables in here to handle error cases with goto statement.
1962 OCProvisionDev_t* pOwnedDevList = NULL;
1963 OCProvisionDev_t* pLinkedDevList = NULL;
1964 RemoveData_t* removeData = NULL;
1966 //1. Find all devices that has a credential of the revoked device
1967 OCUuidList_t* pLinkedUuidList = NULL;
1968 size_t numOfDevices = 0;
1969 OCStackResult res = OC_STACK_ERROR;
1970 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
1971 if (OC_STACK_OK != res)
1973 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
1976 // if there is no related device, we can skip further process.
1977 if (0 == numOfDevices)
1979 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
1980 res = OC_STACK_CONTINUE;
1984 //2. Find owned device from the network
1985 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
1986 if (OC_STACK_OK != res)
1988 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
1992 //3. Make a list of devices to send DELETE credential request
1993 // by comparing owned devices from provisioning database with mutlicast discovery result.
1994 size_t numOfLinkedDev = 0;
1995 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
1996 &pLinkedDevList, &numOfLinkedDev);
1997 if (OC_STACK_OK != res)
1999 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
2002 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2003 { // So we don't have to send request message.
2004 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
2005 res = OC_STACK_CONTINUE;
2009 // 4. Prepare RemoveData Context data.
2010 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2013 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
2014 res = OC_STACK_NO_MEMORY;
2018 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2019 if (!removeData->revokeTargetDev)
2021 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
2022 res = OC_STACK_NO_MEMORY;
2026 removeData->removeRes =
2027 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2028 if (!removeData->removeRes)
2030 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
2031 res = OC_STACK_NO_MEMORY;
2035 removeData->ctx = ctx;
2036 removeData->linkedDevList = pLinkedDevList;
2037 removeData->resultCallback = resultCallback;
2038 removeData->numOfResults = 0;
2039 removeData->sizeOfResArray = numOfLinkedDev;
2040 removeData->hasError = false;
2042 // 5. Send DELETE credential request to linked devices.
2043 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2044 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2045 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2047 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2048 removeData->revokeTargetDev, curDev);
2049 if (OC_STACK_OK != res)
2051 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
2052 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2056 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2060 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2061 PMDeleteDeviceList(pOwnedDevList);
2062 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
2064 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2067 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2068 PMDeleteDeviceList(pOwnedDevList);
2069 PMDeleteDeviceList(pLinkedDevList);
2072 OICFree(removeData->revokeTargetDev);
2073 OICFree(removeData->removeRes);
2074 OICFree(removeData);
2076 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");
2081 * Function to device revocation
2082 * This function will remove credential of target device from all devices in subnet.
2084 * @param[in] ctx Application context would be returned in result callback
2085 * @param[in] pOwnedDevList List of owned devices
2086 * @param[in] pTargetDev Device information to be revoked.
2087 * @param[in] resultCallback callback provided by API user, callback will be called when
2088 * credential revocation is finished.
2089 * @return OC_STACK_OK in case of success and other value otherwise.
2090 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2091 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2093 OCStackResult SRPRemoveDeviceWithoutDiscovery(void* ctx, const OCProvisionDev_t* pOwnedDevList,
2094 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2096 OIC_LOG(INFO, TAG, "IN SRPRemoveDeviceWithoutDiscovery");
2100 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : Owned Device List is empty");
2101 return OC_STACK_CONTINUE;
2105 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL parameters");
2106 return OC_STACK_INVALID_PARAM;
2108 if (!resultCallback)
2110 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL Callback");
2111 return OC_STACK_INVALID_CALLBACK;
2114 // Declare variables in here to handle error cases with goto statement.
2115 OCProvisionDev_t* pLinkedDevList = NULL;
2116 RemoveData_t* removeData = NULL;
2118 //1. Find all devices that has a credential of the revoked device
2119 OCUuidList_t* pLinkedUuidList = NULL;
2120 size_t numOfDevices = 0;
2121 OCStackResult res = OC_STACK_ERROR;
2122 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2123 if (OC_STACK_OK != res)
2125 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to get linked devices information");
2126 return OC_STACK_CONTINUE;
2128 // if there is no related device, we can skip further process.
2129 if (0 == numOfDevices)
2131 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : No linked device found.");
2132 return OC_STACK_CONTINUE;
2135 //2. Make a list of devices to send DELETE credential request
2136 // by comparing owned devices from provisioning database with mutlicast discovery result.
2137 size_t numOfLinkedDev = 0;
2138 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2139 &pLinkedDevList, &numOfLinkedDev);
2140 if (OC_STACK_OK != res)
2142 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : GetListofDevToReqDeleteCred() failed");
2145 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2146 { // So we don't have to send request message.
2147 OIC_LOG(DEBUG, TAG, "SRPRemoveDeviceWithoutDiscovery : No alived & linked device found.");
2148 return OC_STACK_CONTINUE;
2151 // 3. Prepare RemoveData Context data.
2152 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2155 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2156 res = OC_STACK_NO_MEMORY;
2160 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2161 if (!removeData->revokeTargetDev)
2163 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : PMCloneOCProvisionDev Failed");
2164 res = OC_STACK_NO_MEMORY;
2168 removeData->removeRes =
2169 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2170 if (!removeData->removeRes)
2172 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2173 res = OC_STACK_NO_MEMORY;
2177 removeData->ctx = ctx;
2178 removeData->linkedDevList = pLinkedDevList;
2179 removeData->resultCallback = resultCallback;
2180 removeData->numOfResults = 0;
2181 removeData->sizeOfResArray = numOfLinkedDev;
2182 removeData->hasError = false;
2184 // 5. Send DELETE credential request to linked devices.
2185 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2186 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2187 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2189 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2190 removeData->revokeTargetDev, curDev);
2191 if (OC_STACK_OK != res)
2193 OIC_LOG_V(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Fail to send the DELETE credential request to\
2194 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2198 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2202 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2203 OIC_LOG(INFO, TAG, "OUT SRPRemoveDeviceWithoutDiscovery");
2205 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2208 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2209 PMDeleteDeviceList(pLinkedDevList);
2212 OICFree(removeData->revokeTargetDev);
2213 OICFree(removeData->removeRes);
2214 OICFree(removeData);
2216 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDeviceWithoutDiscovery");
2221 * Function to sync-up credential and ACL of the target device.
2222 * This function will remove credential and ACL of target device from all devices in subnet.
2224 * @param[in] ctx Application context would be returned in result callback
2225 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
2226 * @param[in] pTargetDev Device information to be revoked.
2227 * @param[in] resultCallback callback provided by API user, callback will be called when
2228 * credential revocation is finished.
2229 * when there is an error, this user callback is called immediately.
2230 * @return OC_STACK_OK in case of success and other value otherwise.
2231 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2232 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2234 OCStackResult SRPSyncDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
2235 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2237 OIC_LOG(INFO, TAG, "IN SRPSyncDevice");
2238 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
2240 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL parameters");
2241 return OC_STACK_INVALID_PARAM;
2243 if (!resultCallback)
2245 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL Callback");
2246 return OC_STACK_INVALID_CALLBACK;
2249 // Declare variables in here to handle error cases with goto statement.
2250 OCProvisionDev_t* pOwnedDevList = NULL;
2251 OCProvisionDev_t* pLinkedDevList = NULL;
2252 RemoveData_t* removeData = NULL;
2254 //1. Find all devices that has a credential of the revoked device
2255 OCUuidList_t* pLinkedUuidList = NULL;
2256 size_t numOfDevices = 0;
2257 OCStackResult res = OC_STACK_ERROR;
2258 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2259 if (OC_STACK_OK != res)
2261 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to get linked devices information");
2264 // if there is no related device, we can skip further process.
2265 if (0 == numOfDevices)
2267 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No linked device found.");
2268 res = OC_STACK_CONTINUE;
2272 //2. Find owned device from the network
2273 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
2274 if (OC_STACK_OK != res)
2276 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to PMDeviceDiscovery");
2280 //3. Make a list of devices to send DELETE credential request
2281 // by comparing owned devices from provisioning database with mutlicast discovery result.
2282 size_t numOfLinkedDev = 0;
2283 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2284 &pLinkedDevList, &numOfLinkedDev);
2285 if (OC_STACK_OK != res)
2287 OIC_LOG(ERROR, TAG, "SRPSyncDevice : GetListofDevToReqDeleteCred() failed");
2290 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2291 { // So we don't have to send request message.
2292 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No alived & linked device found.");
2293 res = OC_STACK_CONTINUE;
2297 // 4. Prepare RemoveData Context data.
2298 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2301 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to memory allocation");
2302 res = OC_STACK_NO_MEMORY;
2306 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2307 if (!removeData->revokeTargetDev)
2309 OIC_LOG(ERROR, TAG, "SRPSyncDevice : PMCloneOCProvisionDev Failed");
2310 res = OC_STACK_NO_MEMORY;
2314 removeData->removeRes =
2315 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2316 if (!removeData->removeRes)
2318 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to allocate memory");
2319 res = OC_STACK_NO_MEMORY;
2323 removeData->ctx = ctx;
2324 removeData->linkedDevList = pLinkedDevList;
2325 removeData->resultCallback = resultCallback;
2326 removeData->numOfResults = 0;
2327 removeData->sizeOfResArray = numOfLinkedDev;
2328 removeData->hasError = false;
2330 // 5. Send DELETE credential request to linked devices.
2331 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2332 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2333 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2335 res = SendDeleteACLRequest((void*)removeData, &SRPSyncDeviceACLCB,
2336 removeData->revokeTargetDev, curDev);
2337 if (OC_STACK_OK != res)
2339 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE ACL request to\
2340 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2343 res = SendDeleteCredentialRequest((void*)removeData, &SRPSyncDeviceCredCB,
2344 removeData->revokeTargetDev, curDev);
2345 if (OC_STACK_OK != res)
2347 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE credential request to\
2348 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2349 totalRes = OC_STACK_ERROR;
2353 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2357 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2358 PMDeleteDeviceList(pOwnedDevList);
2359 OIC_LOG(INFO, TAG, "OUT SRPSyncDevice");
2361 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2364 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2365 PMDeleteDeviceList(pOwnedDevList);
2366 PMDeleteDeviceList(pLinkedDevList);
2369 OICFree(removeData->revokeTargetDev);
2370 OICFree(removeData->removeRes);
2371 OICFree(removeData);
2373 OIC_LOG(INFO, TAG, "OUT ERROR case SRPSyncDevice");
2378 * Function for remote reset
2379 * This function will send pstat PUT message to the target device to initiate remote reset.
2381 * @param[in] pTargetDev Device information to be revoked.
2382 * @param[in] resultCallback callback provided by API user, callback will be called when
2383 * credential revocation is finished.
2384 * when there is an error, this user callback is called immediately.
2385 * @return OC_STACK_OK in case of success and other value otherwise.
2386 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2387 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2389 OCStackResult SRPResetDevice(const OCProvisionDev_t* pTargetDev,
2390 OCProvisionResultCB resultCallback)
2392 OIC_LOG(INFO, TAG, "IN SRPResetDevice");
2395 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL parameters");
2396 return OC_STACK_INVALID_PARAM;
2398 if (!resultCallback)
2400 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL Callback");
2401 return OC_STACK_INVALID_CALLBACK;
2404 OCStackResult res = OC_STACK_ERROR;
2405 OicSecPstat_t * pstat = (OicSecPstat_t *) OICCalloc(1, sizeof(OicSecPstat_t));
2408 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2409 return OC_STACK_NO_MEMORY;
2413 pstat->isOp = false;
2414 memcpy(pstat->deviceID.id, pTargetDev->doxm->deviceID.id, sizeof(OicUuid_t));
2415 pstat->tm = TAKE_OWNER;
2416 pstat->om = (OicSecDpom_t)(SINGLE_SERVICE_CLIENT_DRIVEN); // the only mode IoTivity supports currently
2418 pstat->sm = (OicSecDpom_t *) OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
2419 if (NULL == pstat->sm)
2421 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2423 return OC_STACK_NO_MEMORY;
2425 pstat->sm[0] = (OicSecDpom_t)(SINGLE_SERVICE_CLIENT_DRIVEN); // the only mode IoTivity supports currently
2427 OCSecurityPayload * secPayload = (OCSecurityPayload *) OICCalloc(1, sizeof(OCSecurityPayload));
2430 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2431 res = OC_STACK_NO_MEMORY;
2434 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2436 if (OC_STACK_OK != PstatToCBORPayload(pstat, &(secPayload->securityData),
2437 &(secPayload->payloadSize), true))
2439 OCPayloadDestroy((OCPayload *) secPayload);
2440 OIC_LOG(ERROR, TAG, "Failed to PstatToCBORPayload");
2441 res = OC_STACK_NO_MEMORY;
2444 OIC_LOG(DEBUG, TAG, "Created payload for pstat set");
2445 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2447 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2448 if (!PMGenerateQuery(true,
2449 pTargetDev->endpoint.addr,
2450 pTargetDev->securePort,
2451 pTargetDev->connType,
2452 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2454 OIC_LOG(ERROR, TAG, "SRPResetDevice : Failed to generate query");
2455 OCPayloadDestroy((OCPayload *) secPayload);
2456 res = OC_STACK_ERROR;
2459 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2461 OCCallbackData cbData = { .context = NULL, .cb = NULL, .cd = NULL };
2462 OCMethod method = OC_REST_POST;
2463 OCDoHandle handle = NULL;
2464 OCProvisionDev_t * targetDev = PMCloneOCProvisionDev(pTargetDev);
2465 if (NULL == targetDev)
2467 OIC_LOG(ERROR, TAG, "target dev is null");
2468 res = OC_STACK_ERROR;
2471 cbData.cb = &SRPResetDeviceCB;
2472 cbData.context = (void *) targetDev;
2474 OIC_LOG(DEBUG, TAG, "Sending PSTAT info to resource server");
2475 res = OCDoResource(&handle, method, query,
2476 &targetDev->endpoint, (OCPayload *)secPayload,
2477 targetDev->connType, OC_LOW_QOS, &cbData, NULL, 0);\
2478 if (OC_STACK_OK != res)
2480 OIC_LOG(ERROR, TAG, "OCStack resource error");
2486 OIC_LOG(INFO, TAG, "OUT SRPResetDevice");
2491 * Internal Function to store results in result array during GetCredResourceCB.
2493 static void registerResultForGetCredResourceCB(GetSecData_t *GetSecData,
2494 OCStackResult stackresult)
2496 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetCredResourceCB "
2497 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2498 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2499 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2500 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2501 ++(GetSecData->numOfResults);
2505 * Callback handler of SRPGetCredResource.
2507 * @param[in] ctx ctx value passed to callback from calling function.
2508 * @param[in] UNUSED handle to an invocation
2509 * @param[in] clientResponse Response from queries to remote servers.
2510 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2511 * and OC_STACK_KEEP_TRANSACTION to keep it.
2513 static OCStackApplicationResult SRPGetCredResourceCB(void *ctx, OCDoHandle UNUSED,
2514 OCClientResponse *clientResponse)
2516 OIC_LOG_V(INFO, TAG, "Inside SRPGetCredResourceCB.");
2518 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2519 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2520 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2524 if(OC_STACK_OK == clientResponse->result)
2526 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2527 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2529 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2531 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2532 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2535 OICFree(GetSecData->resArr);
2536 OICFree(GetSecData);
2538 return OC_STACK_DELETE_TRANSACTION;
2541 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2542 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2545 OIC_LOG_V(ERROR, TAG, "SRPGetCredResourceCB received Null clientResponse");
2546 OICFree(GetSecData->resArr);
2547 OICFree(GetSecData);
2549 return OC_STACK_DELETE_TRANSACTION;
2553 OCStackResult SRPGetCredResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2554 OCProvisionResultCB resultCallback)
2556 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2557 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2559 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2560 if(!PMGenerateQuery(true,
2561 selectedDeviceInfo->endpoint.addr,
2562 selectedDeviceInfo->securePort,
2563 selectedDeviceInfo->connType,
2564 query, sizeof(query), OIC_RSRC_CRED_URI))
2566 OIC_LOG(ERROR, TAG, "SRPGetCredResource : Failed to generate query");
2567 return OC_STACK_ERROR;
2569 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2571 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2572 cbData.cb = &SRPGetCredResourceCB;
2573 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2574 if (NULL == GetSecData)
2576 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2577 return OC_STACK_NO_MEMORY;
2579 GetSecData->deviceInfo = selectedDeviceInfo;
2580 GetSecData->resultCallback = resultCallback;
2581 GetSecData->numOfResults=0;
2582 GetSecData->ctx = ctx;
2584 int noOfRiCalls = 1;
2585 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2586 if (NULL == GetSecData->resArr)
2588 OICFree(GetSecData);
2589 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2590 return OC_STACK_NO_MEMORY;
2592 cbData.context = (void *)GetSecData;
2594 OCMethod method = OC_REST_GET;
2595 OCDoHandle handle = NULL;
2596 OIC_LOG(DEBUG, TAG, "Sending Get Cred to resource server");
2597 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2598 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2599 if (OC_STACK_OK != ret)
2601 OIC_LOG(ERROR, TAG, "OCStack resource error");
2602 OICFree(GetSecData->resArr);
2603 OICFree(GetSecData);
2605 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2606 OIC_LOG(DEBUG, TAG, "OUT SRPGetCredResource");
2612 * Internal Function to store results in result array during GetACLResourceCB.
2614 static void registerResultForGetACLResourceCB(GetSecData_t *GetSecData,
2615 OCStackResult stackresult)
2617 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetACLResourceCB "
2618 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2619 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2620 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2621 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2622 ++(GetSecData->numOfResults);
2626 * Callback handler of SRPGetACLResource.
2628 * @param[in] ctx ctx value passed to callback from calling function.
2629 * @param[in] UNUSED handle to an invocation
2630 * @param[in] clientResponse Response from queries to remote servers.
2631 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2632 * and OC_STACK_KEEP_TRANSACTION to keep it.
2634 static OCStackApplicationResult SRPGetACLResourceCB(void *ctx, OCDoHandle UNUSED,
2635 OCClientResponse *clientResponse)
2637 OIC_LOG_V(INFO, TAG, "Inside SRPGetACLResourceCB.");
2639 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2640 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2641 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2645 if(OC_STACK_OK == clientResponse->result)
2647 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2648 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2650 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2652 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2653 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2656 OICFree(GetSecData->resArr);
2657 OICFree(GetSecData);
2659 return OC_STACK_DELETE_TRANSACTION;
2662 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2663 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2666 OIC_LOG_V(ERROR, TAG, "SRPGetACLResourceCB received Null clientResponse");
2667 OICFree(GetSecData->resArr);
2668 OICFree(GetSecData);
2670 return OC_STACK_DELETE_TRANSACTION;
2674 OCStackResult SRPGetACLResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2675 OCProvisionResultCB resultCallback)
2677 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2678 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2680 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2681 if(!PMGenerateQuery(true,
2682 selectedDeviceInfo->endpoint.addr,
2683 selectedDeviceInfo->securePort,
2684 selectedDeviceInfo->connType,
2685 query, sizeof(query), OIC_RSRC_ACL_URI))
2687 OIC_LOG(ERROR, TAG, "SRPGetACLResource : Failed to generate query");
2688 return OC_STACK_ERROR;
2690 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2692 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2693 cbData.cb = &SRPGetACLResourceCB;
2694 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2695 if (NULL == GetSecData)
2697 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2698 return OC_STACK_NO_MEMORY;
2700 GetSecData->deviceInfo = selectedDeviceInfo;
2701 GetSecData->resultCallback = resultCallback;
2702 GetSecData->numOfResults=0;
2703 GetSecData->ctx = ctx;
2705 int noOfRiCalls = 1;
2706 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2707 if (NULL == GetSecData->resArr)
2709 OICFree(GetSecData);
2710 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2711 return OC_STACK_NO_MEMORY;
2713 cbData.context = (void *)GetSecData;
2715 OCMethod method = OC_REST_GET;
2716 OCDoHandle handle = NULL;
2717 OIC_LOG(DEBUG, TAG, "Sending Get ACL to resource server");
2718 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2719 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2720 if (OC_STACK_OK != ret)
2722 OIC_LOG(ERROR, TAG, "OCStack resource error");
2723 OICFree(GetSecData->resArr);
2724 OICFree(GetSecData);
2726 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2727 OIC_LOG(DEBUG, TAG, "OUT SRPGetACLResource");
2732 OCStackResult SRPReadTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
2735 OIC_LOG(DEBUG, TAG, "IN SRPReadTrustCertChain");
2737 OCStackResult res = OC_STACK_ERROR;
2739 OicSecCred_t* credData = GetCredEntryByCredId(credId);
2742 res = CredToCBORPayload((const OicSecCred_t*) credData, trustCertChain,
2743 chainSize, secureFlag);
2744 if(OC_STACK_OK != res)
2746 OIC_LOG(INFO, TAG, "CredToCBORPayload failed");
2749 DeleteCredList(credData);