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 * *****************************************************************/
25 #include "ocprovisioningmanager.h"
26 #include "secureresourceprovider.h"
28 #include "oic_malloc.h"
29 #include "aclresource.h"
30 #include "pstatresource.h"
31 #include "srmresourcestrings.h"
32 #include "credresource.h"
33 #include "doxmresource.h"
34 #include "pconfresource.h"
35 #include "credentialgenerator.h"
36 #include "cainterface.h"
39 #include "pmutility.h"
40 #include "provisioningdatabasemanager.h"
43 #include "ocpayload.h"
46 #include "crlresource.h"
52 * Macro to verify argument is not equal to NULL.
53 * eg: VERIFY_NON_NULL(TAG, ptrData, ERROR,OC_STACK_ERROR);
55 #define VERIFY_NON_NULL(tag, arg, logLevel, retValue) { if (NULL == (arg)) \
56 { OIC_LOG((logLevel), tag, #arg " is NULL"); return retValue; } }
59 * Macro to verify success of operation.
60 * eg: VERIFY_SUCCESS(TAG, OC_STACK_OK == foo(), ERROR, OC_STACK_ERROR);
62 #define VERIFY_SUCCESS(tag, op, logLevel, retValue) { if (!(op)) \
63 {OIC_LOG((logLevel), tag, #op " failed!!"); return retValue;} }
66 * Structure to carry credential data to callback.
68 typedef struct CredentialData CredentialData_t;
71 void *ctx; /**< Pointer to user context.**/
72 const OCProvisionDev_t *deviceInfo1; /**< Pointer to OCProvisionDev_t.**/
73 const OCProvisionDev_t *deviceInfo2; /**< Pointer to OCProvisionDev_t.**/
74 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
75 OicSecCred_t *credInfoFirst; /**< Pointer to OicSecCred_t.**/
76 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
77 OCProvisionResult_t *resArr; /**< Result array.**/
78 int numOfResults; /**< Number of results in result array.**/
82 * Structure to carry ACL provision API data to callback.
84 typedef struct ACLData ACLData_t;
87 void *ctx; /**< Pointer to user context.**/
88 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
89 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
90 OCProvisionResult_t *resArr; /**< Result array.**/
91 int numOfResults; /**< Number of results in result array.**/
95 * Structure to carry PCONF provision API data to callback.
97 typedef struct PconfData PconfData_t;
100 void *ctx; /**< Pointer to user context.**/
101 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
102 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
103 OCProvisionResult_t *resArr; /**< Result array.**/
104 int numOfResults; /**< Number of results in result array.**/
107 // Enum type index for unlink callback.
109 IDX_FIRST_DEVICE_RES = 0, // index for resulf of the first device
110 IDX_SECOND_DEVICE_RES, // index for result of the second device
111 IDX_DB_UPDATE_RES // index for result of updating provisioning database.
114 // Structure to carry unlink APIs data to callback.
115 typedef struct UnlinkData UnlinkData_t;
118 OCProvisionDev_t* unlinkDev; /**< Pointer to OCProvisionDev_t to be unlinked.**/
119 OCProvisionResult_t* unlinkRes; /**< Result array.**/
120 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
121 int numOfResults; /**< Number of results in result array.**/
124 //Example of DELETE cred request -> coaps://0.0.0.0:5684/oic/sec/cred?sub=(BASE64 ENCODED UUID)
125 const char * SRP_FORM_DELETE_CREDENTIAL = "coaps://[%s]:%d%s?%s=%s";
127 // Structure to carry remove APIs data to callback.
128 typedef struct RemoveData RemoveData_t;
131 OCProvisionDev_t* revokeTargetDev; /**< Device which is going to be revoked..**/
132 OCProvisionDev_t* linkedDevList; /**< A list of devices which have invalid credential.**/
133 OCProvisionResult_t* removeRes; /**< Result array.**/
134 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
135 size_t numOfResults; /**< Number of results in result array.**/
136 size_t sizeOfResArray;
143 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
144 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
145 OCClientResponseHandler responseHandler);
149 * Internal function to update result in result array.
151 static void registerResultForCredProvisioning(CredentialData_t *credData,
152 OCStackResult stackresult, int cause)
155 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",credData->numOfResults);
158 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
159 credData->deviceInfo1->doxm->deviceID.id,UUID_LENGTH);
163 memcpy(credData->resArr[(credData->numOfResults)].deviceId.id,
164 credData->deviceInfo2->doxm->deviceID.id,UUID_LENGTH);
166 credData->resArr[(credData->numOfResults)].res = stackresult;
167 ++(credData->numOfResults);
171 * Callback handler for handling callback of provisioning device 2.
173 * @param[in] ctx ctx value passed to callback from calling function.
174 * @param[in] UNUSED handle to an invocation
175 * @param[in] clientResponse Response from queries to remote servers.
176 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
177 * and OC_STACK_KEEP_TRANSACTION to keep it.
179 static OCStackApplicationResult provisionCredentialCB2(void *ctx, OCDoHandle UNUSED,
180 OCClientResponse *clientResponse)
182 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
183 CredentialData_t *credData = (CredentialData_t *) ctx;
186 OCProvisionResultCB resultCallback = credData->resultCallback;
187 OIC_LOG(INFO, TAG, "provisionCredentialCB2 called");
190 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
192 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CREATED, 2);
193 OCStackResult res = PDMLinkDevices(&credData->deviceInfo1->doxm->deviceID,
194 &credData->deviceInfo2->doxm->deviceID);
195 if (OC_STACK_OK != res)
197 OIC_LOG(ERROR, TAG, "Error occured on PDMLinkDevices");
198 return OC_STACK_DELETE_TRANSACTION;
200 OIC_LOG(INFO, TAG, "Link created successfully");
202 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
205 OICFree(credData->resArr);
207 return OC_STACK_DELETE_TRANSACTION;
211 OIC_LOG(INFO, TAG, "provisionCredentialCB2 received Null clientResponse");
212 registerResultForCredProvisioning(credData, OC_STACK_ERROR, 2);
213 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
216 OICFree(credData->resArr);
218 return OC_STACK_DELETE_TRANSACTION;
222 * Callback handler for handling callback of provisioning device 1.
224 * @param[in] ctx ctx value passed to callback from calling function.
225 * @param[in] UNUSED handle to an invocation
226 * @param[in] clientResponse Response from queries to remote servers.
227 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
228 * and OC_STACK_KEEP_TRANSACTION to keep it.
230 static OCStackApplicationResult provisionCredentialCB1(void *ctx, OCDoHandle UNUSED,
231 OCClientResponse *clientResponse)
233 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
235 CredentialData_t* credData = (CredentialData_t*) ctx;
236 OICFree(credData->credInfoFirst);
237 const OCProvisionDev_t *deviceInfo = credData->deviceInfo2;
238 OicSecCred_t *credInfo = credData->credInfo;
239 const OCProvisionResultCB resultCallback = credData->resultCallback;
242 if (OC_STACK_RESOURCE_CREATED == clientResponse->result)
244 // send credentials to second device
245 registerResultForCredProvisioning(credData, OC_STACK_RESOURCE_CREATED,1);
246 OCStackResult res = provisionCredentials(credInfo, deviceInfo, credData,
247 provisionCredentialCB2);
248 DeleteCredList(credInfo);
249 if (OC_STACK_OK != res)
251 registerResultForCredProvisioning(credData, res,2);
252 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
255 OICFree(credData->resArr);
262 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
263 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
266 OICFree(credData->resArr);
273 OIC_LOG(INFO, TAG, "provisionCredentialCB received Null clientResponse for first device");
274 registerResultForCredProvisioning(credData, OC_STACK_ERROR,1);
275 ((OCProvisionResultCB)(resultCallback))(credData->ctx, credData->numOfResults,
278 DeleteCredList(credInfo);
279 OICFree(credData->resArr);
283 return OC_STACK_DELETE_TRANSACTION;
289 * Internal function for handling credential generation and sending credential to resource server.
291 * @param[in] cred Instance of cred resource.
292 * @param[in] deviceInfo information about device to which credential is to be provisioned.
293 * @param[in] responseHandler callbak called by OC stack when request API receives response.
294 * @return OC_STACK_OK in case of success and other value otherwise.
296 static OCStackResult provisionCredentials(const OicSecCred_t *cred,
297 const OCProvisionDev_t *deviceInfo, CredentialData_t *credData,
298 OCClientResponseHandler responseHandler)
300 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
303 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
304 return OC_STACK_NO_MEMORY;
306 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
307 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData1, &secPayload->payloadSize);
308 if((OC_STACK_OK != res) && (NULL == secPayload->securityData1))
310 OCPayloadDestroy((OCPayload *)secPayload);
311 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
312 return OC_STACK_NO_MEMORY;
315 OIC_LOG_V(INFO, TAG, "Credential for provisioning : %s",secPayload->securityData1);
316 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
317 if(!PMGenerateQuery(true,
318 deviceInfo->endpoint.addr,
319 deviceInfo->securePort,
320 deviceInfo->connType,
321 query, sizeof(query), OIC_RSRC_CRED_URI))
323 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
324 return OC_STACK_ERROR;
326 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
328 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
329 cbData.cb = responseHandler;
330 cbData.context = (void *) credData;
333 OCDoHandle handle = NULL;
334 OCMethod method = OC_REST_POST;
335 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
336 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
337 OIC_LOG_V(INFO, TAG, "OCDoResource::Credential provisioning returned : %d",ret);
338 if (ret != OC_STACK_OK)
340 OIC_LOG(ERROR, TAG, "OCStack resource error");
348 * Structure to carry certificate data to callback.
350 typedef struct CertificateData CertData_t;
351 struct CertificateData
353 void *ctx; /**< Pointer to user context.**/
354 const OCProvisionDev_t *deviceInfo; /**< Pointer to OCProvisionDev_t.**/
355 OicSecCred_t *credInfo; /**< Pointer to OicSecCred_t.**/
356 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
357 OCProvisionResult_t *resArr; /**< Result array.**/
358 int numOfResults; /**< Number of results in result array.**/
362 * Structure to carry CRL provision API data to callback.
364 typedef struct CRLData CRLData_t;
367 void *ctx; /**< Pointer to user context.**/
368 const OCProvisionDev_t *deviceInfo; /**< Pointer to PMDevInfo_t.**/
369 OCProvisionResultCB resultCallback; /**< Pointer to result callback.**/
370 OCProvisionResult_t *resArr; /**< Result array.**/
371 int numOfResults; /**< Number of results in result array.**/
375 * Internal function to update result in result array.
377 static void registerResultForCertProvisioning(CertData_t *certData,
378 OCStackResult stackresult)
381 OIC_LOG_V(INFO,TAG,"value of credData->numOfResults is %d",certData->numOfResults);
382 memcpy(certData->resArr[(certData->numOfResults)].deviceId.id,
383 certData->deviceInfo->doxm->deviceID.id,UUID_LENGTH);
384 certData->resArr[(certData->numOfResults)].res = stackresult;
385 ++(certData->numOfResults);
389 * Internal Function to store results in result array during ACL provisioning.
391 static void registerResultForCRLProvisioning(CRLData_t *crlData,
392 OCStackResult stackresult)
394 OIC_LOG_V(INFO, TAG, "Inside registerResultForCRLProvisioning crlData->numOfResults is %d\n",
395 crlData->numOfResults);
396 memcpy(crlData->resArr[(crlData->numOfResults)].deviceId.id,
397 crlData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
398 crlData->resArr[(crlData->numOfResults)].res = stackresult;
399 ++(crlData->numOfResults);
404 * Callback handler of SRPProvisionCRL.
406 * @param[in] ctx ctx value passed to callback from calling function.
407 * @param[in] UNUSED handle to an invocation
408 * @param[in] clientResponse Response from queries to remote servers.
409 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
410 * and OC_STACK_KEEP_TRANSACTION to keep it.
412 static OCStackApplicationResult SRPProvisionCRLCB(void *ctx, OCDoHandle UNUSED,
413 OCClientResponse *clientResponse)
415 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionCRLCB.");
417 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
418 CRLData_t *crlData = (CRLData_t*)ctx;
419 OCProvisionResultCB resultCallback = crlData->resultCallback;
423 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
425 registerResultForCRLProvisioning(crlData, OC_STACK_RESOURCE_CREATED);
426 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
429 OICFree(crlData->resArr);
431 return OC_STACK_DELETE_TRANSACTION;
434 registerResultForCRLProvisioning(crlData, OC_STACK_ERROR);
435 ((OCProvisionResultCB)(resultCallback))(crlData->ctx, crlData->numOfResults,
438 OIC_LOG_V(ERROR, TAG, "SRPProvisionCRLCB received Null clientResponse");
439 OICFree(crlData->resArr);
441 return OC_STACK_DELETE_TRANSACTION;
444 OCStackResult SRPProvisionCRL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
445 OicSecCrl_t *crl, OCProvisionResultCB resultCallback)
447 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
448 VERIFY_NON_NULL(TAG, crl, ERROR, OC_STACK_INVALID_PARAM);
449 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
451 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
454 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
455 return OC_STACK_NO_MEMORY;
458 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
459 OCStackResult res = CrlToCBORPayload(crl, &secPayload->securityData1, &secPayload->payloadSize);
460 if((OC_STACK_OK != res) && (NULL == secPayload->securityData1))
463 OIC_LOG(ERROR, TAG, "Failed to BinToCrlJSON");
464 return OC_STACK_NO_MEMORY;
466 OIC_LOG_V(INFO, TAG, "CRL : %s", secPayload->securityData1);
468 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
469 if(!PMGenerateQuery(true,
470 selectedDeviceInfo->endpoint.addr,
471 selectedDeviceInfo->securePort,
472 selectedDeviceInfo->connType,
473 query, sizeof(query), OIC_RSRC_CRL_URI))
475 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
476 OCPayloadDestroy((OCPayload *)secPayload);
477 return OC_STACK_ERROR;
479 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
481 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
482 cbData.cb = &SRPProvisionCRLCB;
483 CRLData_t *crlData = (CRLData_t *) OICCalloc(1, sizeof(CRLData_t));
486 OCPayloadDestroy((OCPayload *)secPayload);
487 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
488 return OC_STACK_NO_MEMORY;
491 crlData->deviceInfo = selectedDeviceInfo;
492 crlData->resultCallback = resultCallback;
493 crlData->numOfResults=0;
496 crlData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
497 if (crlData->resArr == NULL)
499 OCPayloadDestroy((OCPayload *)secPayload);
500 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
501 return OC_STACK_NO_MEMORY;
504 cbData.context = (void *)crlData;
506 OCMethod method = OC_REST_POST;
507 OCDoHandle handle = NULL;
508 OIC_LOG(DEBUG, TAG, "Sending CRL info to resource server");
510 OCStackResult ret = OCDoResource(&handle, method, query,
511 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
512 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
514 if (ret != OC_STACK_OK)
516 OICFree(crlData->resArr);
524 * Internal function for handling credential generation and sending cretificate credential.
526 * @param[in] cred Instance of cred resource.
527 * @param[in] deviceInfo information about device to which credential is to be provisioned.
528 * @param[in] responseHandler callbak called by OC stack when request API receives response.
529 * @return OC_STACK_OK in case of success and other value otherwise.
531 static OCStackResult provisionCertCred(const OicSecCred_t *cred,
532 const OCProvisionDev_t *deviceInfo, CertData_t *certData,
533 OCClientResponseHandler responseHandler)
535 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
538 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
539 return OC_STACK_NO_MEMORY;
541 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
542 OCStackResult res = CredToCBORPayload(cred, &secPayload->securityData1,
543 &secPayload->payloadSize);
545 if ((OC_STACK_OK != res) || (NULL == secPayload->securityData1))
548 OIC_LOG(ERROR, TAG, "Failed to CredToCBORPayload");
549 return OC_STACK_NO_MEMORY;
552 OIC_LOG_V(INFO, TAG, "Credential for provisioning : %s",secPayload->securityData1);
553 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
554 if(!PMGenerateQuery(true,
555 deviceInfo->endpoint.addr,
556 deviceInfo->securePort,
557 deviceInfo->connType,
558 query, sizeof(query), OIC_RSRC_CRED_URI))
560 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
561 OCPayloadDestroy((OCPayload *)secPayload);
562 return OC_STACK_ERROR;
564 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
566 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
567 cbData.cb = responseHandler;
568 cbData.context = (void *) certData;
571 OCDoHandle handle = NULL;
572 OCMethod method = OC_REST_POST;
573 OCStackResult ret = OCDoResource(&handle, method, query, 0, (OCPayload*)secPayload,
574 deviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
575 OIC_LOG_V(INFO, TAG, "OCDoResource::Certificate provisioning returned : %d",ret);
576 if (ret != OC_STACK_OK)
578 OIC_LOG(ERROR, TAG, "OCStack resource error");
585 * Callback handler for handling callback of certificate provisioning device.
587 * @param[in] ctx ctx value passed to callback from calling function.
588 * @param[in] UNUSED handle to an invocation
589 * @param[in] clientResponse Response from queries to remote servers.
590 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
591 * and OC_STACK_KEEP_TRANSACTION to keep it.
593 static OCStackApplicationResult provisionCertCB(void *ctx, OCDoHandle UNUSED,
594 OCClientResponse *clientResponse)
596 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
597 CertData_t *certData = (CertData_t *) ctx;
600 OCProvisionResultCB resultCallback = certData->resultCallback;
601 OIC_LOG(INFO, TAG, "provisionCertCred called");
604 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
606 registerResultForCertProvisioning(certData, OC_STACK_RESOURCE_CREATED);
607 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
610 OICFree(certData->resArr);
612 return OC_STACK_DELETE_TRANSACTION;
616 OIC_LOG(INFO, TAG, "provisionCertCredCB received Null clientResponse");
617 registerResultForCertProvisioning(certData, OC_STACK_ERROR);
618 ((OCProvisionResultCB)(resultCallback))(certData->ctx, certData->numOfResults,
621 OICFree(certData->resArr);
623 return OC_STACK_DELETE_TRANSACTION;
625 #endif // __WITH_X509__
627 OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize,
628 const OCProvisionDev_t *pDev1,
629 const OCProvisionDev_t *pDev2,
630 OCProvisionResultCB resultCallback)
632 VERIFY_NON_NULL(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM);
633 if (SYMMETRIC_PAIR_WISE_KEY == type)
635 VERIFY_NON_NULL(TAG, pDev2, ERROR, OC_STACK_INVALID_PARAM);
637 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
640 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
641 return OC_STACK_INVALID_CALLBACK;
643 if (SYMMETRIC_PAIR_WISE_KEY == type &&
644 0 == memcmp(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, sizeof(OicUuid_t)))
646 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
647 return OC_STACK_INVALID_PARAM;
650 if (SYMMETRIC_PAIR_WISE_KEY == type &&
651 !(OWNER_PSK_LENGTH_128 == keySize || OWNER_PSK_LENGTH_256 == keySize))
653 OIC_LOG(INFO, TAG, "Invalid key size");
654 return OC_STACK_INVALID_PARAM;
657 OIC_LOG(INFO, TAG, "In SRPProvisionCredentials");
659 if (SYMMETRIC_PAIR_WISE_KEY == type)
661 bool linkExisits = true;
662 OCStackResult res = PDMIsLinkExists(&pDev1->doxm->deviceID, &pDev2->doxm->deviceID, &linkExisits);
664 if (res != OC_STACK_OK)
666 OIC_LOG(ERROR, TAG, "Internal error occured");
671 OIC_LOG(ERROR, TAG, "Link already exists");
672 return OC_STACK_INVALID_PARAM;
676 OicUuid_t provTooldeviceID = {{0,}};
677 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
679 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
680 return OC_STACK_ERROR;
682 OIC_LOG(INFO, TAG, "retrieved deviceid");
685 case SYMMETRIC_PAIR_WISE_KEY:
687 const OCProvisionDev_t *firstDevice = pDev1;
688 const OCProvisionDev_t *secondDevice = pDev2;
690 OicSecCred_t *firstCred = NULL;
691 OicSecCred_t *secondCred = NULL;
692 OCStackResult res = PMGeneratePairWiseCredentials(type, keySize, &provTooldeviceID,
693 &firstDevice->doxm->deviceID, &secondDevice->doxm->deviceID,
694 &firstCred, &secondCred);
695 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
696 OIC_LOG(INFO, TAG, "Credentials generated successfully");
697 CredentialData_t *credData =
698 (CredentialData_t *) OICCalloc(1, sizeof(CredentialData_t));
699 if (NULL == credData)
703 OIC_LOG(ERROR, TAG, "Memory allocation problem");
704 return OC_STACK_NO_MEMORY;
706 credData->deviceInfo1 = firstDevice;
707 credData->deviceInfo2 = secondDevice;
708 credData->credInfo = secondCred;
710 credData->credInfoFirst = firstCred;
711 credData->numOfResults = 0;
712 credData->resultCallback = resultCallback;
713 // first call to provision creds to device1.
714 // second call to provision creds to device2.
717 (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
718 if (NULL == credData->resArr)
723 OIC_LOG(ERROR, TAG, "Memory allocation problem");
724 return OC_STACK_NO_MEMORY;
726 res = provisionCredentials(firstCred, firstDevice, credData, &provisionCredentialCB1);
727 if (OC_STACK_OK != res)
729 DeleteCredList(firstCred);
730 DeleteCredList(secondCred);
731 OICFree(credData->resArr);
734 OIC_LOG_V(INFO, TAG, "provisionCredentials returned: %d",res);
735 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
739 case SIGNED_ASYMMETRIC_KEY:
741 const OCProvisionDev_t *firstDevice = pDev1;
742 OicSecCred_t *cred = NULL;
743 OCStackResult res = PMGenerateCertificateCredentials(&provTooldeviceID,
744 &firstDevice->doxm->deviceID,&cred);
745 VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR);
746 OIC_LOG(INFO, TAG, "Certificate credentials generated successfully");
747 CertData_t *certData = (CertData_t *) OICCalloc(1, sizeof(CertData_t));
748 if (NULL == certData)
751 OIC_LOG(ERROR, TAG, "Memory allocation problem");
752 return OC_STACK_NO_MEMORY;
755 certData->deviceInfo = firstDevice;
757 certData->credInfo = cred;
758 certData->numOfResults = 0;
759 certData->resultCallback = resultCallback;
761 certData->resArr = (OCProvisionResult_t*)OICCalloc(1, sizeof(OCProvisionResult_t));
762 if (NULL == certData->resArr)
764 DeleteCredList(cred);
766 OIC_LOG(ERROR, TAG, "Memory allocation problem");
767 return OC_STACK_NO_MEMORY;
770 res = provisionCertCred(cred, firstDevice, certData, &provisionCertCB);
771 if (OC_STACK_OK != res)
773 OICFree(certData->resArr);
776 DeleteCredList(cred);
777 OIC_LOG_V(INFO, TAG, "provisionCertCredentials returned: %d",res);
784 OIC_LOG(ERROR, TAG, "Invalid option.");
785 return OC_STACK_INVALID_PARAM;
788 return OC_STACK_ERROR;
792 * Internal Function to store results in result array during ACL provisioning.
794 static void registerResultForACLProvisioning(ACLData_t *aclData,
795 OCStackResult stackresult)
797 OIC_LOG_V(INFO, TAG, "Inside registerResultForACLProvisioning aclData->numOfResults is %d\n",
798 aclData->numOfResults);
799 memcpy(aclData->resArr[(aclData->numOfResults)].deviceId.id,
800 aclData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
801 aclData->resArr[(aclData->numOfResults)].res = stackresult;
802 ++(aclData->numOfResults);
806 * Callback handler of SRPProvisionACL.
808 * @param[in] ctx ctx value passed to callback from calling function.
809 * @param[in] UNUSED handle to an invocation
810 * @param[in] clientResponse Response from queries to remote servers.
811 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
812 * and OC_STACK_KEEP_TRANSACTION to keep it.
814 static OCStackApplicationResult SRPProvisionACLCB(void *ctx, OCDoHandle UNUSED,
815 OCClientResponse *clientResponse)
817 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionACLCB.");
819 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
820 ACLData_t *aclData = (ACLData_t*)ctx;
821 OCProvisionResultCB resultCallback = aclData->resultCallback;
825 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
827 registerResultForACLProvisioning(aclData, OC_STACK_RESOURCE_CREATED);
828 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
831 OICFree(aclData->resArr);
833 return OC_STACK_DELETE_TRANSACTION;
836 registerResultForACLProvisioning(aclData, OC_STACK_ERROR);
837 ((OCProvisionResultCB)(resultCallback))(aclData->ctx, aclData->numOfResults,
840 OIC_LOG_V(ERROR, TAG, "SRPProvisionACLCB received Null clientResponse");
841 OICFree(aclData->resArr);
843 return OC_STACK_DELETE_TRANSACTION;
846 OCStackResult SRPProvisionACL(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
847 OicSecAcl_t *acl, OCProvisionResultCB resultCallback)
849 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
850 VERIFY_NON_NULL(TAG, acl, ERROR, OC_STACK_INVALID_PARAM);
851 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
853 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
856 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
857 return OC_STACK_NO_MEMORY;
859 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
860 if(OC_STACK_OK != AclToCBORPayload(acl, &secPayload->securityData1, &secPayload->payloadSize))
862 OCPayloadDestroy((OCPayload *)secPayload);
863 OIC_LOG(ERROR, TAG, "Failed to AclToCBORPayload");
864 return OC_STACK_NO_MEMORY;
866 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
867 if(!PMGenerateQuery(true,
868 selectedDeviceInfo->endpoint.addr,
869 selectedDeviceInfo->securePort,
870 selectedDeviceInfo->connType,
871 query, sizeof(query), OIC_RSRC_ACL_URI))
873 OIC_LOG(ERROR, TAG, "DeviceDiscoveryHandler : Failed to generate query");
874 return OC_STACK_ERROR;
876 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
878 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
879 cbData.cb = &SRPProvisionACLCB;
880 ACLData_t *aclData = (ACLData_t *) OICCalloc(1, sizeof(ACLData_t));
883 OCPayloadDestroy((OCPayload *)secPayload);
884 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
885 return OC_STACK_NO_MEMORY;
887 aclData->deviceInfo = selectedDeviceInfo;
888 aclData->resultCallback = resultCallback;
889 aclData->numOfResults=0;
891 // call to provision ACL to device1.
893 aclData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
894 if (aclData->resArr == NULL)
897 OCPayloadDestroy((OCPayload *)secPayload);
898 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
899 return OC_STACK_NO_MEMORY;
901 cbData.context = (void *)aclData;
903 OCMethod method = OC_REST_POST;
904 OCDoHandle handle = NULL;
905 OIC_LOG(DEBUG, TAG, "Sending ACL info to resource server");
906 OCStackResult ret = OCDoResource(&handle, method, query,
907 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
908 selectedDeviceInfo->connType, OC_HIGH_QOS, &cbData, NULL, 0);
909 if (ret != OC_STACK_OK)
911 OICFree(aclData->resArr);
914 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
919 * Internal Function to store results in result array during Direct-Pairing provisioning.
921 static void registerResultForDirectPairingProvisioning(PconfData_t *pconfData,
922 OCStackResult stackresult)
924 OIC_LOG_V(INFO, TAG, "Inside registerResultForDirectPairingProvisioning "
925 "pconfData->numOfResults is %d\n", pconfData->numOfResults);
926 memcpy(pconfData->resArr[(pconfData->numOfResults)].deviceId.id,
927 pconfData->deviceInfo->doxm->deviceID.id, UUID_LENGTH);
928 pconfData->resArr[(pconfData->numOfResults)].res = stackresult;
929 ++(pconfData->numOfResults);
933 * Callback handler of SRPProvisionDirectPairing.
935 * @param[in] ctx ctx value passed to callback from calling function.
936 * @param[in] UNUSED handle to an invocation
937 * @param[in] clientResponse Response from queries to remote servers.
938 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
939 * and OC_STACK_KEEP_TRANSACTION to keep it.
941 static OCStackApplicationResult SRPProvisionDirectPairingCB(void *ctx, OCDoHandle UNUSED,
942 OCClientResponse *clientResponse)
944 OIC_LOG_V(INFO, TAG, "Inside SRPProvisionDirectPairingCB.");
946 VERIFY_NON_NULL(TAG, ctx, ERROR, OC_STACK_DELETE_TRANSACTION);
947 PconfData_t *pconfData = (PconfData_t*)ctx;
948 OCProvisionResultCB resultCallback = pconfData->resultCallback;
952 if(OC_STACK_RESOURCE_CREATED == clientResponse->result)
954 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_RESOURCE_CREATED);
955 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
958 OICFree(pconfData->resArr);
960 return OC_STACK_DELETE_TRANSACTION;
963 registerResultForDirectPairingProvisioning(pconfData, OC_STACK_ERROR);
964 ((OCProvisionResultCB)(resultCallback))(pconfData->ctx, pconfData->numOfResults,
967 OIC_LOG_V(ERROR, TAG, "SRPProvisionDirectPairingCB received Null clientResponse");
968 OICFree(pconfData->resArr);
970 return OC_STACK_DELETE_TRANSACTION;
973 OCStackResult SRPProvisionDirectPairing(void *ctx, const OCProvisionDev_t *selectedDeviceInfo,
974 OicSecPconf_t *pconf, OCProvisionResultCB resultCallback)
976 VERIFY_NON_NULL(TAG, selectedDeviceInfo, ERROR, OC_STACK_INVALID_PARAM);
977 VERIFY_NON_NULL(TAG, pconf, ERROR, OC_STACK_INVALID_PARAM);
978 VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK);
980 // check direct-pairing capability
981 if (true != selectedDeviceInfo->doxm->dpc)
983 OIC_LOG(ERROR, TAG, "Resouce server does not have Direct-Pairing Capability ");
984 return OC_STACK_UNAUTHORIZED_REQ;
987 OicUuid_t provTooldeviceID = {.id={0}};
988 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
990 OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
991 return OC_STACK_ERROR;
993 memcpy(&pconf->rowner, &provTooldeviceID, sizeof(OicUuid_t));
995 OCSecurityPayload* secPayload = (OCSecurityPayload*)OICCalloc(1, sizeof(OCSecurityPayload));
998 OIC_LOG(ERROR, TAG, "Failed to memory allocation");
999 return OC_STACK_NO_MEMORY;
1001 secPayload->base.type = PAYLOAD_TYPE_SECURITY;
1003 if (OC_STACK_OK != PconfToCBORPayload(pconf, &(secPayload->securityData1),
1004 &(secPayload->payloadSize)))
1006 OCPayloadDestroy((OCPayload*)secPayload);
1007 OIC_LOG(ERROR, TAG, "Failed to PconfToCborPayload");
1008 return OC_STACK_NO_MEMORY;
1010 OIC_LOG_V(INFO, TAG, "PCONF : %s", secPayload->securityData1);
1012 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1013 if(!PMGenerateQuery(true,
1014 selectedDeviceInfo->endpoint.addr,
1015 selectedDeviceInfo->securePort,
1016 selectedDeviceInfo->connType,
1017 query, sizeof(query), OIC_RSRC_PCONF_URI))
1019 OIC_LOG(ERROR, TAG, "SRPProvisionDirectPairing : Failed to generate query");
1020 return OC_STACK_ERROR;
1022 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1024 OCCallbackData cbData = {.context=NULL, .cb=NULL, .cd=NULL};
1025 cbData.cb = &SRPProvisionDirectPairingCB;
1026 PconfData_t *pconfData = (PconfData_t *) OICCalloc(1, sizeof(PconfData_t));
1027 if (NULL == pconfData)
1029 OCPayloadDestroy((OCPayload*)secPayload);
1030 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1031 return OC_STACK_NO_MEMORY;
1033 pconfData->deviceInfo = selectedDeviceInfo;
1034 pconfData->resultCallback = resultCallback;
1035 pconfData->numOfResults=0;
1036 pconfData->ctx = ctx;
1037 // call to provision PCONF to device1.
1038 int noOfRiCalls = 1;
1039 pconfData->resArr = (OCProvisionResult_t*)OICCalloc(noOfRiCalls, sizeof(OCProvisionResult_t));
1040 if (NULL == pconfData->resArr)
1043 OCPayloadDestroy((OCPayload*)secPayload);
1044 OIC_LOG(ERROR, TAG, "Unable to allocate memory");
1045 return OC_STACK_NO_MEMORY;
1047 cbData.context = (void *)pconfData;
1049 OCMethod method = OC_REST_POST;
1050 OCDoHandle handle = NULL;
1051 OIC_LOG(DEBUG, TAG, "Sending PCONF info to resource server");
1052 OCStackResult ret = OCDoResource(&handle, method, query,
1053 &selectedDeviceInfo->endpoint, (OCPayload*)secPayload,
1054 selectedDeviceInfo->connType, OC_LOW_QOS, &cbData, NULL, 0);
1055 if (OC_STACK_OK != ret)
1057 OICFree(pconfData->resArr);
1060 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR, OC_STACK_ERROR);
1064 static void DeleteUnlinkData_t(UnlinkData_t *unlinkData)
1068 OICFree(unlinkData->unlinkDev);
1069 OICFree(unlinkData->unlinkRes);
1070 OICFree(unlinkData);
1074 static void registerResultForUnlinkDevices(UnlinkData_t *unlinkData, OCStackResult stackresult,
1077 if (NULL != unlinkData)
1079 OIC_LOG_V(INFO, TAG, "Inside registerResultForUnlinkDevices unlinkData->numOfResults is %d\n",
1080 unlinkData->numOfResults);
1081 OIC_LOG_V(INFO, TAG, "Stack result :: %d", stackresult);
1083 OicUuid_t *pUuid = &unlinkData->unlinkRes[(unlinkData->numOfResults)].deviceId;
1085 // Set result in the result array according to the position (devNum).
1086 if (idx != IDX_DB_UPDATE_RES)
1088 memcpy(pUuid->id, unlinkData->unlinkDev[idx].doxm->deviceID.id, sizeof(pUuid->id));
1091 { // When deivce ID is 000... this means it's the result of database update.
1092 memset(pUuid->id, 0, sizeof(pUuid->id));
1094 unlinkData->unlinkRes[(unlinkData->numOfResults)].res = stackresult;
1095 ++(unlinkData->numOfResults);
1096 OIC_LOG (INFO, TAG, "Out registerResultForUnlinkDevices");
1100 static OCStackResult SendDeleteCredentialRequest(void* ctx,
1101 OCClientResponseHandler respHandler,
1102 const OCProvisionDev_t* revokedDev,
1103 const OCProvisionDev_t* destDev)
1105 OIC_LOG(DEBUG, TAG, "IN SendDeleteCredentialRequest");
1107 if (NULL == ctx || NULL == respHandler || NULL == revokedDev || NULL == destDev)
1109 return OC_STACK_INVALID_PARAM;
1113 OCStackResult ret = ConvertUuidToStr(&revokedDev->doxm->deviceID, &subID);
1114 if(OC_STACK_OK != ret)
1116 OIC_LOG(ERROR, TAG, "SendDeleteCredentialRequest : Failed to canonical UUID encoding");
1117 return OC_STACK_ERROR;
1120 char reqBuf[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1122 //coaps://0.0.0.0:5684/oic/sec/cred?subjectid=(Canonical ENCODED UUID)
1123 snRet = snprintf(reqBuf, sizeof(reqBuf), SRP_FORM_DELETE_CREDENTIAL, destDev->endpoint.addr,
1124 destDev->securePort, OIC_RSRC_CRED_URI, OIC_JSON_SUBJECTID_NAME, subID);
1128 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error (snprintf) %d\n", snRet);
1129 return OC_STACK_ERROR;
1131 else if ((size_t)snRet >= sizeof(reqBuf))
1133 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Truncated (snprintf) %d\n", snRet);
1134 return OC_STACK_ERROR;
1137 OCCallbackData cbData;
1138 memset(&cbData, 0, sizeof(cbData));
1139 cbData.context = ctx;
1140 cbData.cb = respHandler;
1142 OIC_LOG_V(INFO, TAG, "URI: %s",reqBuf);
1144 OIC_LOG(DEBUG, TAG, "Sending remove credential request to resource server");
1146 ret = OCDoResource(NULL, OC_REST_DELETE, reqBuf,
1147 &destDev->endpoint, NULL,
1148 CT_ADAPTER_IP, OC_HIGH_QOS, &cbData, NULL, 0);
1149 if (OC_STACK_OK != ret)
1151 OIC_LOG_V(ERROR, TAG, "SendDeleteCredentialRequest : Error in OCDoResource %d", ret);
1153 OIC_LOG(DEBUG, TAG, "OUT SendDeleteCredentialRequest");
1159 * Callback handler of unlink second device.
1161 * @param[in] ctx ctx value passed to callback from calling function.
1162 * @param[in] handle handle to an invocation
1163 * @param[in] clientResponse Response from queries to remote servers.
1164 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1165 * OC_STACK_KEEP_TRANSACTION to keep it.
1167 static OCStackApplicationResult SRPUnlinkDevice2CB(void *unlinkCtx, OCDoHandle handle,
1168 OCClientResponse *clientResponse)
1171 OIC_LOG(DEBUG, TAG, "IN SRPUnlinkDevice2CB");
1172 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1173 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1177 OIC_LOG(DEBUG, TAG, "Valid client response for device 2");
1178 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_SECOND_DEVICE_RES);
1180 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1182 OIC_LOG(DEBUG, TAG, "Credential of device2 revoked");
1186 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 2");
1187 unlinkData->resultCallback(unlinkData->ctx,
1188 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1194 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1195 IDX_SECOND_DEVICE_RES);
1196 unlinkData->resultCallback(unlinkData->ctx,
1197 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1198 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice2CB received Null clientResponse");
1202 //Update provisioning DB when succes case.
1203 if (OC_STACK_OK != PDMUnlinkDevices(&unlinkData->unlinkDev[0].doxm->deviceID,
1204 &unlinkData->unlinkDev[1].doxm->deviceID))
1206 OIC_LOG(FATAL, TAG, "All requests are successfully done but update provisioning DB FAILED.");
1207 registerResultForUnlinkDevices(unlinkData, OC_STACK_INCONSISTENT_DB, IDX_DB_UPDATE_RES);
1208 unlinkData->resultCallback(unlinkData->ctx,
1209 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1212 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults, unlinkData->unlinkRes,
1216 DeleteUnlinkData_t(unlinkData);
1217 OIC_LOG(DEBUG, TAG, "OUT SRPUnlinkDevice2CB");
1218 return OC_STACK_DELETE_TRANSACTION;
1223 * Callback handler of unlink first device.
1225 * @param[in] ctx ctx value passed to callback from calling function.
1226 * @param[in] handle handle to an invocation
1227 * @param[in] clientResponse Response from queries to remote servers.
1228 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction and
1229 * OC_STACK_KEEP_TRANSACTION to keep it.
1231 static OCStackApplicationResult SRPUnlinkDevice1CB(void *unlinkCtx, OCDoHandle handle,
1232 OCClientResponse *clientResponse)
1234 OIC_LOG_V(INFO, TAG, "Inside SRPUnlinkDevice1CB ");
1235 VERIFY_NON_NULL(TAG, unlinkCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1236 UnlinkData_t* unlinkData = (UnlinkData_t*)unlinkCtx;
1241 OIC_LOG(DEBUG, TAG, "Valid client response for device 1");
1242 registerResultForUnlinkDevices(unlinkData, clientResponse->result, IDX_FIRST_DEVICE_RES);
1244 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1246 OIC_LOG(DEBUG, TAG, "Credential of device 1 is revoked");
1248 // Second revocation request to second device.
1249 OCStackResult res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice2CB,
1250 &unlinkData->unlinkDev[0],
1251 &unlinkData->unlinkDev[1] /*Dest*/);
1252 OIC_LOG_V(DEBUG, TAG, "Credential revocation request device 2, result :: %d",res);
1253 if (OC_STACK_OK != res)
1255 OIC_LOG(ERROR, TAG, "Error while sending revocation request for device 2");
1256 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1257 IDX_SECOND_DEVICE_RES);
1258 unlinkData->resultCallback(unlinkData->ctx,
1259 unlinkData->numOfResults, unlinkData->unlinkRes, true);
1264 OIC_LOG(DEBUG, TAG, "Request for credential revocation successfully sent");
1265 return OC_STACK_DELETE_TRANSACTION;
1270 OIC_LOG(ERROR, TAG, "Unable to delete credential information from device 1");
1272 unlinkData->resultCallback(unlinkData->ctx, unlinkData->numOfResults,
1273 unlinkData->unlinkRes, true);
1279 OIC_LOG(DEBUG, TAG, "Invalid response from server");
1280 registerResultForUnlinkDevices(unlinkData, OC_STACK_INVALID_REQUEST_HANDLE,
1281 IDX_FIRST_DEVICE_RES );
1282 unlinkData->resultCallback(unlinkData->ctx,
1283 unlinkData->numOfResults, unlinkData->unlinkRes,
1285 OIC_LOG(ERROR, TAG, "SRPUnlinkDevice1CB received Null clientResponse");
1289 OIC_LOG_V(INFO, TAG, "Out SRPUnlinkDevice1CB");
1290 DeleteUnlinkData_t(unlinkData);
1291 return OC_STACK_DELETE_TRANSACTION;
1295 * Function to unlink devices.
1296 * This function will remove the credential & relationship between the two devices.
1298 * @param[in] ctx Application context would be returned in result callback
1299 * @param[in] pTargetDev1 first device information to be unlinked.
1300 * @param[in] pTargetDev2 second device information to be unlinked.
1301 * @param[in] resultCallback callback provided by API user, callback will be called when
1302 * device unlink is finished.
1303 * @return OC_STACK_OK in case of success and other value otherwise.
1305 OCStackResult SRPUnlinkDevices(void* ctx,
1306 const OCProvisionDev_t* pTargetDev1,
1307 const OCProvisionDev_t* pTargetDev2,
1308 OCProvisionResultCB resultCallback)
1310 OIC_LOG(INFO, TAG, "IN SRPUnlinkDevices");
1312 if (!pTargetDev1 || !pTargetDev2 || !pTargetDev1->doxm || !pTargetDev2->doxm)
1314 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL parameters");
1315 return OC_STACK_INVALID_PARAM;
1317 if (!resultCallback)
1319 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : NULL Callback");
1320 return OC_STACK_INVALID_CALLBACK;
1322 if (0 == memcmp(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID, sizeof(OicUuid_t)))
1324 OIC_LOG(INFO, TAG, "SRPUnlinkDevices : Same device ID");
1325 return OC_STACK_INVALID_PARAM;
1328 OIC_LOG(INFO, TAG, "Unlinking following devices: ");
1329 PMPrintOCProvisionDev(pTargetDev1);
1330 PMPrintOCProvisionDev(pTargetDev2);
1332 // Mark the link status stale
1333 OCStackResult res = PDMSetLinkStale(&pTargetDev1->doxm->deviceID, &pTargetDev2->doxm->deviceID);
1334 if (OC_STACK_OK != res)
1336 OIC_LOG(FATAL, TAG, "unable to update DB. Try again.");
1340 UnlinkData_t* unlinkData = (UnlinkData_t*)OICCalloc(1, sizeof(UnlinkData_t));
1341 VERIFY_NON_NULL(TAG, unlinkData, ERROR, OC_STACK_NO_MEMORY);
1343 //Initialize unlink data
1344 unlinkData->ctx = ctx;
1345 unlinkData->unlinkDev = (OCProvisionDev_t*)OICCalloc(2, sizeof(OCProvisionDev_t));
1346 if (NULL == unlinkData->unlinkDev)
1348 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1349 res = OC_STACK_NO_MEMORY;
1353 unlinkData->unlinkRes = (OCProvisionResult_t*)OICCalloc(3, sizeof(OCProvisionResult_t));
1354 if (NULL == unlinkData->unlinkRes)
1356 OIC_LOG(ERROR, TAG, "Memory allocation failed");
1357 res = OC_STACK_NO_MEMORY;
1361 memcpy(&unlinkData->unlinkDev[0], pTargetDev1, sizeof(OCProvisionDev_t));
1362 memcpy(&unlinkData->unlinkDev[1], pTargetDev2, sizeof(OCProvisionDev_t));
1364 unlinkData->numOfResults = 0;
1365 unlinkData->resultCallback = resultCallback;
1367 res = SendDeleteCredentialRequest((void*)unlinkData, &SRPUnlinkDevice1CB,
1368 &unlinkData->unlinkDev[1], &unlinkData->unlinkDev[0]);
1369 if (OC_STACK_OK != res)
1371 OIC_LOG(ERROR, TAG, "SRPUnlinkDevices : SendDeleteCredentialRequest failed");
1378 OIC_LOG(INFO, TAG, "OUT SRPUnlinkDevices");
1379 DeleteUnlinkData_t(unlinkData);
1383 static void DeleteRemoveData_t(RemoveData_t* pRemoveData)
1387 OICFree(pRemoveData->revokeTargetDev);
1388 OCDeleteDiscoveredDevices(pRemoveData->linkedDevList);
1389 OICFree(pRemoveData->removeRes);
1390 OICFree(pRemoveData);
1394 static void registerResultForRemoveDevice(RemoveData_t *removeData, OicUuid_t *pLinkedDevId,
1395 OCStackResult stackresult, bool hasError)
1397 OIC_LOG_V(INFO, TAG, "Inside registerResultForRemoveDevice removeData->numOfResults is %zu\n",
1398 removeData->numOfResults + 1);
1401 memcpy(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1402 &pLinkedDevId->id, sizeof(pLinkedDevId->id));
1406 memset(removeData->removeRes[(removeData->numOfResults)].deviceId.id,
1407 0, sizeof(pLinkedDevId->id) );
1409 removeData->removeRes[(removeData->numOfResults)].res = stackresult;
1410 removeData->hasError = hasError;
1411 ++(removeData->numOfResults);
1413 // If we get suffcient result from linked devices, we have to call user callback and do free
1414 if (removeData->sizeOfResArray == removeData->numOfResults)
1416 if(!removeData->hasError)
1418 // Remove device info from prvisioning database
1419 if (OC_STACK_OK != PDMDeleteDevice(&removeData->revokeTargetDev->doxm->deviceID))
1421 OIC_LOG(ERROR, TAG, "ResultForRemoveDevice : Failed to remove device in PDM.");
1422 removeData->hasError = true;
1425 removeData->resultCallback(removeData->ctx, removeData->numOfResults, removeData->removeRes,
1426 removeData->hasError);
1427 DeleteRemoveData_t(removeData);
1432 * Callback handler of unlink first device.
1434 * @param[in] ctx ctx value passed to callback from calling function.
1435 * @param[in] handle handle to an invocation
1436 * @param[in] clientResponse Response from queries to remote servers.
1437 * @return OC_STACK_DELETE_TRANSACTION to delete the transaction
1438 * and OC_STACK_KEEP_TRANSACTION to keep it.
1440 static OCStackApplicationResult SRPRemoveDeviceCB(void *delDevCtx, OCDoHandle handle,
1441 OCClientResponse *clientResponse)
1443 //Update the delete credential into delete device context
1444 //Save the deleted status in delDevCtx
1446 OIC_LOG_V(INFO, TAG, "Inside SRPRemoveDeviceCB.");
1447 VERIFY_NON_NULL(TAG, delDevCtx, ERROR, OC_STACK_DELETE_TRANSACTION);
1448 OCStackResult res = OC_STACK_ERROR;
1450 RemoveData_t* removeData = (RemoveData_t*)delDevCtx;
1454 OicUuid_t revDevUuid = {.id={0}};
1455 if(UUID_LENGTH == clientResponse->identity.id_length)
1457 memcpy(revDevUuid.id, clientResponse->identity.id, sizeof(revDevUuid.id));
1458 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1460 res = PDMUnlinkDevices(&removeData->revokeTargetDev->doxm->deviceID, &revDevUuid);
1461 if (OC_STACK_OK != res)
1463 OIC_LOG(ERROR, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1464 registerResultForRemoveDevice(removeData, &revDevUuid,
1465 OC_STACK_INCONSISTENT_DB, true);
1467 return OC_STACK_DELETE_TRANSACTION;
1470 registerResultForRemoveDevice(removeData, &revDevUuid,
1471 OC_STACK_RESOURCE_DELETED, false);
1475 registerResultForRemoveDevice(removeData, &revDevUuid,
1476 clientResponse->result, true);
1477 OIC_LOG(ERROR, TAG, "Unexpected result from DELETE credential request!");
1482 OIC_LOG_V(WARNING, TAG, "Incorrect length of device UUID was sent from %s:%d",
1483 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1485 if (OC_STACK_RESOURCE_DELETED == clientResponse->result)
1488 * Since server's credential was deleted,
1489 * register result as OC_STACK_INCONSISTENT_DB with NULL UUID.
1491 OIC_LOG_V(ERROR, TAG, "But server's credential was deleted.");
1492 registerResultForRemoveDevice(removeData, NULL, OC_STACK_INCONSISTENT_DB, true);
1496 registerResultForRemoveDevice(removeData, NULL, clientResponse->result, true);
1502 registerResultForRemoveDevice(removeData, NULL, OC_STACK_ERROR, true);
1503 OIC_LOG(ERROR, TAG, "SRPRemoveDevices received Null clientResponse");
1507 return OC_STACK_DELETE_TRANSACTION;
1510 static OCStackResult GetListofDevToReqDeleteCred(const OCProvisionDev_t* pRevokeTargetDev,
1511 OCProvisionDev_t* pOwnedDevList,
1512 OCUuidList_t* pLinkedUuidList,
1513 OCProvisionDev_t** ppLinkedDevList,
1514 size_t *numOfLinkedDev)
1516 // pOwnedDevList could be NULL. It means no alived and owned device now.
1517 if (pRevokeTargetDev == NULL || pLinkedUuidList == NULL ||\
1518 ppLinkedDevList == NULL || numOfLinkedDev == NULL)
1520 return OC_STACK_INVALID_PARAM;
1524 OCUuidList_t *curUuid = NULL, *tmpUuid = NULL;
1525 LL_FOREACH_SAFE(pLinkedUuidList, curUuid, tmpUuid)
1527 // Mark the link status stale.
1528 OCStackResult res = PDMSetLinkStale(&curUuid->dev, &pRevokeTargetDev->doxm->deviceID);
1529 if (OC_STACK_OK != res)
1531 OIC_LOG(FATAL, TAG, "PDMSetLinkStale() FAIL: PDB is an obsolete one.");
1532 return OC_STACK_INCONSISTENT_DB;
1537 // If this linked device is alive (power-on), add the deivce to the list.
1538 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1539 LL_FOREACH_SAFE(pOwnedDevList, curDev, tmpDev)
1541 if (memcmp(curDev->doxm->deviceID.id, curUuid->dev.id, sizeof(curUuid->dev.id)) == 0)
1543 OCProvisionDev_t* targetDev = PMCloneOCProvisionDev(curDev);
1544 if (NULL == targetDev)
1546 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Cloning OCProvisionDev_t Failed.");
1547 return OC_STACK_NO_MEMORY;
1550 LL_PREPEND(*ppLinkedDevList, targetDev);
1557 *numOfLinkedDev = cnt;
1562 * Function to device revocation
1563 * This function will remove credential of target device from all devices in subnet.
1565 * @param[in] ctx Application context would be returned in result callback
1566 * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds)
1567 * @param[in] pTargetDev Device information to be revoked.
1568 * @param[in] resultCallback callback provided by API user, callback will be called when
1569 * credential revocation is finished.
1570 * @return OC_STACK_OK in case of success and other value otherwise.
1571 * If OC_STACK_OK is returned, the caller of this API should wait for callback.
1572 * OC_STACK_CONTINUE means operation is success but no request is need to be initiated.
1574 OCStackResult SRPRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery,
1575 const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback)
1577 OIC_LOG(INFO, TAG, "IN SRPRemoveDevice");
1579 if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery)
1581 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL parameters");
1582 return OC_STACK_INVALID_PARAM;
1584 if (!resultCallback)
1586 OIC_LOG(INFO, TAG, "SRPRemoveDevice : NULL Callback");
1587 return OC_STACK_INVALID_CALLBACK;
1590 // Declare variables in here to handle error cases with goto statement.
1591 OCProvisionDev_t* pOwnedDevList = NULL;
1592 OCProvisionDev_t* pLinkedDevList = NULL;
1593 RemoveData_t* removeData = NULL;
1595 //1. Find all devices that has a credential of the revoked device
1596 OCUuidList_t* pLinkedUuidList = NULL;
1597 size_t numOfDevices = 0;
1598 OCStackResult res = OC_STACK_ERROR;
1599 res = PDMGetLinkedDevices(&pTargetDev->doxm->deviceID, &pLinkedUuidList, &numOfDevices);
1600 if (OC_STACK_OK != res)
1602 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to get linked devices information");
1605 // if there is no related device, we can skip further process.
1606 if (0 == numOfDevices)
1608 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No linked device found.");
1609 res = OC_STACK_CONTINUE;
1613 //2. Find owned device from the network
1614 res = PMDeviceDiscovery(waitTimeForOwnedDeviceDiscovery, true, &pOwnedDevList);
1615 if (OC_STACK_OK != res)
1617 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : Failed to PMDeviceDiscovery");
1621 //3. Make a list of devices to send DELETE credential request
1622 // by comparing owned devices from provisioning database with mutlicast discovery result.
1623 size_t numOfLinkedDev = 0;
1624 res = GetListofDevToReqDeleteCred(pTargetDev, pOwnedDevList, pLinkedUuidList,
1625 &pLinkedDevList, &numOfLinkedDev);
1626 if (OC_STACK_OK != res)
1628 OIC_LOG(ERROR, TAG, "SRPRemoveDevice : GetListofDevToReqDeleteCred() failed");
1631 if (0 == numOfLinkedDev) // This case means, there is linked device but it's not alive now.
1632 { // So we don't have to send request message.
1633 OIC_LOG(DEBUG, TAG, "SRPRemoveDevice : No alived & linked device found.");
1634 res = OC_STACK_CONTINUE;
1638 // 4. Prepare RemoveData Context data.
1639 removeData = (RemoveData_t*)OICCalloc(1, sizeof(RemoveData_t));
1642 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1643 res = OC_STACK_NO_MEMORY;
1647 removeData->revokeTargetDev = PMCloneOCProvisionDev(pTargetDev);
1648 if (!removeData->revokeTargetDev)
1650 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : PMCloneOCProvisionDev Failed");
1651 res = OC_STACK_NO_MEMORY;
1655 removeData->removeRes =
1656 (OCProvisionResult_t*)OICCalloc(numOfLinkedDev, sizeof(OCProvisionResult_t));
1657 if (!removeData->removeRes)
1659 OIC_LOG(ERROR, TAG, "SRPRemoveDevices : Failed to memory allocation");
1660 res = OC_STACK_NO_MEMORY;
1664 removeData->ctx = ctx;
1665 removeData->linkedDevList = pLinkedDevList;
1666 removeData->resultCallback = resultCallback;
1667 removeData->numOfResults = 0;
1668 removeData->sizeOfResArray = numOfLinkedDev;
1669 removeData->hasError = false;
1671 // 5. Send DELETE credential request to linked devices.
1672 OCProvisionDev_t *curDev = NULL, *tmpDev = NULL;
1673 OCStackResult totalRes = OC_STACK_ERROR; /* variable for checking request is sent or not */
1674 LL_FOREACH_SAFE(pLinkedDevList, curDev, tmpDev)
1676 res = SendDeleteCredentialRequest((void*)removeData, &SRPRemoveDeviceCB,
1677 removeData->revokeTargetDev, curDev);
1678 if (OC_STACK_OK != res)
1680 OIC_LOG_V(ERROR, TAG, "SRPRemoveDevice : Fail to send the DELETE credential request to\
1681 %s:%u", curDev->endpoint.addr, curDev->endpoint.port);
1685 totalRes = OC_STACK_OK; // This means at least one request is successfully sent.
1689 PDMDestoryOicUuidLinkList(pLinkedUuidList); //TODO: Modify API name to have unified convention.
1690 PMDeleteDeviceList(pOwnedDevList);
1691 OIC_LOG(INFO, TAG, "OUT SRPRemoveDevice");
1693 return totalRes; // Caller of this API should wait callback if totalRes == OC_STACK_OK.
1696 PDMDestoryOicUuidLinkList(pLinkedUuidList);
1697 PMDeleteDeviceList(pOwnedDevList);
1698 PMDeleteDeviceList(pLinkedDevList);
1701 OICFree(removeData->revokeTargetDev);
1702 OICFree(removeData->removeRes);
1703 OICFree(removeData);
1705 OIC_LOG(INFO, TAG, "OUT ERROR case SRPRemoveDevice");