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 * *****************************************************************/
21 // Defining _POSIX_C_SOURCE macro with 199309L (or greater) as value
22 // causes header files to expose definitions
23 // corresponding to the POSIX.1b, Real-time extensions
24 // (IEEE Std 1003.1b-1993) specification
26 // For this specific file, see use of clock_gettime,
27 // Refer to http://pubs.opengroup.org/stage7tc1/functions/clock_gettime.html
28 // and to http://man7.org/linux/man-pages/man2/clock_gettime.2.html
30 #ifndef _POSIX_C_SOURCE
31 #define _POSIX_C_SOURCE 200809L
41 #include "ocpayload.h"
42 #include "ocpayloadcbor.h"
43 #include "oic_malloc.h"
46 #include "cainterface.h"
47 #include "provisioningmanager.h"
48 #include "credentialgenerator.h"
51 #include "aclresource.h"
52 #include "doxmresource.h"
53 #include "pstatresource.h"
54 #include "srmresourcestrings.h"
55 #include "credresource.h"
56 #include "oic_string.h"
57 #include "secureresourcemanager.h"
63 SP_DISCOVERY_STARTED = (0x1 << 1),
64 SP_DISCOVERY_ERROR = (0x1 << 2),
65 SP_DISCOVERY_DONE = (0x1 << 3),
66 SP_UP_OWN_TR_METH_STARTED = (0x1 << 4),
67 SP_UP_OWN_TR_METH_ERROR = (0x1 << 5),
68 SP_UP_OWN_TR_METH_DONE = (0x1 << 6),
69 SP_LIST_METHODS_STARTED = (0x1 << 7),
70 SP_LIST_METHODS_ERROR = (0x1 << 8),
71 SP_LIST_METHODS_DONE = (0x1 << 9),
72 SP_UPDATE_OP_MODE_STARTED = (0x1 << 10),
73 SP_UPDATE_OP_MODE_ERROR = (0x1 << 11),
74 SP_UPDATE_OP_MODE_DONE = (0x1 << 12),
75 SP_UPDATE_OWNER_STARTED = (0x1 << 13),
76 SP_UPDATE_OWNER_ERROR = (0x1 << 14),
77 SP_UPDATE_OWNER_DONE = (0x1 << 15),
78 SP_PROV_ACL_STARTED = (0x1 << 16),
79 SP_PROV_ACL_ERROR = (0x1 << 17),
80 SP_PROV_ACL_DONE = (0x1 << 18),
81 SP_UP_HASH_STARTED = (0x1 << 19),
82 SP_UP_HASH_ERROR = (0x1 << 20),
83 SP_UP_HASH_DONE = (0x1 << 21),
84 SP_PROV_CRED_STARTED = (0x1 << 22),
85 SP_PROV_CRED_ERROR = (0x1 << 23),
86 SP_PROV_CRED_DONE = (0x1 << 24)
88 } SPProvisioningStates;
90 #define SP_MAX_BUF_LEN 1024
91 #define TAG "SPProvisionAPI"
92 #define COAP_QUERY "coap://%s:%d%s"
93 #define COAPS_QUERY "coaps://%s:%d%s"
94 #define CA_SECURE_PORT 5684
96 bool (*handler)(const CAEndpoint_t *, const CAResponseInfo_t *);
99 * CA token to keep track of response.
101 static CAToken_t gToken = NULL;
104 * start pointer for discovered device linked list.
106 static SPTargetDeviceInfo_t *gStartOfDiscoveredDevices = NULL;
109 * current pointer of device linked list.
111 static SPTargetDeviceInfo_t *gCurrent = NULL;
114 * Variable to keep track of various request.
116 static uint32_t gStateManager = 0;
119 * Variable for storing provisioning tool's provisioning capabilities
120 * Must be in decreasing order of preference. More prefered method should
121 * have lower array index.
123 static OicSecDpom_t gProvisioningToolCapability[] = { SINGLE_SERVICE_CLIENT_DRIVEN };
126 * Number of supported provisioning methods
127 * current version supports only one.
129 static int gNumOfProvisioningMethodsPT = 1;
132 * Global variable to save pstat.
134 static OicSecPstat_t *gPstat = NULL;
137 * Secure String copy function
138 * @param[in] destination Pointer to destination string.
139 * @param[in] source Pointer to source string.
140 * @return pointer to destination string, NULL in case of error.
142 static inline char *SPStringCopy(char *destination, const char *source, size_t num)
144 if (strncpy(destination, source, num))
146 destination[num - 1] = '\0';
153 * Function to convert CA result code to SP result code.
155 * @return result code of SP corresponding to that of CA.
157 static SPResult convertCAResultToSPResult(CAResult_t caResult)
163 return SP_RESULT_SUCCESS;
165 case CA_STATUS_INVALID_PARAM:
167 return SP_RESULT_CONN_INVALID_PARAM;
169 case CA_ADAPTER_NOT_ENABLED:
171 return SP_RESULT_CONN_SERVER_STARTED_ALREADY;
173 case CA_SERVER_STARTED_ALREADY:
175 return SP_RESULT_CONN_SERVER_STARTED_ALREADY;
177 case CA_SERVER_NOT_STARTED:
179 return SP_RESULT_CONN_SERVER_NOT_STARTED;
181 case CA_DESTINATION_NOT_REACHABLE:
183 return SP_RESULT_CONN_DESTINATION_NOT_REACHABLE;
185 case CA_SOCKET_OPERATION_FAILED:
187 return SP_RESULT_CONN_SOCKET_OPERATION_FAILED;
191 return SP_RESULT_CONN_SEND_FAILED;
193 case CA_RECEIVE_FAILED:
195 return SP_RESULT_CONN_RECEIVE_FAILED;
197 case CA_MEMORY_ALLOC_FAILED:
199 return SP_RESULT_CONN_MEMORY_ALLOC_FAILED;
201 case CA_REQUEST_TIMEOUT:
203 return SP_RESULT_CONN_REQUEST_TIMEOUT;
205 case CA_DESTINATION_DISCONNECTED:
207 return SP_RESULT_CONN_DESTINATION_DISCONNECTED;
209 case CA_STATUS_FAILED:
211 return SP_RESULT_CONN_STATUS_FAILED;
213 case CA_NOT_SUPPORTED:
215 return SP_RESULT_CONN_NOT_SUPPORTED;
219 return SP_RESULT_INTERNAL_ERROR;
225 * Function to delete memory allocated to linked list.
228 static void deleteList()
230 SPTargetDeviceInfo_t *current = gStartOfDiscoveredDevices;
234 SPTargetDeviceInfo_t *next = current->next;
235 DeleteDoxmBinData(current->doxm);
236 DeletePstatBinData(current->pstat);
240 gStartOfDiscoveredDevices = NULL;
244 * Timeout implementation.
245 * @param[in] timeout Timeout in seconds. with 0 it will wait forever for success.
246 * @param[in] mask Mask of operation and 0 for no mask.
247 * @return SP_RESULT_SUCCESS on success otherwise error.
249 static SPResult SPTimeout(unsigned short timeout, uint32_t mask)
251 struct timespec startTime = {};
252 struct timespec currTime = {};
254 CAResult_t res = SP_RESULT_SUCCESS;
255 #ifdef _POSIX_MONOTONIC_CLOCK
256 int clock_res = clock_gettime(CLOCK_MONOTONIC, &startTime);
258 int clock_res = clock_gettime(CLOCK_REALTIME, &startTime);
262 return SP_RESULT_INTERNAL_ERROR;
264 while (CA_STATUS_OK == res)
266 res = CAHandleRequestResponse();
267 #ifdef _POSIX_MONOTONIC_CLOCK
268 clock_res = clock_gettime(CLOCK_MONOTONIC, &currTime);
270 clock_res = clock_gettime(CLOCK_REALTIME, &currTime);
274 return SP_RESULT_INTERNAL_ERROR;
276 long elapsed = (currTime.tv_sec - startTime.tv_sec);
277 if (SP_NO_MASK == mask)
279 if (elapsed > timeout)
281 return SP_RESULT_SUCCESS;
286 if (gStateManager & mask)
288 return SP_RESULT_SUCCESS;
290 if ((elapsed > timeout) && timeout)
292 return SP_RESULT_INTERNAL_ERROR;
296 return convertCAResultToSPResult(res);
300 * Function to send request to resource server.
301 * @param[in] method method to be used for sending rquest.
302 * @param[in] endpoint endpoint address
303 * @param[in] secure use secure connection
304 * @param[in] resourceUri resourceUri token.
305 * @param[in] payload Payload to be sent with data. NULL is case message
306 * doesn't have payload.
307 * @param[in] payloadLen Size of data to be sent.
308 * @return CA_STATUS_OK on success, otherwise error code.
310 static CAResult_t sendCARequest(CAMethod_t method,
311 const OCDevAddr *devAddr,
312 OCTransportFlags secure,
313 const char *resourceUri,
314 char *payload, int payloadLen)
316 if (payload && '\0' != (*(payload + payloadLen)))
318 OC_LOG(ERROR, TAG, "Payload not properly terminated.");
319 return CA_STATUS_INVALID_PARAM;
322 if (CA_STATUS_OK != CAGenerateToken(&gToken, CA_MAX_TOKEN_LEN))
324 OC_LOG(ERROR, TAG, "Error while generating token");
325 return CA_MEMORY_ALLOC_FAILED;
328 CAEndpoint_t *endpoint = NULL;
329 if (CA_STATUS_OK != CACreateEndpoint((CATransportFlags_t)secure,
330 devAddr->adapter, devAddr->addr,
331 devAddr->port, &endpoint))
333 OC_LOG(ERROR, TAG, "Failed to create remote endpoint");
334 CADestroyEndpoint(endpoint);
335 return CA_STATUS_FAILED;
338 OCSecurityPayload secPayload = {};
339 secPayload.securityData = payload;
340 secPayload.base.type = PAYLOAD_TYPE_SECURITY;
342 CARequestInfo_t requestInfo = {};
343 requestInfo.method = method;
344 requestInfo.isMulticast = false;
345 OCConvertPayload((OCPayload*)(&secPayload), &requestInfo.info.payload,
346 &requestInfo.info.payloadSize);
348 requestInfo.info.type = CA_MSG_CONFIRM;
349 requestInfo.info.token = gToken;
350 requestInfo.info.tokenLength = CA_MAX_TOKEN_LEN;
351 requestInfo.info.resourceUri = (CAURI_t)resourceUri;
353 CAResult_t caResult = CA_STATUS_OK;
354 caResult = CASendRequest(endpoint, &requestInfo);
355 if (CA_STATUS_OK != caResult)
357 OC_LOG(ERROR, TAG, "Send Request Error !!");
359 CADestroyEndpoint(endpoint);
366 * @param[in] endpoint Endpoint information
367 * @param[in] doxm pointer to doxm instance.
368 * @return SP_RESULT_SUCCESS for success and errorcode otherwise.
370 static SPResult addDevice(const CAEndpoint_t *endpoint, OicSecDoxm_t* doxm)
372 if (NULL == endpoint)
374 return SP_RESULT_INVALID_PARAM;
376 SPTargetDeviceInfo_t *ptr = (SPTargetDeviceInfo_t *)OICCalloc(1, sizeof (SPTargetDeviceInfo_t));
379 OC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
380 return SP_RESULT_MEM_ALLOCATION_FAIL;
383 memcpy(&(ptr->endpoint), endpoint, sizeof(CAEndpoint_t));
388 if (NULL == gStartOfDiscoveredDevices)
390 gStartOfDiscoveredDevices = ptr;
395 gCurrent->next = ptr;
398 return SP_RESULT_SUCCESS;
402 * Function to provide timeframe in which response can be received.
404 * @param[in] timeout Timeout in seconds.
405 * @return SP_RESULT_SUCCESS on success , otherwise error code.
407 static SPResult SPWaitForResponse(unsigned short timeout)
409 return SPTimeout(timeout, SP_NO_MASK);
413 * Function to select appropriate provisioning method.
415 * @param[in] supportedMethodsList List of supported methods
416 * @param[out] selectedMethod Selected methods
417 * @return SP_SUCCESS on success
419 static SPResult selectProvisioningMethod(OicSecOxm_t *supportedMethods, size_t numberOfMethods,
420 OicSecOxm_t *selectedMethod)
423 TODO Logic to find appropiate method and assign it to out param
424 for beachhead release method at index 0 will be returned.
426 *selectedMethod = supportedMethods[0];
427 return SP_RESULT_SUCCESS;
431 * Response handler for discovery.
433 * @param[in] object Remote endpoint object
434 * @param[in] requestInfo Datastructure containing request information.
435 * @return true is CA token matches request token, false otherwise.
437 static bool ProvisionDiscoveryHandler(const CAEndpoint_t *object,
438 const CAResponseInfo_t *responseInfo)
440 if ((gStateManager & SP_DISCOVERY_STARTED) && gToken)
442 // Response handler for discovery.
443 if (0 == memcmp(gToken, responseInfo->info.token, responseInfo->info.tokenLength))
445 OC_LOG(INFO, TAG, "Inside ProvisionDiscoveryHandler.");
446 if (NULL == responseInfo->info.payload)
448 OC_LOG(INFO, TAG, "Skiping Null payload");
453 OCStackResult result = OCParsePayload(&payload, responseInfo->info.payload,
454 responseInfo->info.payloadSize);
456 OicSecDoxm_t *ptrDoxm = NULL;
458 if(result == OC_STACK_OK && payload->type == PAYLOAD_TYPE_SECURITY)
460 ptrDoxm = JSONToDoxmBin(((OCSecurityPayload*)payload)->securityData);
465 OC_LOG(INFO, TAG, "Ignoring malformed JSON");
469 OC_LOG(DEBUG, TAG, "Successfully converted doxm json to bin.");
471 SPResult res = addDevice(object, ptrDoxm);
472 if (SP_RESULT_SUCCESS != res)
474 OC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
475 gStateManager = gStateManager | SP_DISCOVERY_ERROR;
476 DeleteDoxmBinData(ptrDoxm);
479 OC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
480 gStateManager |= SP_DISCOVERY_DONE;
490 * Response handler ownership transfer.
492 * @param[in] object Remote endpoint object
493 * @param[in] requestInfo Datastructure containing request information.
494 * @return true is CA token matches request token, false otherwise.
496 static bool OwnerShipTransferModeHandler(const CAEndpoint_t *object,
497 const CAResponseInfo_t *responseInfo)
499 if ((gStateManager & SP_UP_OWN_TR_METH_STARTED) && gToken)
501 // response handler for ownership tranfer
502 OC_LOG(INFO, TAG, "Inside OwnerShipTransferModeHandler.");
503 if (memcmp(gToken, responseInfo->info.token, responseInfo->info.tokenLength) == 0)
505 OC_LOG_V(DEBUG, TAG, "Response result for OwnerShipTransferMode: %d", responseInfo->result);
506 if (CA_SUCCESS == responseInfo->result)
508 gStateManager |= SP_UP_OWN_TR_METH_DONE;
509 OC_LOG(INFO, TAG, "Exiting OwnerShipTransferModeHandler.");
513 gStateManager |= SP_UP_OWN_TR_METH_ERROR;
514 OC_LOG(ERROR, TAG, "Error in OwnerShipTransferModeHandler.");
523 * Response handler list methods.
525 * @param[in] object Remote endpoint object
526 * @param[in] requestInfo Datastructure containing request information.
527 * @return true is CA token matches request token, false otherwise.
529 static bool ListMethodsHandler(const CAEndpoint_t *object,
530 const CAResponseInfo_t *responseInfo)
532 if ((gStateManager & SP_LIST_METHODS_STARTED) && gToken)
534 OC_LOG(INFO, TAG, "Inside ListMethodsHandler.");
535 if (memcmp(gToken, responseInfo->info.token, responseInfo->info.tokenLength) == 0)
537 OC_LOG_V(DEBUG, TAG, "Response result for ListMethodsHandler: %d", responseInfo->result);
538 if (CA_SUCCESS == responseInfo->result)
540 OC_LOG_V (DEBUG, TAG, "Response Payload: %s", responseInfo->info.payload);
541 // Temp logic to trim oc attribute from json
542 // JSONToPstatBin should handle OC in JSON.
543 if (NULL == responseInfo->info.payload)
545 OC_LOG(ERROR, TAG, "response payload is null.");
546 gStateManager |= SP_LIST_METHODS_ERROR;
551 OCStackResult result = OCParsePayload(&payload, responseInfo->info.payload,
552 responseInfo->info.payloadSize);
554 OicSecPstat_t *pstat = NULL;
556 if(result == OC_STACK_OK && payload->type == PAYLOAD_TYPE_SECURITY)
558 pstat = JSONToPstatBin(((OCSecurityPayload*)payload)->securityData);
563 OC_LOG(ERROR, TAG, "Error while converting json to pstat bin");
564 gStateManager |= SP_LIST_METHODS_ERROR;
567 DeletePstatBinData(gPstat);
570 gStateManager |= SP_LIST_METHODS_DONE;
572 OC_LOG(INFO, TAG, "Exiting ListMethodsHandler.");
581 * Response handler for update operation mode.
583 * @param[in] object Remote endpoint object
584 * @param[in] requestInfo Datastructure containing request information.
585 * @return true is CA token matches request token, false otherwise.
587 static bool OperationModeUpdateHandler(const CAEndpoint_t *object,
588 const CAResponseInfo_t *responseInfo)
590 if ((gStateManager & SP_UPDATE_OP_MODE_STARTED) && gToken)
592 if (0 == memcmp(gToken, responseInfo->info.token, responseInfo->info.tokenLength))
594 OC_LOG(INFO, TAG, "Inside OperationModeUpdateHandler.");
595 OC_LOG_V(DEBUG, TAG, "Response result for OperationModeUpdateHandler: %d", responseInfo->result);
596 if (CA_SUCCESS == responseInfo->result)
598 gStateManager |= SP_UPDATE_OP_MODE_DONE;
599 OC_LOG(INFO, TAG, "Exiting OperationModeUpdateHandler.");
603 gStateManager |= SP_UPDATE_OP_MODE_ERROR;
604 OC_LOG(ERROR, TAG, "Error in OperationModeUpdateHandler.");
613 * Response handler for ownership transfer.
615 * @param[in] object Remote endpoint object
616 * @param[in] requestInfo Datastructure containing request information.
617 * @return true is CA token matches request token, false otherwise.
619 static bool OwnerShipUpdateHandler(const CAEndpoint_t *object,
620 const CAResponseInfo_t *responseInfo)
622 if ((gStateManager & SP_UPDATE_OWNER_STARTED) && gToken)
624 // response handler for ownership tranfer
625 if (0 == memcmp(gToken, responseInfo->info.token, responseInfo->info.tokenLength))
627 OC_LOG(INFO, TAG, "Inside OwnerShipUpdateHandler.");
628 OC_LOG_V(DEBUG, TAG, "Response result for OwnerShipUpdateHandler: %d", responseInfo->result);
629 if (CA_SUCCESS == responseInfo->result)
631 gStateManager |= SP_UPDATE_OWNER_DONE;
632 OC_LOG(INFO, TAG, "Exiting OwnerShipUpdateHandler.");
636 gStateManager |= SP_UPDATE_OWNER_ERROR;
637 OC_LOG(ERROR, TAG, "Error in OwnerShipUpdateHandler.");
646 * Response handler for ACL provisioning.
648 * @param[in] object Remote endpoint object
649 * @param[in] requestInfo Datastructure containing request information.
650 * @return true is CA token matches request token, false otherwise.
652 static bool ACLProvisioningHandler(const CAEndpoint_t *object,
653 const CAResponseInfo_t *responseInfo)
655 if ((gStateManager & SP_PROV_ACL_STARTED) && gToken)
658 // response handler for ACL provisioning.
659 if (0 == memcmp(gToken, responseInfo->info.token, responseInfo->info.tokenLength))
661 OC_LOG(INFO, TAG, "Inside ACLProvisioningHandler.");
662 OC_LOG_V(DEBUG, TAG, "Response result for ACLProvisioningHandler: %d", responseInfo->result);
663 if (CA_CREATED == responseInfo->result)
665 OC_LOG(INFO, TAG, "Exiting ACLProvisioningHandler.");
666 gStateManager |= SP_PROV_ACL_DONE;
670 OC_LOG(ERROR, TAG, "Error in ACLProvisioningHandler.");
671 gStateManager |= SP_PROV_ACL_ERROR;
680 * Response handler for provisioning finalization.
682 * @param[in] object Remote endpoint object
683 * @param[in] requestInfo Datastructure containing request information.
684 * @return true is CA token matches request token, false otherwise.
686 static bool FinalizeProvisioningHandler(const CAEndpoint_t *object,
687 const CAResponseInfo_t *responseInfo)
689 if ((gStateManager & SP_UP_HASH_STARTED) && gToken)
691 // response handler for finalize provisioning.
692 if (0 == memcmp(gToken, responseInfo->info.token, responseInfo->info.tokenLength))
694 OC_LOG(INFO, TAG, "Inside FinalizeProvisioningHandler.");
695 OC_LOG_V(DEBUG, TAG, "Response result for FinalizeProvisioningHandler: %d", responseInfo->result);
696 if (CA_SUCCESS == responseInfo->result)
698 gStateManager |= SP_UP_HASH_DONE;
699 OC_LOG(INFO, TAG, "Exiting FinalizeProvisioningHandler.");
703 gStateManager |= SP_UP_HASH_ERROR;
704 OC_LOG(ERROR, TAG, "Error in FinalizeProvisioningHandler.");
713 * Response handler for Credential provisioning.
715 * @param[in] object Remote endpoint object
716 * @param[in] requestInfo Datastructure containing request information.
717 * @return true is CA token matches request token, false otherwise.
719 static bool CredProvisioningHandler(const CAEndpoint_t *object,
720 const CAResponseInfo_t *responseInfo)
722 if ((gStateManager & SP_PROV_CRED_STARTED) && gToken)
724 // response handler for CRED provisioning.
725 OC_LOG(INFO, TAG, "Inside CredProvisioningHandler.");
726 OC_LOG_V(DEBUG, TAG, "Response result for CredProvisioningHandler: %d", responseInfo->result);
727 if (0 == memcmp(gToken, responseInfo->info.token, responseInfo->info.tokenLength))
729 if (CA_CREATED == responseInfo->result)
731 gStateManager |= SP_PROV_CRED_DONE;
732 OC_LOG(INFO, TAG, "Exiting CredProvisioningHandler.");
736 gStateManager |= SP_PROV_CRED_ERROR;
737 OC_LOG(ERROR, TAG, "Error in CredProvisioningHandler.");
748 * @param[in] object Remote endpoint object
749 * @param[in] responseInfo Datastructure containing response information.
750 * @return true if received response is for provisioning API false otherwise.
752 static bool SPResponseHandler(const CAEndpoint_t *object,
753 const CAResponseInfo_t *responseInfo)
755 bool isProvResponse = false;
756 if ((NULL != responseInfo) && (NULL != responseInfo->info.token))
758 isProvResponse = handler(object, responseInfo);
760 return isProvResponse;
764 * Function to find the resources using multicast discovery.
766 * @param[in] timeout timeout in secs
767 * @return SP_RESULT_SUCCESS normally otherwise error code.
769 static SPResult findResource(unsigned short timeout)
771 static char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
772 CAResult_t res = CAGenerateToken(&gToken, CA_MAX_TOKEN_LEN);
773 if (CA_STATUS_OK != res)
775 OC_LOG(ERROR, TAG, "Error while generating token.");
776 return SP_RESULT_INTERNAL_ERROR;
779 CAEndpoint_t endpoint = { CA_DEFAULT_FLAGS };
781 CAMessageType_t msgType = CA_MSG_NONCONFIRM;
782 CAInfo_t requestData = { 0 };
783 requestData.token = gToken;
784 requestData.tokenLength = CA_MAX_TOKEN_LEN;
785 requestData.payload = NULL;
786 requestData.payloadSize = 0;
787 requestData.type = msgType;
788 requestData.resourceUri = DOXM_OWNED_FALSE_MULTICAST_QUERY;
789 CARequestInfo_t requestInfo = { 0 };
790 requestInfo.method = CA_GET;
791 requestInfo.info = requestData;
792 requestInfo.isMulticast = true;
793 res = CASendRequest(&endpoint, &requestInfo);
795 handler = &ProvisionDiscoveryHandler;
796 gStateManager |= SP_DISCOVERY_STARTED;
797 if (CA_STATUS_OK != res)
799 OC_LOG(ERROR, TAG, "Error while finding resource.");
800 return convertCAResultToSPResult(res);
804 OC_LOG(INFO, TAG, "Discovery Request sent successfully");
806 return SPWaitForResponse(timeout);
810 * Function to update the operation mode. As per the spec. Operation mode in client driven
811 * single service provisioning it will be updated to 0x3
813 * @param[in] timeout timeout for operation.
814 * @param[in] deviceInfo Device Info.
815 * @return SP_SUCCESS on success
817 static SPResult updateOwnerTransferModeToResource(unsigned short timeout,
818 SPTargetDeviceInfo_t *deviceInfo, OicSecOxm_t selectedMethod)
820 SPResult res = SP_RESULT_INTERNAL_ERROR;
822 deviceInfo->doxm->oxmSel = selectedMethod;
823 char *payload = BinToDoxmJSON(deviceInfo->doxm);
826 OC_LOG(ERROR, TAG, "Error while converting bin to json");
827 return SP_RESULT_INTERNAL_ERROR;
829 OC_LOG_V(DEBUG, TAG, "Payload: %s", payload);
830 int payloadLen = strlen(payload);
832 handler = &OwnerShipTransferModeHandler;
833 gStateManager |= SP_UP_OWN_TR_METH_STARTED;
835 CAResult_t result = sendCARequest(CA_PUT,
836 &deviceInfo->endpoint,
839 payload, payloadLen);
841 if (CA_STATUS_OK != result)
843 OC_LOG(ERROR, TAG, "Error while sending request.");
844 CADestroyToken(gToken);
845 return convertCAResultToSPResult(result);
847 res = SPTimeout(timeout, SP_UP_OWN_TR_METH_DONE);
848 if (SP_RESULT_SUCCESS != res)
850 OC_LOG(ERROR, TAG, "Internal Error occured");
851 CADestroyToken(gToken);
852 return SP_RESULT_TIMEOUT;
854 CADestroyToken(gToken);
855 return SP_RESULT_SUCCESS;
859 * Function to send request to resource to get its pstat resource information.
861 * @param[in] timeout timeout for operation.
862 * @param[in] deviceInfo Device Info.
863 * @return SP_SUCCESS on success
865 static SPResult getProvisioningStatusResource(unsigned short timeout,
866 SPTargetDeviceInfo_t *deviceInfo)
868 handler = &ListMethodsHandler;
869 gStateManager |= SP_LIST_METHODS_STARTED;
871 CAResult_t result = sendCARequest(CA_GET,
872 &deviceInfo->endpoint,
876 if (CA_STATUS_OK != result)
878 OC_LOG(ERROR, TAG, "Failure while sending request.");
879 CADestroyToken(gToken);
880 return convertCAResultToSPResult(result);
882 SPResult res = SPTimeout(timeout, SP_LIST_METHODS_DONE);
883 if (SP_RESULT_SUCCESS != res)
885 OC_LOG(ERROR, TAG, "Timeout while getting method list.");
886 CADestroyToken(gToken);
887 return SP_RESULT_TIMEOUT;
889 if (gStateManager && SP_LIST_METHODS_DONE)
891 deviceInfo->pstat = gPstat;
892 CADestroyToken(gToken);
893 OC_LOG(DEBUG, TAG, "getProvisioningStatusResource completed.");
894 return SP_RESULT_SUCCESS;
896 CADestroyToken(gToken);
897 return SP_RESULT_INTERNAL_ERROR;
901 * Function to update the operation mode. As per the spec. Operation mode in client driven
902 * single service provisioning it will be updated to 0x3
904 * @param[in] timeout timeout for operation.
905 * @param[in] deviceInfo Device Info.
906 * @return SP_SUCCESS on success
908 static SPResult updateOperationMode(unsigned short timeout,
909 SPTargetDeviceInfo_t *deviceInfo,
910 OicSecDpom_t selectedOperationMode)
913 SPResult res = SP_RESULT_INTERNAL_ERROR;
915 deviceInfo->pstat->om = selectedOperationMode;
917 char *payloadBuffer = BinToPstatJSON(deviceInfo->pstat);
918 if (NULL == payloadBuffer)
920 OC_LOG(ERROR, TAG, "Error while converting pstat bin to json");
921 return SP_RESULT_INTERNAL_ERROR;
924 size_t payloadLen = strlen(payloadBuffer);
925 handler = &OperationModeUpdateHandler;
926 gStateManager |= SP_UPDATE_OP_MODE_STARTED;
928 CAResult_t result = sendCARequest(CA_PUT,
929 &deviceInfo->endpoint,
932 payloadBuffer, payloadLen);
933 if (CA_STATUS_OK != result)
935 OC_LOG(ERROR, TAG, "Error while sending request.");
936 CADestroyToken(gToken);
937 OICFree(payloadBuffer);
938 return convertCAResultToSPResult(result);
940 res = SPTimeout(timeout, SP_UPDATE_OP_MODE_DONE);
941 if (SP_RESULT_SUCCESS != res)
943 OC_LOG(ERROR, TAG, "Internal Error occured");
944 CADestroyToken(gToken);
945 OICFree(payloadBuffer);
946 return SP_RESULT_TIMEOUT;
948 CADestroyToken(gToken);
949 OICFree(payloadBuffer);
951 if (gStateManager & SP_UPDATE_OP_MODE_DONE)
953 return SP_RESULT_SUCCESS;
955 return SP_RESULT_INTERNAL_ERROR;
959 * Function to initiate DTLS handshake.
961 * @param[in] deviceInfo Provisioning context
962 * @return SP_SUCCESS on success
964 static SPResult initiateDtlsHandshake(const CAEndpoint_t *endpoint)
966 CAResult_t caresult = CAEnableAnonECDHCipherSuite(true);
967 if (CA_STATUS_OK != caresult)
969 OC_LOG_V(ERROR, TAG, "Unable to enable anon cipher suite");
970 return SP_RESULT_INTERNAL_ERROR;
972 OC_LOG(INFO, TAG, "Anonymous cipher suite Enabled.");
974 caresult = CAInitiateHandshake(endpoint);
975 if (CA_STATUS_OK != caresult)
977 OC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
980 return SP_RESULT_SUCCESS;
984 * Function to send ownerShip info. This function would update Owned as true and
985 * owner as UUID for provisioning tool
987 * @param[in] timeout timeout value for the operation.
988 * @param[in] deviceInfo provisioning context.
989 * @return SP_SUCCESS on success
991 static SPResult sendOwnershipInfo(unsigned short timeout,
992 SPTargetDeviceInfo_t *selectedDeviceInfo)
994 OicUuid_t provTooldeviceID = {};
996 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
998 OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
999 return SP_RESULT_INTERNAL_ERROR;
1001 memcpy(selectedDeviceInfo->doxm->owner.id, provTooldeviceID.id , UUID_LENGTH);
1003 selectedDeviceInfo->doxm->owned = true;
1005 char *payloadBuffer = BinToDoxmJSON(selectedDeviceInfo->doxm);
1006 if (NULL == payloadBuffer)
1008 OC_LOG(ERROR, TAG, "Error while converting doxm bin to json");
1009 return SP_RESULT_INTERNAL_ERROR;
1011 int payloadLen = strlen(payloadBuffer);
1013 handler = &OwnerShipUpdateHandler;
1014 gStateManager |= SP_UPDATE_OWNER_STARTED;
1016 CAResult_t result = sendCARequest(CA_PUT,
1017 &selectedDeviceInfo->endpoint,
1020 payloadBuffer, payloadLen);
1021 if (CA_STATUS_OK != result)
1023 OC_LOG(ERROR, TAG, "Error while sending request.");
1024 CADestroyToken(gToken);
1025 OICFree(payloadBuffer);
1026 return convertCAResultToSPResult(result);
1028 SPResult res = SPTimeout(timeout, SP_UPDATE_OWNER_DONE);
1029 if (SP_RESULT_SUCCESS != res)
1031 OC_LOG(ERROR, TAG, "Internal Error occured");
1032 CADestroyToken(gToken);
1033 OICFree(payloadBuffer);
1034 return SP_RESULT_TIMEOUT;
1036 CADestroyToken(gToken);
1037 OICFree(payloadBuffer);
1038 return SP_RESULT_SUCCESS;
1042 * Function to save ownerPSK at provisioning tool end.
1044 * @return SP_SUCCESS on success
1046 static SPResult saveOwnerPSK(SPTargetDeviceInfo_t *selectedDeviceInfo)
1048 SPResult result = SP_RESULT_INTERNAL_ERROR;
1050 CAEndpoint_t endpoint = {};
1051 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
1052 endpoint.port = CA_SECURE_PORT;
1054 OicUuid_t provTooldeviceID = {};
1055 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1057 OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1061 uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {};
1063 //Generating OwnerPSK
1064 CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint,
1065 (uint8_t *)OXM_JUST_WORKS, strlen(OXM_JUST_WORKS), provTooldeviceID.id,
1066 sizeof(provTooldeviceID.id), selectedDeviceInfo->doxm->deviceID.id,
1067 sizeof(selectedDeviceInfo->doxm->deviceID.id), ownerPSK,
1068 OWNER_PSK_LENGTH_128);
1070 if (CA_STATUS_OK == pskRet)
1072 OC_LOG(INFO, TAG,"ownerPSK dump:\n");
1073 OC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128);
1074 //Generating new credential for provisioning tool
1076 uint32_t outLen = 0;
1078 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(ownerPSK)) + 1] = {};
1079 B64Result b64Ret = b64Encode(ownerPSK, sizeof(ownerPSK), base64Buff, sizeof(base64Buff),
1081 if (B64_OK == b64Ret)
1083 OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID,
1084 SYMMETRIC_PAIR_WISE_KEY, NULL,
1085 base64Buff, ownLen, &provTooldeviceID);
1088 //Update the SVR database.
1089 if (OC_STACK_OK == AddCredential(cred))
1091 result = SP_RESULT_SUCCESS;
1095 OC_LOG(ERROR, TAG, "AddCredential failed");
1100 OC_LOG(ERROR, TAG, "GenerateCredential failed");
1105 OC_LOG(ERROR, TAG, "b64Encode failed");
1110 OC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed");
1116 * Function to select operation mode.This function will return most secure common operation mode.
1118 * @param[out] selectedMode selected operation mode
1119 * @return SP_SUCCESS on success
1121 static void selectOperationMode(const SPTargetDeviceInfo_t *selectedDeviceInfo,
1122 OicSecDpom_t **selectedMode)
1126 while (i < gNumOfProvisioningMethodsPT && j < selectedDeviceInfo->pstat->smLen)
1128 if (gProvisioningToolCapability[i] < selectedDeviceInfo->pstat->sm[j])
1132 else if (selectedDeviceInfo->pstat->sm[j] < gProvisioningToolCapability[i])
1136 else /* if gProvisioningToolCapability[i] == deviceSupportedMethods[j] */
1138 *selectedMode = &(gProvisioningToolCapability[j]);
1145 * Function to perform onwership tranfer based to ownership transfer mode.
1147 * @param[in] timeout timeout in secs to perform operation. 0 timeout means
1148 function will wait forever.
1149 * @param[in] selectedDeviceInfo instance of SPTargetDeviceInfo_t structure.
1150 * @return SP_SUCCESS on success
1152 static SPResult doOwnerShipTransfer(unsigned short timeout,
1153 SPTargetDeviceInfo_t *selectedDeviceInfo)
1155 OicSecDpom_t *selectedOperationMode = NULL;
1156 selectOperationMode(selectedDeviceInfo, &selectedOperationMode);
1158 SPResult res = updateOperationMode(timeout, selectedDeviceInfo, *selectedOperationMode);
1159 if (SP_RESULT_SUCCESS != res)
1161 OC_LOG(ERROR, TAG, "Error while updating operation mode.");
1162 return SP_RESULT_INTERNAL_ERROR;
1164 if (*selectedOperationMode == SINGLE_SERVICE_CLIENT_DRIVEN)
1166 CAEndpoint_t endpoint = {0};
1167 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
1168 endpoint.port = CA_SECURE_PORT;
1170 res = initiateDtlsHandshake(&endpoint);
1171 if (SP_RESULT_SUCCESS == res)
1173 selectedDeviceInfo->endpoint.port = CA_SECURE_PORT;
1174 res = sendOwnershipInfo(timeout, selectedDeviceInfo);
1175 if (SP_RESULT_SUCCESS != res)
1177 OC_LOG(ERROR, TAG, "Error while updating ownership information.");
1179 res = saveOwnerPSK(selectedDeviceInfo);
1181 //Close temporal DTLS session
1182 if(CA_STATUS_OK != CACloseDtlsSession(&endpoint))
1184 OC_LOG(WARNING, TAG, "doOwnerShipTransfer() : failed to close the dtls session");
1189 OC_LOG(ERROR, TAG, "Error during initiating DTLS handshake.");
1192 //Disable Anonymous ECDH cipher suite before leaving this method
1193 if(CA_STATUS_OK != CAEnableAnonECDHCipherSuite(false))
1195 OC_LOG(WARNING, TAG, "doOwnerShipTransfer() : failed to disable Anon ECDH cipher suite");
1198 return (res != SP_RESULT_SUCCESS) ? SP_RESULT_INTERNAL_ERROR : SP_RESULT_SUCCESS;
1203 * Function to provision credentials to specific device.
1205 * @param[in] timeout timeout in secs to perform operation. 0 timeout means function will
1207 * @param[in] cred credential to be provisioned.
1208 * @param[in] deviceInfo Instance of SPDevInfo_t structure. Representing a selected device for
1210 * @return SP_SUCCESS on success
1212 SPResult provisionCredentials(unsigned short timeout, const OicSecCred_t *cred,
1213 const SPDevInfo_t *deviceInfo)
1215 char *credJson = NULL;
1216 credJson = BinToCredJSON(cred);
1217 if (NULL == credJson)
1219 OC_LOG(ERROR, TAG, "Memory allocation problem");
1220 return SP_RESULT_MEM_ALLOCATION_FAIL;
1223 int payloadLen = strlen(credJson);
1224 handler = &CredProvisioningHandler;
1225 gStateManager |= SP_PROV_CRED_STARTED;
1227 CAResult_t result = sendCARequest(CA_POST,
1228 &deviceInfo->endpoint,
1231 credJson, payloadLen);
1233 if (CA_STATUS_OK != result)
1235 OC_LOG(ERROR, TAG, "Internal Error while sending Credentials.");
1236 CADestroyToken(gToken);
1237 return convertCAResultToSPResult(result);
1240 SPResult res = SPTimeout(timeout, SP_PROV_CRED_DONE);
1241 if (SP_RESULT_SUCCESS != res)
1243 OC_LOG(ERROR, TAG, "Internal Error occured");
1244 CADestroyToken(gToken);
1245 return SP_RESULT_TIMEOUT;
1247 CADestroyToken(gToken);
1252 SPResult SPProvisioningDiscovery(unsigned short timeout,
1253 SPTargetDeviceInfo_t **list)
1257 OC_LOG(ERROR, TAG, "List is not null can cause memory leak");
1258 return SP_RESULT_INVALID_PARAM;
1260 SRMRegisterProvisioningResponseHandler(SPResponseHandler);
1261 SPResult smResponse = SP_RESULT_SUCCESS;
1262 smResponse = findResource(timeout);
1263 if (SP_RESULT_SUCCESS != smResponse)
1265 return SP_RESULT_INTERNAL_ERROR;
1267 if (gStateManager & SP_DISCOVERY_DONE)
1269 if (gStateManager & SP_DISCOVERY_ERROR)
1271 return SP_RESULT_INTERNAL_ERROR;
1273 *list = gStartOfDiscoveredDevices;
1274 return SP_RESULT_SUCCESS;
1276 return SP_RESULT_INTERNAL_ERROR;
1279 SPResult SPInitProvisionContext(unsigned short timeout,
1280 SPTargetDeviceInfo_t *selectedDeviceInfo)
1282 if (NULL == selectedDeviceInfo )
1284 return SP_RESULT_INVALID_PARAM;
1287 SPResult res = SP_RESULT_SUCCESS;
1288 OicSecOxm_t selectedMethod = OIC_JUST_WORKS;
1290 selectProvisioningMethod(selectedDeviceInfo->doxm->oxm, selectedDeviceInfo->doxm->oxmLen,
1292 OC_LOG_V(DEBUG, TAG, "Selected method %d:", selectedMethod);
1293 res = updateOwnerTransferModeToResource(timeout, selectedDeviceInfo, selectedMethod);
1295 if (SP_RESULT_SUCCESS != res)
1297 OC_LOG(ERROR, TAG, "Error while updating owner transfer mode.");
1298 return SP_RESULT_INTERNAL_ERROR;
1301 res = getProvisioningStatusResource(timeout, selectedDeviceInfo);
1302 if (SP_RESULT_SUCCESS != res)
1304 OC_LOG(ERROR, TAG, "Error while getting provisioning status.");
1305 return SP_RESULT_INTERNAL_ERROR;
1307 OC_LOG(INFO, TAG, "Starting ownership transfer");
1308 return doOwnerShipTransfer(timeout, selectedDeviceInfo);
1312 SPResult SPProvisionACL(unsigned short timeout, const SPTargetDeviceInfo_t *selectedDeviceInfo,
1315 if (NULL == selectedDeviceInfo || NULL == acl)
1317 return SP_RESULT_INVALID_PARAM;
1319 char *aclString = NULL;
1320 aclString = BinToAclJSON(acl);
1322 if (NULL == aclString)
1324 OC_LOG(ERROR, TAG, "Memory allocation problem");
1325 return SP_RESULT_MEM_ALLOCATION_FAIL;
1328 int payloadLen = strlen(aclString);
1329 handler = &ACLProvisioningHandler;
1330 gStateManager |= SP_PROV_ACL_STARTED;
1332 CAResult_t result = sendCARequest(CA_POST,
1333 &selectedDeviceInfo->endpoint,
1336 aclString, payloadLen);
1338 if (CA_STATUS_OK != result)
1340 OC_LOG(ERROR, TAG, "Internal Error while sending ACL.");
1341 CADestroyToken(gToken);
1342 return convertCAResultToSPResult(result);
1345 SPResult res = SPTimeout(timeout, SP_PROV_ACL_DONE);
1346 if (SP_RESULT_SUCCESS != res)
1348 OC_LOG(ERROR, TAG, "Internal Error occured");
1349 CADestroyToken(gToken);
1350 return SP_RESULT_TIMEOUT;
1352 CADestroyToken(gToken);
1356 SPResult SPProvisionCredentials(unsigned short timeout, OicSecCredType_t type,
1357 const SPDevInfo_t *pDevList)
1359 if (NULL == pDevList)
1361 return SP_RESULT_INVALID_PARAM;
1363 const SPDevInfo_t *curr = pDevList;
1364 OicUuid_t provTooldeviceID = {};
1365 if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID))
1367 OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
1368 return SP_RESULT_INTERNAL_ERROR;
1370 //TODO Need to support other key types in future.
1373 case SYMMETRIC_PAIR_WISE_KEY:
1375 if (NULL == curr->next)
1377 return SP_RESULT_INVALID_PARAM;
1379 // Devices if present after second node will not be considered.
1380 // in scenario-2. 2 devices are provisioned with credentials.
1381 const SPDevInfo_t *firstDevice = curr;
1382 const SPDevInfo_t *secondDevice = curr->next;
1384 OicSecCred_t *firstCred = NULL;
1385 OicSecCred_t *secondCred = NULL;
1387 SPResult res = SPGeneratePairWiseCredentials(type, &provTooldeviceID,
1388 &firstDevice->deviceId, &secondDevice->deviceId,
1389 &firstCred, &secondCred);
1390 if (res != SP_RESULT_SUCCESS)
1392 OC_LOG(ERROR, TAG, "error while generating credentials");
1393 return SP_RESULT_INTERNAL_ERROR;
1395 res = provisionCredentials(timeout, firstCred, firstDevice);
1396 if (SP_RESULT_SUCCESS != res)
1398 OC_LOG_V(ERROR, TAG, "Credentials provisioning Error");
1399 DeleteCredList(firstCred);
1400 DeleteCredList(secondCred);
1401 return SP_RESULT_INTERNAL_ERROR;
1403 res = provisionCredentials(timeout, secondCred, secondDevice);
1404 if (SP_RESULT_SUCCESS != res)
1406 OC_LOG_V(ERROR, TAG, "Credentials provisioning Error");
1407 DeleteCredList(firstCred);
1408 DeleteCredList(secondCred);
1409 return SP_RESULT_INTERNAL_ERROR;
1411 DeleteCredList(firstCred);
1412 DeleteCredList(secondCred);
1413 return SP_RESULT_SUCCESS;
1417 OC_LOG(ERROR, TAG, "Invalid option.");
1418 return SP_RESULT_INVALID_PARAM;
1420 return SP_RESULT_INTERNAL_ERROR;
1424 SPResult SPFinalizeProvisioning(unsigned short timeout,
1425 SPTargetDeviceInfo_t *selectedDeviceInfo)
1428 if (NULL == selectedDeviceInfo)
1430 OC_LOG(ERROR, TAG, "Target device Info is NULL.");
1431 return SP_RESULT_INVALID_PARAM;
1434 uint16_t aclHash = 0; // value for beachhead version.
1435 selectedDeviceInfo->pstat->commitHash = aclHash;
1436 selectedDeviceInfo->pstat->tm = NORMAL;
1437 char *payloadBuffer = BinToPstatJSON(selectedDeviceInfo->pstat);
1438 if (NULL == payloadBuffer)
1440 OC_LOG(ERROR, TAG, "Error while converting pstat bin to json");
1441 return SP_RESULT_INTERNAL_ERROR;
1443 int payloadLen = strlen(payloadBuffer);
1445 handler = &FinalizeProvisioningHandler;
1446 gStateManager |= SP_UP_HASH_STARTED;
1448 CAResult_t result = sendCARequest(CA_PUT,
1449 &selectedDeviceInfo->endpoint,
1452 payloadBuffer, payloadLen);
1453 OICFree(payloadBuffer);
1454 if (CA_STATUS_OK != result)
1456 OC_LOG(ERROR, TAG, "Internal Error occured");
1457 CADestroyToken(gToken);
1458 return convertCAResultToSPResult(result);
1461 SPResult res = SPTimeout(timeout, SP_UP_HASH_DONE);
1462 if (SP_RESULT_SUCCESS != res)
1464 OC_LOG(ERROR, TAG, "Internal Error occured");
1465 CADestroyToken(gToken);
1466 return SP_RESULT_TIMEOUT;
1469 CAEndpoint_t endpoint = {};
1470 OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr);
1471 endpoint.port = CA_SECURE_PORT;
1473 result = CACloseDtlsSession(&endpoint);
1474 if (CA_STATUS_OK != result)
1476 OC_LOG(WARNING, TAG, "Failed to close the DTLS session.");
1479 CADestroyToken(gToken);
1485 SPResult SPTerminateProvisioning()
1488 return SP_RESULT_SUCCESS;;