1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * *****************************************************************/
20 #include "iotivity_config.h"
28 #include "ocprovisioningmanager.h"
29 #include "secureresourceprovider.h"
31 #include "oic_malloc.h"
32 #include "oic_string.h"
33 #include "aclresource.h"
34 #include "pstatresource.h"
35 #include "srmresourcestrings.h"
36 #include "credresource.h"
37 #include "doxmresource.h"
38 #include "pconfresource.h"
39 #include "credentialgenerator.h"
40 #include "cainterface.h"
41 #if defined (__TIZENRT__)
42 #include <apps/netutils/cJSON.h>
47 #include "pmutility.h"
48 #include "srmutility.h"
49 #include "provisioningdatabasemanager.h"
52 #include "ocpayload.h"
55 #include "crlresource.h"
58 #define TAG "OIC_SRPAPI"
61 * Macro to verify argument is not equal to NULL.
62 * eg: VERIFY_NON_NULL_RET(TAG, ptrData, ERROR,OC_STACK_ERROR);
64 #define VERIFY_NON_NULL_RET(tag, arg, logLevel, retValue) { if (NULL == (arg)) \
65 { OIC_LOG((logLevel), tag, #arg " is NULL"); return retValue; } }
68 * Macro to verify success of operation.
69 * eg: VERIFY_SUCCESS_RET(TAG, OC_STACK_OK == foo(), ERROR, OC_STACK_ERROR);
71 #define VERIFY_SUCCESS_RET(tag, op, logLevel, retValue) { if (!(op)) \
72 {OIC_LOG((logLevel), tag, #op " failed!!"); return retValue;} }
75 trustCertChainContext_t g_trustCertChainNotifier;
78 * Structure to carry credential data to callback.
80 typedef struct CredentialData CredentialData_t;
83 void *ctx; /**< Pointer to user context.**/
84 const OCProvisionDev_t *deviceInfo1; /**< Pointer to OCProvisionDev_t.**/
85 const OCProvisionDev_t *deviceInfo2; /**< Pointer to OCProvisionDev_t.**/
86 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
87 OicSecCred_t *credInfoFirst; /**< Pointer to OicSecCred_t.**/
88 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
89 OCProvisionResult_t *resArr; /**< Result array.**/
90 int numOfResults; /**< Number of results in result array.**/
94 * Structure to carry ACL provision API data to callback.
96 typedef struct ACLData ACLData_t;
99 void *ctx; /**< Pointer to user context.**/
100 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
101 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
102 OCProvisionResult_t *resArr; /**< Result array.**/
103 int numOfResults; /**< Number of results in result array.**/
106 // Structure to carry get security resource APIs data to callback.
107 typedef struct GetSecData GetSecData_t;
110 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
111 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
112 OCProvisionResult_t *resArr; /**< Result array.**/
113 int numOfResults; /**< Number of results in result array.**/
117 * Structure to carry PCONF provision API data to callback.
119 typedef struct PconfData PconfData_t;
122 void *ctx; /**< Pointer to user context.**/
123 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
124 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
125 OCProvisionResult_t *resArr; /**< Result array.**/
126 int numOfResults; /**< Number of results in result array.**/
129 // Enum type index for unlink callback.
131 IDX_FIRST_DEVICE_RES = 0, // index for resulf of the first device
132 IDX_SECOND_DEVICE_RES, // index for result of the second device
133 IDX_DB_UPDATE_RES // index for result of updating provisioning database.
136 // Structure to carry unlink APIs data to callback.
137 typedef struct UnlinkData UnlinkData_t;
140 OCProvisionDev_t* unlinkDev; /**< Pointer to OCProvisionDev_t to be unlinked.**/
141 OCProvisionResult_t* unlinkRes; /**< Result array.**/
142 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
143 int numOfResults; /**< Number of results in result array.**/
146 //Example of DELETE cred request -> coaps://0.0.0.0:5684/oic/sec/cred?sub=(BASE64 ENCODED UUID)
147 const char * SRP_FORM_DELETE_CREDENTIAL = "coaps://[%s]:%d%s?%s=%s";
148 const char * SRP_FORM_DELETE_CREDENTIAL_TCP = "coaps+tcp://[%s]:%d%s?%s=%s";
150 // Structure to carry remove APIs data to callback.
151 typedef struct RemoveData RemoveData_t;
154 OCProvisionDev_t* revokeTargetDev; /**< Device which is going to be revoked..**/
155 OCProvisionDev_t* linkedDevList; /**< A list of devices which have invalid credential.**/
156 OCProvisionResult_t* removeRes; /**< Result array.**/
157 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
158 OCClientContextDeleter deleteCallback; /**< Pointer to delete callback.**/
159 size_t numOfResults; /**< Number of results in result array.**/
160 size_t sizeOfResArray;
167 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
168 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
169 OCClientResponseHandler responseHandler);
173 * Internal function to update result in result array.
175 static void registerResultForCredProvisioning(CredentialData_t *credData,
176 OCStackResult stackresult, int cause)
179 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",credData->numOfResults);
182 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
183 credData->deviceInfo1->doxm->deviceID.id,UUID_LENGTH);
187 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
188 credData->deviceInfo2->doxm->deviceID.id,UUID_LENGTH);
190 credData->resArr[(credData->numOfResults)].res = stackresult;
191 ++(credData->numOfResults);
195 * Callback handler for handling callback of provisioning device 2.
197 * @param[in] ctx ctx value passed to callback from calling function.
198 * @param[in] UNUSED handle to an invocation
199 * @param[in] clientResponse Response from queries to remote servers.
200 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
201 * and OC_STACK_KEEP_TRANSACTION to keep it.
203 static OCStackApplicationResult provisionCredentialCB2(void *ctx, OCDoHandle UNUSED,
204 OCClientResponse *clientResponse)
206 VERIFY_NON_NULL_RET(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
207 CredentialData_t *credData = (CredentialData_t *) ctx;
210 OCProvisionResultCB resultCallback = credData->resultCallback;
211 OIC_LOG(INFO, TAG, "provisionCredentialCB2 called");
214 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
216 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CHANGED, 2);
217 OCStackResult res = PDMLinkDevices(&credData->deviceInfo1->doxm->deviceID,
218 &credData->deviceInfo2->doxm->deviceID);
219 if (OC_STACK_OK != res)
221 OIC_LOG(ERROR, TAG, "Error occured on PDMLinkDevices");
222 return OC_STACK_DELETE_TRANSACTION;
224 OIC_LOG(INFO, TAG, "Link created successfully");
226 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
229 OICFree(credData->resArr);
231 return OC_STACK_DELETE_TRANSACTION;
235 OIC_LOG(INFO, TAG, "provisionCredentialCB2 received Null clientResponse");
236 registerResultForCredProvisioning(credData, OC_STACK_ERROR, 2);
237 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
240 OICFree(credData->resArr);
242 return OC_STACK_DELETE_TRANSACTION;
246 * Callback handler for handling callback of provisioning device 1.
248 * @param[in] ctx ctx value passed to callback from calling function.
249 * @param[in] UNUSED handle to an invocation
250 * @param[in] clientResponse Response from queries to remote servers.
251 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
252 * and OC_STACK_KEEP_TRANSACTION to keep it.
254 static OCStackApplicationResult provisionCredentialCB1(void *ctx, OCDoHandle UNUSED,
255 OCClientResponse *clientResponse)
257 VERIFY_NON_NULL_RET(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
259 CredentialData_t* credData = (CredentialData_t*) ctx;
260 OICFree(credData->credInfoFirst);
261 const OCProvisionDev_t *deviceInfo = credData->deviceInfo2;
262 OicSecCred_t *credInfo = credData->credInfo;
263 const OCProvisionResultCB resultCallback = credData->resultCallback;
266 if (OC_STACK_RESOURCE_CHANGED == clientResponse->result)
268 // send credentials to second device
269 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CHANGED,1);
270 OCStackResult res = provisionCredentials(credInfo, deviceInfo, credData,
271 provisionCredentialCB2);
272 DeleteCredList(credInfo);
273 if (OC_STACK_OK != res)
275 registerResultForCredProvisioning(credData, res,2);
276 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
279 OICFree(credData->resArr);
286 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
287 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
290 OICFree(credData->resArr);
297 OIC_LOG(INFO, TAG, "provisionCredentialCB received Null clientResponse for first device");
298 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
299 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
302 DeleteCredList(credInfo);
303 OICFree(credData->resArr);
307 return OC_STACK_DELETE_TRANSACTION;
313 * Internal function for handling credential generation and sending credential to resource server.
315 * @param[in] cred Instance of cred resource.
316 * @param[in] deviceInfo information about device to which credential is to be provisioned.
317 * @param[in] responseHandler callbak called by OC stack when request API receives response.
318 * @return OC_STACK_OK in case of success and other value otherwise.
320 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
321 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
322 OCClientResponseHandler responseHandler)
324 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
327 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
328 return OC_STACK_NO_MEMORY;
330 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
332 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData,
333 &secPayload->payloadSize, secureFlag);
334 if((OC_STACK_OK != res) && (NULL == secPayload->securityData))
336 OCPayloadDestroy((OCPayload *)secPayload);
337 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
338 return OC_STACK_NO_MEMORY;
341 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
342 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
343 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
344 if(!PMGenerateQuery(true,
345 deviceInfo->endpoint.addr,
346 deviceInfo->securePort,
347 deviceInfo->connType,
348 query, sizeof(query), OIC_RSRC_CRED_URI))
350 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
351 OCPayloadDestroy((OCPayload *)secPayload);
352 return OC_STACK_ERROR;
354 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
356 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
357 cbData.cb = responseHandler;
358 cbData.context = (void *) credData;
361 OCDoHandle handle = NULL;
362 OCMethod method = OC_REST_POST;
363 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
364 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
365 OIC_LOG_V(INFO, TAG, "OCDoResource::Credential provisioning returned : %d",ret);
366 if (ret != OC_STACK_OK)
368 OIC_LOG(ERROR, TAG, "OCStack resource error");
374 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
376 * Structure to carry certificate data to callback.
378 typedef struct CertificateData CertData_t;
379 struct CertificateData
381 void *ctx; /**< Pointer to user context.**/
382 const OCProvisionDev_t *deviceInfo; /**< Pointer to OCProvisionDev_t.**/
383 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
384 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
385 OCProvisionResult_t *resArr; /**< Result array.**/
386 int numOfResults; /**< Number of results in result array.**/
390 * Internal function to update result in result array.
392 static void registerResultForCertProvisioning(CertData_t *certData,
393 OCStackResult stackresult)
396 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",certData->numOfResults);
397 memcpy(certData->resArr[(certData->numOfResults)].deviceId.id,
398 certData->deviceInfo->doxm->deviceID.id,UUID_LENGTH);
399 certData->resArr[(certData->numOfResults)].res = stackresult;
400 ++(certData->numOfResults);
403 OCStackResult SRPRegisterTrustCertChainNotifier(void *ctx, TrustCertChainChangeCB callback)
405 if (g_trustCertChainNotifier.callback)
407 OIC_LOG(ERROR, TAG, "Can't register Notifier, Unregister previous one");
408 return OC_STACK_ERROR;
411 g_trustCertChainNotifier.callback = callback;
412 g_trustCertChainNotifier.context = ctx;
416 void SRPRemoveTrustCertChainNotifier()
418 g_trustCertChainNotifier.callback = NULL;
419 g_trustCertChainNotifier.context = NULL;
424 * Callback handler for handling callback of certificate provisioning device.
426 * @param[in] ctx ctx value passed to callback from calling function.
427 * @param[in] UNUSED handle to an invocation
428 * @param[in] clientResponse Response from queries to remote servers.
429 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
430 * and OC_STACK_KEEP_TRANSACTION to keep it.
432 static OCStackApplicationResult provisionCertCB(void *ctx, OCDoHandle UNUSED,
433 OCClientResponse *clientResponse)
435 VERIFY_NON_NULL_RET(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
436 CertData_t *certData = (CertData_t *) ctx;
439 OCProvisionResultCB resultCallback = certData->resultCallback;
440 OIC_LOG(INFO, TAG, "provisionCertCB called");
443 if(OC_STACK_RESOURCE_CHANGED == clientResponse->result)
445 registerResultForCertProvisioning(certData, OC_STACK_RESOURCE_CHANGED);
446 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
449 OICFree(certData->resArr);
451 return OC_STACK_DELETE_TRANSACTION;
455 OIC_LOG(INFO, TAG, "provisionCertCredCB received Null clientResponse");
456 registerResultForCertProvisioning(certData, OC_STACK_ERROR);
457 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
460 OICFree(certData->resArr);
462 return OC_STACK_DELETE_TRANSACTION;
465 OCStackResult SRPProvisionTrustCertChain(void *ctx, OicSecCredType_t type, uint16_t credId,
466 const OCProvisionDev_t *selectedDeviceInfo, OCProvisionResultCB resultCallback)
468 OIC_LOG(INFO, TAG, "In SRPProvisionTrustCertChain");
469 VERIFY_NON_NULL_RET(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
470 VERIFY_NON_NULL_RET(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
471 if (SIGNED_ASYMMETRIC_KEY != type)
473 OIC_LOG(INFO, TAG, "Invalid key type");
474 return OC_STACK_INVALID_PARAM;
477 OicSecCred_t *trustCertChainCred = GetCredEntryByCredId(credId);
478 if(NULL == trustCertChainCred)
480 OIC_LOG(ERROR, TAG, "Can not find matched Trust Cert. Chain.");
481 return OC_STACK_NO_RESOURCE;
484 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
487 DeleteCredList(trustCertChainCred);
488 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
489 return OC_STACK_NO_MEMORY;
491 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
493 if(OC_STACK_OK != CredToCBORPayload(trustCertChainCred, &secPayload->securityData, &secPayload->payloadSize, secureFlag))
495 DeleteCredList(trustCertChainCred);
496 OCPayloadDestroy((OCPayload *)secPayload);
497 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
498 return OC_STACK_NO_MEMORY;
500 DeleteCredList(trustCertChainCred);
501 OIC_LOG(DEBUG, TAG, "Created payload for Cred:");
502 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
504 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
505 if(!PMGenerateQuery(true,
506 selectedDeviceInfo->endpoint.addr,
507 selectedDeviceInfo->securePort,
508 selectedDeviceInfo->connType,
509 query, sizeof(query), OIC_RSRC_CRED_URI))
511 OIC_LOG(ERROR, TAG, "SRPProvisionTrustCertChain : Failed to generate query");
512 OCPayloadDestroy((OCPayload *)secPayload);
513 return OC_STACK_ERROR;
515 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
517 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
518 cbData.cb = &provisionCertCB;
519 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
520 if (NULL == certData)
522 OIC_LOG(ERROR, TAG, "Memory allocation problem");
523 OCPayloadDestroy((OCPayload *)secPayload);
524 return OC_STACK_NO_MEMORY;
526 certData->deviceInfo = selectedDeviceInfo;
527 certData->resultCallback = resultCallback;
528 certData->credInfo = NULL; //credInfo not used in the response handler
529 certData->numOfResults=0;
533 certData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
534 if (certData->resArr == NULL)
537 OCPayloadDestroy((OCPayload *)secPayload);
538 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
539 return OC_STACK_NO_MEMORY;
541 cbData.context = (void *)certData;
543 OCMethod method = OC_REST_POST;
544 OCDoHandle handle = NULL;
545 OIC_LOG(DEBUG, TAG, "Sending Cred info to resource server");
546 OCStackResult ret = OCDoResource(&handle, method, query,
547 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
548 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
549 if (ret != OC_STACK_OK)
551 OICFree(certData->resArr);
555 VERIFY_SUCCESS_RET(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
559 OCStackResult SRPSaveTrustCertChain(uint8_t *trustCertChain, size_t chainSize,
560 OicEncodingType_t encodingType, uint16_t *credId)
562 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
564 VERIFY_NON_NULL_RET(TAG, trustCertChain, ERROR, OC_STACK_INVALID_PARAM);
565 VERIFY_NON_NULL_RET(TAG, credId, ERROR, OC_STACK_INVALID_PARAM);
567 // It is temporary condition.
568 // 6144(6K) = 6 * 1024, generally, one Cert is less than 2k.
569 // because we use 3 Cert, 6K is enough size for saving Certs.
570 if (6144 <= chainSize)
572 OIC_LOG_V(ERROR, TAG, "chainSize(%zd) is invalid", chainSize);
573 return OC_STACK_INVALID_PARAM;
576 OCStackResult res = OC_STACK_ERROR;
578 OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(*cred));
579 VERIFY_NON_NULL_RET(TAG, cred, ERROR, OC_STACK_NO_MEMORY);
581 res = GetDoxmDeviceID(&cred->subject);
582 if (OC_STACK_OK != res)
584 OIC_LOG(ERROR, TAG, "Can't get the device id(GetDoxmDeviceID)");
588 cred->credUsage= (char *)OICCalloc(1, strlen(TRUST_CA)+1 );
589 if (cred->credUsage == NULL)
591 OIC_LOG_V(ERROR, TAG, "%s can't alloc credUsage", __func__);
592 res = OC_STACK_NO_MEMORY;
595 OICStrcpy(cred->credUsage, strlen(TRUST_CA) + 1, TRUST_CA);
597 cred->credType = SIGNED_ASYMMETRIC_KEY;
599 if (encodingType == OIC_ENCODING_PEM)
601 cred->optionalData.data = (uint8_t *)OICCalloc(1, chainSize + 1);
602 if (cred->optionalData.data == NULL)
604 OIC_LOG_V(ERROR, TAG, "%s can't alloc cred->optionalData.data", __func__);
605 res = OC_STACK_NO_MEMORY;
608 cred->optionalData.len = chainSize + 1;
612 cred->optionalData.data = (uint8_t *)OICCalloc(chainSize, sizeof(uint8_t));
613 if (NULL == cred->optionalData.data)
615 OIC_LOG_V(ERROR, TAG, "%s can't alloc cred->optionalData.data", __func__);
616 res = OC_STACK_NO_MEMORY;
619 cred->optionalData.len = chainSize;
621 memcpy(cred->optionalData.data, trustCertChain, chainSize);
622 cred->optionalData.encoding = encodingType;
623 cred->optionalData.revstat = false;
625 res = AddCredential(cred);
626 if(res != OC_STACK_OK)
628 OIC_LOG_V(ERROR, TAG, "%s can't add cred", __func__);
631 *credId = cred->credId;
633 if (g_trustCertChainNotifier.callback)
635 uint8_t *certChain = (uint8_t*)OICCalloc(chainSize, sizeof(uint8_t));
636 VERIFY_NON_NULL_RET(TAG, certChain, ERROR, OC_STACK_NO_MEMORY);
637 memcpy(certChain, trustCertChain, chainSize);
638 g_trustCertChainNotifier.callback(g_trustCertChainNotifier.context, *credId,
639 certChain, chainSize);
643 if (OC_STACK_OK != res)
645 DeleteCredList(cred);
648 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
654 OCStackResult SRPSaveOwnCertChain(OicSecKey_t * cert, OicSecKey_t * key, uint16_t *credId)
656 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
657 VERIFY_NON_NULL_RET(TAG, cert, ERROR, OC_STACK_INVALID_PARAM);
658 VERIFY_NON_NULL_RET(TAG, cert->data, ERROR, OC_STACK_INVALID_PARAM);
659 VERIFY_NON_NULL_RET(TAG, key, ERROR, OC_STACK_INVALID_PARAM);
660 VERIFY_NON_NULL_RET(TAG, key->data, ERROR, OC_STACK_INVALID_PARAM);
661 VERIFY_NON_NULL_RET(TAG, credId, ERROR, OC_STACK_INVALID_PARAM);
663 OCStackResult res = OC_STACK_ERROR;
665 OicSecCred_t *cred = (OicSecCred_t *)OICCalloc(1, sizeof(OicSecCred_t));
666 VERIFY_NON_NULL_RET(TAG, cred, ERROR, OC_STACK_NO_MEMORY);
668 res = GetDoxmDeviceID(&cred->subject);
669 if (OC_STACK_OK != res)
671 OIC_LOG(ERROR, TAG, "Can't get the device id(GetDoxmDeviceID)");
675 cred->credUsage= (char *)OICCalloc(strlen(PRIMARY_CERT) + 1, sizeof(char));
676 if (cred->credUsage == NULL)
678 OIC_LOG_V(ERROR, TAG, "%s can't alloc credUsage", __func__);
679 res = OC_STACK_NO_MEMORY;
682 OICStrcpy(cred->credUsage, strlen(PRIMARY_CERT) + 1, PRIMARY_CERT) ;
684 cred->credType = SIGNED_ASYMMETRIC_KEY;
686 OicSecKey_t *publicData = &cred->publicData;
687 publicData->data = (uint8_t *)OICCalloc(cert->len, sizeof(uint8_t));
688 if (NULL == publicData->data)
690 OIC_LOG_V(ERROR, TAG, "%s can't alloc publicData", __func__);
691 res = OC_STACK_NO_MEMORY;
694 memcpy(publicData->data, cert->data, cert->len);
695 publicData->len = cert->len;
696 publicData->encoding = cert->encoding;
698 OicSecKey_t *privateData = &cred->privateData;
699 privateData->data = (uint8_t *)OICCalloc(key->len, sizeof(uint8_t));
700 if (NULL == privateData->data)
702 OIC_LOG_V(ERROR, TAG, "%s can't alloc privateData->data", __func__);
703 res = OC_STACK_NO_MEMORY;
706 memcpy(privateData->data, key->data, key->len);
707 privateData->len = key->len;
708 privateData->encoding = key->encoding;
710 res = AddCredential(cred);
711 if(res != OC_STACK_OK)
713 OIC_LOG_V(ERROR, TAG, "%s can't add cred", __func__);
716 *credId = cred->credId;
718 if (OC_STACK_OK != res)
720 DeleteCredList(cred);
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_RET(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM);
734 if (SYMMETRIC_PAIR_WISE_KEY == type)
736 VERIFY_NON_NULL_RET(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_RET(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_RET(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_RET(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_RET(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
906 VERIFY_NON_NULL_RET(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
907 VERIFY_NON_NULL_RET(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_RET(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_RET(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_RET(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_RET(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
1066 VERIFY_NON_NULL_RET(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
1067 VERIFY_NON_NULL_RET(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_RET(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);
1219 return OC_STACK_ERROR;
1222 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1224 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1225 const char *srpUri = SRP_FORM_DELETE_CREDENTIAL;
1227 if(OC_ADAPTER_TCP == destDev->endpoint.adapter)
1229 srpUri = SRP_FORM_DELETE_CREDENTIAL_TCP;
1233 snRet = snprintf(reqBuf, sizeof(reqBuf), srpUri, addressEncoded,
1234 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1238 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1239 return OC_STACK_ERROR;
1241 else if ((size_t)snRet >= sizeof(reqBuf))
1243 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1244 return OC_STACK_ERROR;
1247 OCCallbackData cbData;
1248 memset(&cbData, 0, sizeof(cbData));
1249 cbData.context = ctx;
1250 cbData.cb = respHandler;
1252 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1254 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1256 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1257 &destDev->endpoint, NULL,
1258 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1259 if (OC_STACK_OK != ret)
1261 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1263 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1268 static OCStackResult SendDeleteACLRequest(void* ctx,
1269 OCClientResponseHandler respHandler,
1270 const OCProvisionDev_t* revokedDev,
1271 const OCProvisionDev_t* destDev)
1273 OIC_LOG(DEBUG, TAG, "IN SendDeleteACLRequest");
1275 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1277 return OC_STACK_INVALID_PARAM;
1281 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1282 if(OC_STACK_OK != ret)
1284 OIC_LOG(ERROR, TAG, "SendDeleteACLRequest : Failed to canonical UUID encoding");
1285 return OC_STACK_ERROR;
1288 char addressEncoded[CA_MAX_URI_LENGTH] = {0};
1289 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
1290 sizeof(addressEncoded),
1291 destDev->endpoint.addr);
1292 if (OC_STACK_OK != result)
1295 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : encoding error %d\n", result);
1296 return OC_STACK_ERROR;
1300 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1302 //coaps://0.0.0.0:5684/oic/sec/acl?subjectuuid=(Canonical ENCODED UUID)
1303 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, addressEncoded,
1304 destDev->securePort, OIC_RSRC_ACL_URI, OIC_JSON_SUBJECTID_NAME, subID);
1308 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error (snprintf) %d\n", snRet);
1309 return OC_STACK_ERROR;
1311 else if ((size_t)snRet >= sizeof(reqBuf))
1313 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Truncated (snprintf) %d\n", snRet);
1314 return OC_STACK_ERROR;
1317 OCCallbackData cbData;
1318 memset(&cbData, 0, sizeof(cbData));
1319 cbData.context = ctx;
1320 cbData.cb = respHandler;
1322 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1324 OIC_LOG(DEBUG, TAG, "Sending remove ACL request to resource server");
1326 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1327 &destDev->endpoint, NULL,
1328 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1329 if (OC_STACK_OK != ret)
1331 OIC_LOG_V(ERROR, TAG, "SendDeleteACLRequest : Error in OCDoResource %d", ret);
1333 OIC_LOG(DEBUG, TAG, "OUT SendDeleteACLRequest");
1339 * Callback handler of unlink second device.
1341 * @param[in] ctx ctx value passed to callback from calling function.
1342 * @param[in] handle handle to an invocation
1343 * @param[in] clientResponse Response from queries to remote servers.
1344 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1345 * OC_STACK_KEEP_TRANSACTION to keep it.
1347 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1348 OCClientResponse *clientResponse)
1351 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1352 VERIFY_NON_NULL_RET(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1353 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1357 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1358 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1360 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1362 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1366 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1367 unlinkData->resultCallback(unlinkData->ctx,
1368 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1374 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1375 IDX_SECOND_DEVICE_RES);
1376 unlinkData->resultCallback(unlinkData->ctx,
1377 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1378 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1382 //Update provisioning DB when succes case.
1383 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1384 &unlinkData->unlinkDev[1].doxm->deviceID))
1386 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1387 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1388 unlinkData->resultCallback(unlinkData->ctx,
1389 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1392 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1396 DeleteUnlinkData_t(unlinkData);
1397 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1398 return OC_STACK_DELETE_TRANSACTION;
1403 * Callback handler of unlink first device.
1405 * @param[in] ctx ctx value passed to callback from calling function.
1406 * @param[in] handle handle to an invocation
1407 * @param[in] clientResponse Response from queries to remote servers.
1408 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1409 * OC_STACK_KEEP_TRANSACTION to keep it.
1411 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1412 OCClientResponse *clientResponse)
1414 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1415 VERIFY_NON_NULL_RET(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1416 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1421 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1422 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1424 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1426 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1428 // Second revocation request to second device.
1429 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1430 &unlinkData->unlinkDev[0],
1431 &unlinkData->unlinkDev[1] /*Dest*/);
1432 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1433 if (OC_STACK_OK != res)
1435 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1436 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1437 IDX_SECOND_DEVICE_RES);
1438 unlinkData->resultCallback(unlinkData->ctx,
1439 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1444 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1445 return OC_STACK_DELETE_TRANSACTION;
1450 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1452 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1453 unlinkData->unlinkRes, true);
1459 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1460 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1461 IDX_FIRST_DEVICE_RES );
1462 unlinkData->resultCallback(unlinkData->ctx,
1463 unlinkData->numOfResults, unlinkData->unlinkRes,
1465 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1469 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1470 DeleteUnlinkData_t(unlinkData);
1471 return OC_STACK_DELETE_TRANSACTION;
1475 * Function to unlink devices.
1476 * This function will remove the credential & relationship between the two devices.
1478 * @param[in] ctx Application context would be returned in result callback
1479 * @param[in] pTargetDev1 first device information to be unlinked.
1480 * @param[in] pTargetDev2 second device information to be unlinked.
1481 * @param[in] resultCallback callback provided by API user, callback will be called when
1482 * device unlink is finished.
1483 * @return OC_STACK_OK in case of success and other value otherwise.
1485 OCStackResult SRPUnlinkDevices(void* ctx,
1486 const OCProvisionDev_t* pTargetDev1,
1487 const OCProvisionDev_t* pTargetDev2,
1488 OCProvisionResultCB resultCallback)
1490 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1492 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1494 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1495 return OC_STACK_INVALID_PARAM;
1497 if (!resultCallback)
1499 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1500 return OC_STACK_INVALID_CALLBACK;
1502 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1504 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1505 return OC_STACK_INVALID_PARAM;
1508 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1509 PMPrintOCProvisionDev(pTargetDev1);
1510 PMPrintOCProvisionDev(pTargetDev2);
1512 // Mark the link status stale
1513 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1514 if (OC_STACK_OK != res)
1516 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1520 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1521 VERIFY_NON_NULL_RET(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1523 //Initialize unlink data
1524 unlinkData->ctx = ctx;
1525 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1526 if (NULL == unlinkData->unlinkDev)
1528 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1529 res = OC_STACK_NO_MEMORY;
1533 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1534 if (NULL == unlinkData->unlinkRes)
1536 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1537 res = OC_STACK_NO_MEMORY;
1541 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1542 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1544 unlinkData->numOfResults = 0;
1545 unlinkData->resultCallback = resultCallback;
1547 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1548 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1549 if (OC_STACK_OK != res)
1551 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1558 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1559 DeleteUnlinkData_t(unlinkData);
1563 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1567 OICFree(pRemoveData->revokeTargetDev);
1568 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1569 OICFree(pRemoveData->removeRes);
1570 OICFree(pRemoveData);
1574 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1575 OCStackResult stackresult, bool hasError)
1577 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1578 removeData->numOfResults + 1);
1581 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1582 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1586 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1587 0, sizeof(pLinkedDevId->id) );
1589 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1590 removeData->hasError = hasError;
1591 ++(removeData->numOfResults);
1593 // If we get suffcient result from linked devices, we have to call user callback and do free
1594 if (removeData->sizeOfResArray == removeData->numOfResults)
1596 if(!removeData->hasError)
1598 // Remove device info from prvisioning database
1599 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1601 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1602 removeData->hasError = true;
1605 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1606 removeData->hasError);
1607 DeleteRemoveData_t(removeData);
1611 static void registerResultForResetDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1612 OCStackResult stackresult, bool hasError)
1614 OIC_LOG_V(INFO, TAG, "Inside registerResultForResetDevice removeData->numOfResults is %zu\n",
1615 removeData->numOfResults + 1);
1618 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1619 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1623 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1624 0, sizeof(pLinkedDevId->id) );
1626 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1627 removeData->hasError = hasError;
1628 ++(removeData->numOfResults);
1630 // If we get suffcient result from linked devices, we have to call user callback and do free
1631 if (removeData->sizeOfResArray == removeData->numOfResults)
1633 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1634 removeData->hasError);
1635 DeleteRemoveData_t(removeData);
1640 * Callback handler of unlink first device.
1642 * @param[in] ctx ctx value passed to callback from calling function.
1643 * @param[in] handle handle to an invocation
1644 * @param[in] clientResponse Response from queries to remote servers.
1645 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1646 * and OC_STACK_KEEP_TRANSACTION to keep it.
1648 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1649 OCClientResponse *clientResponse)
1651 //Update the delete credential into delete device context
1652 //Save the deleted status in delDevCtx
1654 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1655 VERIFY_NON_NULL_RET(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1656 OCStackResult res = OC_STACK_ERROR;
1658 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1662 OicUuid_t revDevUuid = {.id={0}};
1663 if(UUID_LENGTH == clientResponse->identity.id_length)
1665 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1666 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1668 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1669 if (OC_STACK_OK != res)
1671 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1672 registerResultForRemoveDevice(removeData, &revDevUuid,
1673 OC_STACK_INCONSISTENT_DB, true);
1675 return OC_STACK_DELETE_TRANSACTION;
1678 registerResultForRemoveDevice(removeData, &revDevUuid,
1679 OC_STACK_RESOURCE_DELETED, false);
1683 registerResultForRemoveDevice(removeData, &revDevUuid,
1684 clientResponse->result, true);
1685 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1690 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1691 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1693 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1696 * Since server's credential was deleted,
1697 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1699 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1700 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1704 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1710 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1711 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1715 return OC_STACK_DELETE_TRANSACTION;
1719 * Callback handler of reset device.
1721 * @param[in] ctx ctx value passed to callback from calling function.
1722 * @param[in] handle handle to an invocation
1723 * @param[in] clientResponse Response from queries to remote servers.
1724 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1725 * and OC_STACK_KEEP_TRANSACTION to keep it.
1727 static OCStackApplicationResult SRPSyncDeviceCredCB(void *delDevCtx, OCDoHandle handle,
1728 OCClientResponse *clientResponse)
1730 //Update the delete credential into delete device context
1731 //Save the deleted status in delDevCtx
1733 OIC_LOG_V(INFO, TAG, "Inside SRPSyncDeviceCredCB.");
1734 VERIFY_NON_NULL_RET(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1735 OCStackResult res = OC_STACK_ERROR;
1737 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1738 OCProvisionDev_t * pTargetDev = PMCloneOCProvisionDev(removeData->revokeTargetDev);
1739 OCClientContextDeleter deleteCallback = removeData->deleteCallback;
1742 OicUuid_t revDevUuid = {.id={0}};
1743 if(UUID_LENGTH == clientResponse->identity.id_length)
1745 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1746 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1748 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1749 if (OC_STACK_OK != res)
1751 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1752 registerResultForResetDevice(removeData, &revDevUuid,
1753 OC_STACK_INCONSISTENT_DB, true);
1755 return OC_STACK_DELETE_TRANSACTION;
1758 registerResultForResetDevice(removeData, &revDevUuid,
1759 OC_STACK_RESOURCE_DELETED, false);
1763 registerResultForResetDevice(removeData, &revDevUuid,
1764 clientResponse->result, false);
1765 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1770 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1771 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1773 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1776 * Since server's credential was deleted,
1777 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1779 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1780 registerResultForResetDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1784 registerResultForResetDevice(removeData, NULL, clientResponse->result, true);
1790 registerResultForResetDevice(removeData, NULL, OC_STACK_ERROR, true);
1791 OIC_LOG(ERROR, TAG, "SRPSyncDevice received Null clientResponse");
1794 SRPResetDevice(pTargetDev, deleteCallback);
1796 return OC_STACK_DELETE_TRANSACTION;
1800 * Callback handler of reset device sync-up
1802 * @param[in] ctx ctx value passed to callback from calling function.
1803 * @param[in] handle handle to an invocation
1804 * @param[in] clientResponse Response from queries to remote servers.
1805 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1806 * and OC_STACK_KEEP_TRANSACTION to keep it.
1808 static OCStackApplicationResult SRPSyncDeviceACLCB(void *ctx, OCDoHandle UNUSED,
1809 OCClientResponse *clientResponse)
1813 (void)clientResponse;
1814 return OC_STACK_DELETE_TRANSACTION;
1818 * Callback handler of device remote reset.
1820 * @param[in] ctx ctx value passed to callback from calling function.
1821 * @param[in] UNUSED handle to an invocation
1822 * @param[in] clientResponse Response from queries to remote servers.
1823 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1824 * and OC_STACK_KEEP_TRANSACTION to keep it.
1826 static OCStackApplicationResult SRPResetDeviceCB(void *ctx, OCDoHandle UNUSED,
1827 OCClientResponse *clientResponse)
1829 OIC_LOG(DEBUG, TAG, "IN SRPResetDeviceCB");
1831 if(OC_STACK_OK == clientResponse->result)
1833 OIC_LOG(DEBUG, TAG, "Change Target Device Pstat Cm SUCCEEDED");
1836 // Delete Cred and ACL related to the target device.
1837 const OicSecCred_t *cred = NULL;
1838 OCProvisionDev_t * pTargetDev = (OCProvisionDev_t *)ctx;
1839 cred = GetCredResourceData(&pTargetDev->doxm->deviceID);
1842 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to get credential of target device.");
1846 OCStackResult res = RemoveCredential(&cred->subject);
1847 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1849 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove credential.");
1853 res = RemoveACE(&cred->subject, NULL);
1854 if (res != OC_STACK_RESOURCE_DELETED && res != OC_STACK_NO_RESOURCE)
1856 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to remove ACL.");
1859 if (OC_STACK_OK != PDMDeleteDevice(&pTargetDev->doxm->deviceID))
1861 OIC_LOG(ERROR, TAG, "OCResetDevice : Failed to delete device from PDM");
1864 //Close the DTLS session of the reset device.
1865 CAEndpoint_t* endpoint = (CAEndpoint_t *)&clientResponse->devAddr;
1866 CAResult_t caResult = CAcloseSslSession(endpoint);
1867 if(CA_STATUS_OK != caResult)
1869 OIC_LOG_V(WARNING, TAG, "OCResetDevice : Failed to close DTLS session : %d", caResult);
1873 * If there is no linked device, PM does not send any request.
1874 * So we should directly invoke the result callback to inform the result of OCResetDevice.
1876 if(OC_STACK_NO_RESOURCE == res)
1882 OICFree(pTargetDev);
1883 return OC_STACK_DELETE_TRANSACTION;
1887 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1888 const OCProvisionDev_t* pOwnedDevList,
1889 OCUuidList_t* pLinkedUuidList,
1890 OCProvisionDev_t** ppLinkedDevList,
1891 size_t *numOfLinkedDev)
1893 // pOwnedDevList could be NULL. It means no alived and owned device now.
1894 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1895 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1897 return OC_STACK_INVALID_PARAM;
1901 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1902 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1904 // Mark the link status stale.
1905 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1906 if (OC_STACK_OK != res)
1908 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1909 return OC_STACK_SVR_DB_NOT_EXIST;
1914 // If this linked device is alive (power-on), add the deivce to the list.
1915 const OCProvisionDev_t *curDev = NULL;
1916 const OCProvisionDev_t *tmpDev = NULL;
1917 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1919 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1921 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1922 if (NULL == targetDev)
1924 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1925 return OC_STACK_NO_MEMORY;
1928 LL_PREPEND(*ppLinkedDevList, targetDev);
1935 *numOfLinkedDev = cnt;
1940 * Function to device revocation
1941 * This function will remove credential of target device from all devices in subnet.
1943 * @param[in] ctx Application context would be returned in result callback
1944 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1945 * @param[in] pTargetDev Device information to be revoked.
1946 * @param[in] resultCallback callback provided by API user, callback will be called when
1947 * credential revocation is finished.
1948 * @return OC_STACK_OK in case of success and other value otherwise.
1949 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1950 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1952 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1953 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1955 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1957 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1959 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1960 return OC_STACK_INVALID_PARAM;
1962 if (!resultCallback)
1964 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
1965 return OC_STACK_INVALID_CALLBACK;
1968 // Declare variables in here to handle error cases with goto statement.
1969 OCProvisionDev_t* pOwnedDevList = NULL;
1970 OCProvisionDev_t* pLinkedDevList = NULL;
1971 RemoveData_t* removeData = NULL;
1973 //1. Find all devices that has a credential of the revoked device
1974 OCUuidList_t* pLinkedUuidList = NULL;
1975 size_t numOfDevices = 0;
1976 OCStackResult res = OC_STACK_ERROR;
1977 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
1978 if (OC_STACK_OK != res)
1980 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
1983 // if there is no related device, we can skip further process.
1984 if (0 == numOfDevices)
1986 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
1987 res = OC_STACK_CONTINUE;
1991 //2. Find owned device from the network
1992 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
1993 if (OC_STACK_OK != res)
1995 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
1999 //3. Make a list of devices to send DELETE credential request
2000 // by comparing owned devices from provisioning database with mutlicast discovery result.
2001 size_t numOfLinkedDev = 0;
2002 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2003 &pLinkedDevList, &numOfLinkedDev);
2004 if (OC_STACK_OK != res)
2006 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
2009 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2010 { // So we don't have to send request message.
2011 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
2012 res = OC_STACK_CONTINUE;
2016 // 4. Prepare RemoveData Context data.
2017 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2020 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
2021 res = OC_STACK_NO_MEMORY;
2025 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2026 if (!removeData->revokeTargetDev)
2028 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
2029 res = OC_STACK_NO_MEMORY;
2033 removeData->removeRes =
2034 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2035 if (!removeData->removeRes)
2037 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
2038 res = OC_STACK_NO_MEMORY;
2042 removeData->ctx = ctx;
2043 removeData->linkedDevList = pLinkedDevList;
2044 removeData->resultCallback = resultCallback;
2045 removeData->numOfResults = 0;
2046 removeData->sizeOfResArray = numOfLinkedDev;
2047 removeData->hasError = false;
2049 // 5. Send DELETE credential request to linked devices.
2050 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2051 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2052 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2054 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2055 removeData->revokeTargetDev, curDev);
2056 if (OC_STACK_OK != res)
2058 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
2059 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2063 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2067 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2068 PMDeleteDeviceList(pOwnedDevList);
2069 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
2071 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2074 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2075 PMDeleteDeviceList(pOwnedDevList);
2076 PMDeleteDeviceList(pLinkedDevList);
2079 OICFree(removeData->revokeTargetDev);
2080 OICFree(removeData->removeRes);
2081 OICFree(removeData);
2083 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");
2088 * Function to device revocation
2089 * This function will remove credential of target device from all devices in subnet.
2091 * @param[in] ctx Application context would be returned in result callback
2092 * @param[in] pOwnedDevList List of owned devices
2093 * @param[in] pTargetDev Device information to be revoked.
2094 * @param[in] resultCallback callback provided by API user, callback will be called when
2095 * credential revocation is finished.
2096 * @return OC_STACK_OK in case of success and other value otherwise.
2097 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2098 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2100 OCStackResult SRPRemoveDeviceWithoutDiscovery(void* ctx, const OCProvisionDev_t* pOwnedDevList,
2101 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2103 OIC_LOG(INFO, TAG, "IN SRPRemoveDeviceWithoutDiscovery");
2107 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : Owned Device List is empty");
2108 return OC_STACK_CONTINUE;
2112 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL parameters");
2113 return OC_STACK_INVALID_PARAM;
2115 if (!resultCallback)
2117 OIC_LOG(INFO, TAG, "SRPRemoveDeviceWithoutDiscovery : NULL Callback");
2118 return OC_STACK_INVALID_CALLBACK;
2121 // Declare variables in here to handle error cases with goto statement.
2122 OCProvisionDev_t* pLinkedDevList = NULL;
2123 RemoveData_t* removeData = NULL;
2125 //1. Find all devices that has a credential of the revoked device
2126 OCUuidList_t* pLinkedUuidList = NULL;
2127 size_t numOfDevices = 0;
2128 OCStackResult res = OC_STACK_ERROR;
2129 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2130 if (OC_STACK_OK != res)
2132 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to get linked devices information");
2133 return OC_STACK_CONTINUE;
2135 // if there is no related device, we can skip further process.
2136 if (0 == numOfDevices)
2138 OIC_LOG(WARNING, TAG, "SRPRemoveDeviceWithoutDiscovery : No linked device found.");
2139 return OC_STACK_CONTINUE;
2142 //2. Make a list of devices to send DELETE credential request
2143 // by comparing owned devices from provisioning database with mutlicast discovery result.
2144 size_t numOfLinkedDev = 0;
2145 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2146 &pLinkedDevList, &numOfLinkedDev);
2147 if (OC_STACK_OK != res)
2149 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : GetListofDevToReqDeleteCred() failed");
2152 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2153 { // So we don't have to send request message.
2154 OIC_LOG(DEBUG, TAG, "SRPRemoveDeviceWithoutDiscovery : No alived & linked device found.");
2155 return OC_STACK_CONTINUE;
2158 // 3. Prepare RemoveData Context data.
2159 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2162 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2163 res = OC_STACK_NO_MEMORY;
2167 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2168 if (!removeData->revokeTargetDev)
2170 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : PMCloneOCProvisionDev Failed");
2171 res = OC_STACK_NO_MEMORY;
2175 removeData->removeRes =
2176 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2177 if (!removeData->removeRes)
2179 OIC_LOG(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Failed to memory allocation");
2180 res = OC_STACK_NO_MEMORY;
2184 removeData->ctx = ctx;
2185 removeData->linkedDevList = pLinkedDevList;
2186 removeData->resultCallback = resultCallback;
2187 removeData->numOfResults = 0;
2188 removeData->sizeOfResArray = numOfLinkedDev;
2189 removeData->hasError = false;
2191 // 5. Send DELETE credential request to linked devices.
2192 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2193 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2194 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2196 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
2197 removeData->revokeTargetDev, curDev);
2198 if (OC_STACK_OK != res)
2200 OIC_LOG_V(ERROR, TAG, "SRPRemoveDeviceWithoutDiscovery : Fail to send the DELETE credential request to\
2201 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2205 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2209 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2210 OIC_LOG(INFO, TAG, "OUT SRPRemoveDeviceWithoutDiscovery");
2212 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2215 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2216 PMDeleteDeviceList(pLinkedDevList);
2219 OICFree(removeData->revokeTargetDev);
2220 OICFree(removeData->removeRes);
2221 OICFree(removeData);
2223 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDeviceWithoutDiscovery");
2228 * Function to sync-up credential and ACL of the target device.
2229 * This function will remove credential and ACL of target device from all devices in subnet.
2231 * @param[in] ctx Application context would be returned in result callback
2232 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
2233 * @param[in] pTargetDev Device information to be revoked.
2234 * @param[in] resultCallback callback provided by API user, callback will be called when
2235 * credential revocation is finished.
2236 * when there is an error, this user callback is called immediately.
2237 * @return OC_STACK_OK in case of success and other value otherwise.
2238 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2239 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2241 OCStackResult SRPSyncDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
2242 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
2244 OIC_LOG(INFO, TAG, "IN SRPSyncDevice");
2245 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
2247 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL parameters");
2248 return OC_STACK_INVALID_PARAM;
2250 if (!resultCallback)
2252 OIC_LOG(INFO, TAG, "SRPSyncDevice : NULL Callback");
2253 return OC_STACK_INVALID_CALLBACK;
2256 // Declare variables in here to handle error cases with goto statement.
2257 OCProvisionDev_t* pOwnedDevList = NULL;
2258 OCProvisionDev_t* pLinkedDevList = NULL;
2259 RemoveData_t* removeData = NULL;
2261 //1. Find all devices that has a credential of the revoked device
2262 OCUuidList_t* pLinkedUuidList = NULL;
2263 size_t numOfDevices = 0;
2264 OCStackResult res = OC_STACK_ERROR;
2265 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
2266 if (OC_STACK_OK != res)
2268 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to get linked devices information");
2271 // if there is no related device, we can skip further process.
2272 if (0 == numOfDevices)
2274 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No linked device found.");
2275 res = OC_STACK_CONTINUE;
2279 //2. Find owned device from the network
2280 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
2281 if (OC_STACK_OK != res)
2283 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to PMDeviceDiscovery");
2287 //3. Make a list of devices to send DELETE credential request
2288 // by comparing owned devices from provisioning database with mutlicast discovery result.
2289 size_t numOfLinkedDev = 0;
2290 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
2291 &pLinkedDevList, &numOfLinkedDev);
2292 if (OC_STACK_OK != res)
2294 OIC_LOG(ERROR, TAG, "SRPSyncDevice : GetListofDevToReqDeleteCred() failed");
2297 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
2298 { // So we don't have to send request message.
2299 OIC_LOG(DEBUG, TAG, "SRPSyncDevice : No alived & linked device found.");
2300 res = OC_STACK_CONTINUE;
2304 // 4. Prepare RemoveData Context data.
2305 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
2308 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to memory allocation");
2309 res = OC_STACK_NO_MEMORY;
2313 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
2314 if (!removeData->revokeTargetDev)
2316 OIC_LOG(ERROR, TAG, "SRPSyncDevice : PMCloneOCProvisionDev Failed");
2317 res = OC_STACK_NO_MEMORY;
2321 removeData->removeRes =
2322 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
2323 if (!removeData->removeRes)
2325 OIC_LOG(ERROR, TAG, "SRPSyncDevice : Failed to allocate memory");
2326 res = OC_STACK_NO_MEMORY;
2330 removeData->ctx = ctx;
2331 removeData->linkedDevList = pLinkedDevList;
2332 removeData->resultCallback = resultCallback;
2333 removeData->numOfResults = 0;
2334 removeData->sizeOfResArray = numOfLinkedDev;
2335 removeData->hasError = false;
2337 // 5. Send DELETE credential request to linked devices.
2338 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
2339 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
2340 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
2342 res = SendDeleteACLRequest((void*)removeData, &SRPSyncDeviceACLCB,
2343 removeData->revokeTargetDev, curDev);
2344 if (OC_STACK_OK != res)
2346 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE ACL request to\
2347 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2350 res = SendDeleteCredentialRequest((void*)removeData, &SRPSyncDeviceCredCB,
2351 removeData->revokeTargetDev, curDev);
2352 if (OC_STACK_OK != res)
2354 OIC_LOG_V(ERROR, TAG, "SRPSyncDevice : Fail to send the DELETE credential request to\
2355 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
2356 totalRes = OC_STACK_ERROR;
2360 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
2364 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
2365 PMDeleteDeviceList(pOwnedDevList);
2366 OIC_LOG(INFO, TAG, "OUT SRPSyncDevice");
2368 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
2371 PDMDestoryOicUuidLinkList(pLinkedUuidList);
2372 PMDeleteDeviceList(pOwnedDevList);
2373 PMDeleteDeviceList(pLinkedDevList);
2376 OICFree(removeData->revokeTargetDev);
2377 OICFree(removeData->removeRes);
2378 OICFree(removeData);
2380 OIC_LOG(INFO, TAG, "OUT ERROR case SRPSyncDevice");
2385 * Function for remote reset
2386 * This function will send pstat PUT message to the target device to initiate remote reset.
2388 * @param[in] pTargetDev Device information to be revoked.
2389 * @param[in] resultCallback callback provided by API user, callback will be called when
2390 * credential revocation is finished.
2391 * when there is an error, this user callback is called immediately.
2392 * @return OC_STACK_OK in case of success and other value otherwise.
2393 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
2394 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
2396 OCStackResult SRPResetDevice(const OCProvisionDev_t* pTargetDev,
2397 OCClientContextDeleter deleteCallback)
2399 OIC_LOG(INFO, TAG, "IN SRPResetDevice");
2402 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL parameters");
2403 return OC_STACK_INVALID_PARAM;
2405 if (!deleteCallback)
2407 OIC_LOG(INFO, TAG, "SRPResetDevice : NULL Callback");
2408 return OC_STACK_INVALID_CALLBACK;
2411 OCStackResult res = OC_STACK_ERROR;
2412 OicSecPstat_t * pstat = (OicSecPstat_t *) OICCalloc(1, sizeof(OicSecPstat_t));
2415 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2416 return OC_STACK_NO_MEMORY;
2420 pstat->isOp = false;
2421 memcpy(pstat->deviceID.id, pTargetDev->doxm->deviceID.id, sizeof(OicUuid_t));
2422 pstat->tm = TAKE_OWNER;
2423 pstat->om = (OicSecDpom_t)(SINGLE_SERVICE_CLIENT_DRIVEN); // the only mode IoTivity supports currently
2425 pstat->sm = (OicSecDpom_t *) OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
2426 if (NULL == pstat->sm)
2428 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2430 return OC_STACK_NO_MEMORY;
2432 pstat->sm[0] = (OicSecDpom_t)(SINGLE_SERVICE_CLIENT_DRIVEN); // the only mode IoTivity supports currently
2434 OCSecurityPayload * secPayload = (OCSecurityPayload *) OICCalloc(1, sizeof(OCSecurityPayload));
2437 OIC_LOG(ERROR, TAG, "Failed to allocate memory");
2438 res = OC_STACK_NO_MEMORY;
2441 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
2443 if (OC_STACK_OK != PstatToCBORPayload(pstat, &(secPayload->securityData),
2444 &(secPayload->payloadSize), true))
2446 OCPayloadDestroy((OCPayload *) secPayload);
2447 OIC_LOG(ERROR, TAG, "Failed to PstatToCBORPayload");
2448 res = OC_STACK_NO_MEMORY;
2451 OIC_LOG(DEBUG, TAG, "Created payload for pstat set");
2452 OIC_LOG_BUFFER(DEBUG, TAG, secPayload->securityData, secPayload->payloadSize);
2454 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2455 if (!PMGenerateQuery(true,
2456 pTargetDev->endpoint.addr,
2457 pTargetDev->securePort,
2458 pTargetDev->connType,
2459 query, sizeof(query), OIC_RSRC_PSTAT_URI))
2461 OIC_LOG(ERROR, TAG, "SRPResetDevice : Failed to generate query");
2462 OCPayloadDestroy((OCPayload *) secPayload);
2463 res = OC_STACK_ERROR;
2466 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2468 OCCallbackData cbData = { .context = NULL, .cb = NULL, .cd = NULL };
2469 OCMethod method = OC_REST_POST;
2470 OCDoHandle handle = NULL;
2471 OCProvisionDev_t * targetDev = PMCloneOCProvisionDev(pTargetDev);
2472 if (NULL == targetDev)
2474 OIC_LOG(ERROR, TAG, "target dev is null");
2475 res = OC_STACK_ERROR;
2478 cbData.cb = &SRPResetDeviceCB;
2479 cbData.context = (void *) targetDev;
2480 cbData.cd = deleteCallback;
2481 OIC_LOG(DEBUG, TAG, "Sending PSTAT info to resource server");
2482 res = OCDoResource(&handle, method, query,
2483 &targetDev->endpoint, (OCPayload *)secPayload,
2484 targetDev->connType, OC_LOW_QOS, &cbData, NULL, 0);\
2485 if (OC_STACK_OK != res)
2487 OIC_LOG(ERROR, TAG, "OCStack resource error");
2493 OIC_LOG(INFO, TAG, "OUT SRPResetDevice");
2498 * Internal Function to store results in result array during GetCredResourceCB.
2500 static void registerResultForGetCredResourceCB(GetSecData_t *GetSecData,
2501 OCStackResult stackresult)
2503 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetCredResourceCB "
2504 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2505 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2506 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2507 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2508 ++(GetSecData->numOfResults);
2512 * Callback handler of SRPGetCredResource.
2514 * @param[in] ctx ctx value passed to callback from calling function.
2515 * @param[in] UNUSED handle to an invocation
2516 * @param[in] clientResponse Response from queries to remote servers.
2517 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2518 * and OC_STACK_KEEP_TRANSACTION to keep it.
2520 static OCStackApplicationResult SRPGetCredResourceCB(void *ctx, OCDoHandle UNUSED,
2521 OCClientResponse *clientResponse)
2523 OIC_LOG_V(INFO, TAG, "Inside SRPGetCredResourceCB.");
2525 VERIFY_NON_NULL_RET(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2526 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2527 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2531 if(OC_STACK_OK == clientResponse->result)
2533 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2534 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2536 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2540 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2541 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2544 OICFree(GetSecData->resArr);
2545 OICFree(GetSecData);
2547 return OC_STACK_DELETE_TRANSACTION;
2550 registerResultForGetCredResourceCB(GetSecData, OC_STACK_OK);
2551 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2554 OIC_LOG_V(ERROR, TAG, "SRPGetCredResourceCB received Null clientResponse");
2555 OICFree(GetSecData->resArr);
2556 OICFree(GetSecData);
2558 return OC_STACK_DELETE_TRANSACTION;
2562 OCStackResult SRPGetCredResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2563 OCProvisionResultCB resultCallback)
2565 VERIFY_NON_NULL_RET(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2566 VERIFY_NON_NULL_RET(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2568 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2569 if(!PMGenerateQuery(true,
2570 selectedDeviceInfo->endpoint.addr,
2571 selectedDeviceInfo->securePort,
2572 selectedDeviceInfo->connType,
2573 query, sizeof(query), OIC_RSRC_CRED_URI))
2575 OIC_LOG(ERROR, TAG, "SRPGetCredResource : Failed to generate query");
2576 return OC_STACK_ERROR;
2578 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2580 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2581 cbData.cb = &SRPGetCredResourceCB;
2582 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2583 if (NULL == GetSecData)
2585 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2586 return OC_STACK_NO_MEMORY;
2588 GetSecData->deviceInfo = selectedDeviceInfo;
2589 GetSecData->resultCallback = resultCallback;
2590 GetSecData->numOfResults=0;
2591 GetSecData->ctx = ctx;
2593 int noOfRiCalls = 1;
2594 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2595 if (NULL == GetSecData->resArr)
2597 OICFree(GetSecData);
2598 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2599 return OC_STACK_NO_MEMORY;
2601 cbData.context = (void *)GetSecData;
2603 OCMethod method = OC_REST_GET;
2604 OCDoHandle handle = NULL;
2605 OIC_LOG(DEBUG, TAG, "Sending Get Cred to resource server");
2606 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2607 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2608 if (OC_STACK_OK != ret)
2610 OIC_LOG(ERROR, TAG, "OCStack resource error");
2611 OICFree(GetSecData->resArr);
2612 OICFree(GetSecData);
2614 VERIFY_SUCCESS_RET(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2615 OIC_LOG(DEBUG, TAG, "OUT SRPGetCredResource");
2621 * Internal Function to store results in result array during GetACLResourceCB.
2623 static void registerResultForGetACLResourceCB(GetSecData_t *GetSecData,
2624 OCStackResult stackresult)
2626 OIC_LOG_V(INFO, TAG, "Inside registerResultForGetACLResourceCB "
2627 "GetSecData->numOfResults is %d\n", GetSecData->numOfResults);
2628 memcpy(GetSecData->resArr[(GetSecData->numOfResults)].deviceId.id,
2629 GetSecData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
2630 GetSecData->resArr[(GetSecData->numOfResults)].res = stackresult;
2631 ++(GetSecData->numOfResults);
2635 * Callback handler of SRPGetACLResource.
2637 * @param[in] ctx ctx value passed to callback from calling function.
2638 * @param[in] UNUSED handle to an invocation
2639 * @param[in] clientResponse Response from queries to remote servers.
2640 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
2641 * and OC_STACK_KEEP_TRANSACTION to keep it.
2643 static OCStackApplicationResult SRPGetACLResourceCB(void *ctx, OCDoHandle UNUSED,
2644 OCClientResponse *clientResponse)
2646 OIC_LOG_V(INFO, TAG, "Inside SRPGetACLResourceCB.");
2648 VERIFY_NON_NULL_RET(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
2649 GetSecData_t *GetSecData = (GetSecData_t*)ctx;
2650 OCProvisionResultCB resultCallback = GetSecData->resultCallback;
2654 if(OC_STACK_OK == clientResponse->result)
2656 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
2657 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
2659 OIC_LOG_BUFFER(DEBUG, TAG, payload, size);
2663 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2664 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2667 OICFree(GetSecData->resArr);
2668 OICFree(GetSecData);
2670 return OC_STACK_DELETE_TRANSACTION;
2673 registerResultForGetACLResourceCB(GetSecData, OC_STACK_OK);
2674 ((OCProvisionResultCB)(resultCallback))(GetSecData->ctx, GetSecData->numOfResults,
2677 OIC_LOG_V(ERROR, TAG, "SRPGetACLResourceCB received Null clientResponse");
2678 OICFree(GetSecData->resArr);
2679 OICFree(GetSecData);
2681 return OC_STACK_DELETE_TRANSACTION;
2685 OCStackResult SRPGetACLResource(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
2686 OCProvisionResultCB resultCallback)
2688 VERIFY_NON_NULL_RET(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
2689 VERIFY_NON_NULL_RET(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
2691 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
2692 if(!PMGenerateQuery(true,
2693 selectedDeviceInfo->endpoint.addr,
2694 selectedDeviceInfo->securePort,
2695 selectedDeviceInfo->connType,
2696 query, sizeof(query), OIC_RSRC_ACL_URI))
2698 OIC_LOG(ERROR, TAG, "SRPGetACLResource : Failed to generate query");
2699 return OC_STACK_ERROR;
2701 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
2703 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
2704 cbData.cb = &SRPGetACLResourceCB;
2705 GetSecData_t* GetSecData = (GetSecData_t*)OICCalloc(1, sizeof(GetSecData_t));
2706 if (NULL == GetSecData)
2708 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2709 return OC_STACK_NO_MEMORY;
2711 GetSecData->deviceInfo = selectedDeviceInfo;
2712 GetSecData->resultCallback = resultCallback;
2713 GetSecData->numOfResults=0;
2714 GetSecData->ctx = ctx;
2716 int noOfRiCalls = 1;
2717 GetSecData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
2718 if (NULL == GetSecData->resArr)
2720 OICFree(GetSecData);
2721 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
2722 return OC_STACK_NO_MEMORY;
2724 cbData.context = (void *)GetSecData;
2726 OCMethod method = OC_REST_GET;
2727 OCDoHandle handle = NULL;
2728 OIC_LOG(DEBUG, TAG, "Sending Get ACL to resource server");
2729 OCStackResult ret = OCDoResource(&handle, method, query, NULL, NULL,
2730 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
2731 if (OC_STACK_OK != ret)
2733 OIC_LOG(ERROR, TAG, "OCStack resource error");
2734 OICFree(GetSecData->resArr);
2735 OICFree(GetSecData);
2737 VERIFY_SUCCESS_RET(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
2738 OIC_LOG(DEBUG, TAG, "OUT SRPGetACLResource");
2743 OCStackResult SRPReadTrustCertChain(uint16_t credId, uint8_t **trustCertChain,
2746 OIC_LOG(DEBUG, TAG, "IN SRPReadTrustCertChain");
2748 OCStackResult res = OC_STACK_ERROR;
2750 OicSecCred_t* credData = GetCredEntryByCredId(credId);
2753 res = CredToCBORPayload((const OicSecCred_t*) credData, trustCertChain,
2754 chainSize, secureFlag);
2755 if(OC_STACK_OK != res)
2757 OIC_LOG(INFO, TAG, "CredToCBORPayload failed");
2760 DeleteCredList(credData);