1 //******************************************************************
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #define _POSIX_C_SOURCE 200112L
23 #include "srmutility.h"
24 #include "srmresourcestrings.h"
26 #include "oic_malloc.h"
27 #include "oic_string.h"
29 #include "doxmresource.h"
30 #include "pstatresource.h"
32 #include "casecurityinterface.h"
33 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
34 #include "pkix_interface.h"
37 #define TAG "OIC_SRM_UTILITY"
39 void ParseQueryIterInit(const unsigned char * query, OicParseQueryIter_t * parseIter)
41 OIC_LOG(INFO, TAG, "Initializing coap iterator");
42 if ((NULL == query) || (NULL == parseIter))
47 parseIter->attrPos = NULL;
48 parseIter->attrLen = 0;
49 parseIter->valPos = NULL;
50 parseIter->valLen = 0;
51 coap_parse_iterator_init((unsigned char *)query, strlen((char *)query),
52 (unsigned char *)OIC_SEC_REST_QUERY_SEPARATOR,
53 (unsigned char *) "", 0, &parseIter->pi);
56 OicParseQueryIter_t * GetNextQuery(OicParseQueryIter_t * parseIter)
58 OIC_LOG(INFO, TAG, "Getting Next Query");
59 if (NULL == parseIter)
64 unsigned char * qrySeg = NULL;
67 // Get the next query. Querys are separated by OIC_SEC_REST_QUERY_SEPARATOR.
68 qrySeg = coap_parse_next(&parseIter->pi);
72 delimPos = strchr((char *)qrySeg, OIC_SEC_REST_QUERY_DELIMETER);
75 parseIter->attrPos = parseIter->pi.pos;
76 parseIter->attrLen = (unsigned char *)delimPos - parseIter->pi.pos;
77 parseIter->valPos = (unsigned char *)delimPos + 1;
78 parseIter->valLen = &qrySeg[parseIter->pi.segment_length] - parseIter->valPos;
85 // TODO This functionality is replicated in all SVR's and therefore we need
86 // to encapsulate it in a common method. However, this may not be the right
87 // file for this method.
88 OCStackResult AddUuidArray(const cJSON* jsonRoot, const char* arrayItem,
89 size_t *numUuids, OicUuid_t** uuids)
92 cJSON* jsonObj = cJSON_GetObjectItem((cJSON *)jsonRoot, arrayItem);
93 VERIFY_NON_NULL(TAG, jsonObj, ERROR);
94 VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
96 *numUuids = (size_t)cJSON_GetArraySize(jsonObj);
97 VERIFY_SUCCESS(TAG, *numUuids > 0, ERROR);
98 *uuids = (OicUuid_t*)OICCalloc(*numUuids, sizeof(OicUuid_t));
99 VERIFY_NON_NULL(TAG, *uuids, ERROR);
103 unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {0};
105 B64Result b64Ret = B64_OK;
107 cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
108 VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
109 VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
112 b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
113 sizeof(base64Buff), &outLen);
115 VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof((*uuids)[idxx].id)),
117 memcpy((*uuids)[idxx].id, base64Buff, outLen);
118 } while ( ++idxx < *numUuids);
123 return OC_STACK_ERROR;
128 * Function to getting string of ownership transfer method
130 * @prarm oxmType ownership transfer method
132 * @return string value of ownership transfer method
134 const char* GetOxmString(OicSecOxm_t oxmType)
139 return OXM_JUST_WORKS;
140 case OIC_RANDOM_DEVICE_PIN:
141 return OXM_RANDOM_DEVICE_PIN;
142 case OIC_MANUFACTURER_CERTIFICATE:
143 return OXM_MANUFACTURER_CERTIFICATE;
144 case OIC_RAW_PUB_KEY:
145 return OXM_RAW_PUBLIC_KEY;
146 #ifdef MULTIPLE_OWNER
147 case OIC_PRECONFIG_PIN:
148 return OXM_PRECONF_PIN;
149 #endif //MULTIPLE_OWNER
150 case OIC_MV_JUST_WORKS:
151 return OXM_MV_JUST_WORKS;
152 case OIC_CON_MFG_CERT:
153 return OXM_CON_MFG_CERT;
159 OCStackResult ConvertUuidToStr(const OicUuid_t* uuid, char** strUuid)
161 if(NULL == uuid || NULL == strUuid || NULL != *strUuid)
163 OIC_LOG(ERROR, TAG, "ConvertUuidToStr : Invalid param");
164 return OC_STACK_INVALID_PARAM;
169 const size_t urnBufSize = (UUID_LENGTH * 2) + 4 + 1;
170 char* convertedUrn = (char*)OICCalloc(urnBufSize, sizeof(char));
171 VERIFY_NON_NULL(TAG, convertedUrn, ERROR);
173 for(uuidIdx=0, urnIdx=0; uuidIdx < UUID_LENGTH && urnIdx < urnBufSize; uuidIdx++, urnIdx+=2)
175 // canonical format for UUID has '8-4-4-4-12'
176 if(uuidIdx==4 || uuidIdx==6 || uuidIdx==8 || uuidIdx==10)
178 snprintf(convertedUrn + urnIdx, 2, "%c", '-');
181 snprintf(convertedUrn + urnIdx, 3, "%02x", (uint8_t)(uuid->id[uuidIdx]));
183 convertedUrn[urnBufSize - 1] = '\0';
185 *strUuid = convertedUrn;
189 return OC_STACK_NO_MEMORY;
192 OCStackResult ConvertStrToUuid(const char* strUuid, OicUuid_t* uuid)
194 if(NULL == strUuid || NULL == uuid)
196 OIC_LOG(ERROR, TAG, "ConvertStrToUuid : Invalid param");
197 return OC_STACK_INVALID_PARAM;
202 size_t strUuidLen = 0;
203 char convertedUuid[UUID_LENGTH * 2] = {0};
205 strUuidLen = strlen(strUuid);
208 OIC_LOG(INFO, TAG, "The empty string detected, The UUID will be converted to "\
209 "\"00000000-0000-0000-0000-000000000000\"");
211 else if(UUID_LENGTH * 2 + 4 == strUuidLen)
213 for(uuidIdx=0, urnIdx=0; uuidIdx < UUID_LENGTH ; uuidIdx++, urnIdx+=2)
215 if(*(strUuid + urnIdx) == '-')
219 sscanf(strUuid + urnIdx, "%2hhx", &convertedUuid[uuidIdx]);
224 OIC_LOG(ERROR, TAG, "Invalid string uuid format, Please set the uuid as correct format");
225 OIC_LOG(ERROR, TAG, "e.g) \"72616E64-5069-6E44-6576-557569643030\" (4-2-2-2-6)");
226 OIC_LOG(ERROR, TAG, "e.g) \"\"");
228 return OC_STACK_INVALID_PARAM;
231 memcpy(uuid->id, convertedUuid, UUID_LENGTH);
236 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
237 OCStackResult SetDeviceIdSeed(const uint8_t* seed, size_t seedSize)
239 return SetDoxmDeviceIDSeed(seed, seedSize);
242 static OicSecOtmEventHandler_t gOtmEventHandler = NULL;
243 static char ptAddr[256] = {0};
244 static uint16_t ptPort = 0;
246 void SetOtmEventHandler(OicSecOtmEventHandler_t otmEventHandler)
248 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
250 memset(ptAddr, 0x00, sizeof(ptAddr));
252 gOtmEventHandler = otmEventHandler;
253 OIC_LOG_V(DEBUG, TAG, "Out%s", __func__);
257 * Function to handle the handshake result in OTM.
258 * This function will be invoked after DTLS handshake
259 * @param endPoint [IN] The remote endpoint.
260 * @param errorInfo [IN] Error information from the endpoint.
263 static void DTLSHandshakeServerCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
265 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
266 if(NULL != endpoint && NULL != info)
268 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
269 endpoint->addr, endpoint->port, info->result);
271 //We can't know about PT's secure port, so compare only adress to identify the PT.
272 if (strncmp(endpoint->addr, ptAddr, strlen(ptAddr)) == 0)
274 OIC_LOG_V(INFO, TAG, "Normal port is [%s:%d]", ptAddr, ptPort);
276 //If DTLS handshake error occurred, revert secure resource and notify error event to application.
277 if (CA_STATUS_OK != info->result)
279 OIC_LOG(ERROR, TAG, "Failed to establish a secure session with owner device.");
280 OIC_LOG(ERROR, TAG, "Doxm/Pstat resource will be reverted to init state.");
281 RestoreDoxmToInitState();
282 RestorePstatToInitState();
283 InvokeOtmEventHandler(endpoint->addr, endpoint->port, NULL, OIC_OTM_ERROR);
288 OIC_LOG_V(WARNING, TAG, "[%s:%d] is not a owner device", endpoint->addr, endpoint->port);
293 OIC_LOG(WARNING, TAG, "Invalid param.");
295 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
299 void InvokeOtmEventHandler(const char* addr, uint16_t port,
300 const OicUuid_t* uuid, OicSecOtmEvent_t event)
302 char* strUuid = NULL;
303 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
305 //addr can be NULL for init state
306 //port can be '0' for BLE and init state
307 //uuid can be NULL for init state & coap
312 case OIC_OTM_STARTED:
315 OICStrcpy(ptAddr, sizeof(ptAddr), addr);
320 memset(ptAddr, 0x00, sizeof(ptAddr));
323 //Register TLS event handler to catch the tls event while handshake
324 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(DTLSHandshakeServerCB))
326 OIC_LOG(WARNING, TAG, "Failed to register (D)TLS handshake callback.");
331 memset(ptAddr, 0x00, sizeof(ptAddr));
333 //Register TLS event handler to catch the tls event while handshake
334 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(NULL))
336 OIC_LOG(WARNING, TAG, "Failed to register (D)TLS handshake callback.");
338 //Restore Pkix handler to initial state
339 CAregisterPkixInfoHandler(GetPkixInfo);
340 CAregisterGetCredentialTypesHandler(InitCipherSuiteList);
343 OIC_LOG_V(ERROR, TAG, "Unknow OTM event : %d", event);
349 if(OC_STACK_OK != ConvertUuidToStr(uuid, &strUuid))
351 OIC_LOG(ERROR, TAG, "Failed to convert UUID to String.");
356 OIC_LOG(DEBUG, TAG, "=================================");
357 OIC_LOG(DEBUG, TAG, "[OTM Event]");
358 OIC_LOG_V(DEBUG, TAG, "PT UUID : %s", (strUuid ? strUuid : "NULL"));
359 OIC_LOG_V(DEBUG, TAG, "PT Addr=%s:%d", (addr ? addr : "NULL"), port);
360 OIC_LOG_V(DEBUG, TAG, "Event Code=%d", event);
361 OIC_LOG(DEBUG, TAG, "=================================");
363 if (NULL == gOtmEventHandler)
365 OIC_LOG(WARNING, TAG, "OTM event handler is not registered.");
369 OIC_LOG(DEBUG, TAG, "Invoking callback to notify OTM state..");
370 gOtmEventHandler(addr, port, strUuid, (int)event);
377 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);