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 #ifdef MULTIPLE_OWNER
145 case OIC_PRECONFIG_PIN:
146 return OXM_PRECONF_PIN;
147 #endif //MULTIPLE_OWNER
148 case OIC_MV_JUST_WORKS:
149 return OXM_MV_JUST_WORKS;
150 case OIC_CON_MFG_CERT:
151 return OXM_CON_MFG_CERT;
157 OCStackResult ConvertUuidToStr(const OicUuid_t* uuid, char** strUuid)
159 if(NULL == uuid || NULL == strUuid || NULL != *strUuid)
161 OIC_LOG(ERROR, TAG, "ConvertUuidToStr : Invalid param");
162 return OC_STACK_INVALID_PARAM;
167 const size_t urnBufSize = (UUID_LENGTH * 2) + 4 + 1;
168 char* convertedUrn = (char*)OICCalloc(urnBufSize, sizeof(char));
169 VERIFY_NON_NULL(TAG, convertedUrn, ERROR);
171 for(uuidIdx=0, urnIdx=0; uuidIdx < UUID_LENGTH && urnIdx < urnBufSize; uuidIdx++, urnIdx+=2)
173 // canonical format for UUID has '8-4-4-4-12'
174 if(uuidIdx==4 || uuidIdx==6 || uuidIdx==8 || uuidIdx==10)
176 snprintf(convertedUrn + urnIdx, 2, "%c", '-');
179 snprintf(convertedUrn + urnIdx, 3, "%02x", (uint8_t)(uuid->id[uuidIdx]));
181 convertedUrn[urnBufSize - 1] = '\0';
183 *strUuid = convertedUrn;
187 return OC_STACK_NO_MEMORY;
190 OCStackResult ConvertStrToUuid(const char* strUuid, OicUuid_t* uuid)
192 if(NULL == strUuid || NULL == uuid)
194 OIC_LOG(ERROR, TAG, "ConvertStrToUuid : Invalid param");
195 return OC_STACK_INVALID_PARAM;
200 size_t strUuidLen = 0;
201 char convertedUuid[UUID_LENGTH * 2] = {0};
203 strUuidLen = strlen(strUuid);
206 OIC_LOG(INFO, TAG, "The empty string detected, The UUID will be converted to "\
207 "\"00000000-0000-0000-0000-000000000000\"");
209 else if(UUID_LENGTH * 2 + 4 == strUuidLen)
211 for(uuidIdx=0, urnIdx=0; uuidIdx < UUID_LENGTH ; uuidIdx++, urnIdx+=2)
213 if(*(strUuid + urnIdx) == '-')
217 sscanf(strUuid + urnIdx, "%2hhx", &convertedUuid[uuidIdx]);
222 OIC_LOG(ERROR, TAG, "Invalid string uuid format, Please set the uuid as correct format");
223 OIC_LOG(ERROR, TAG, "e.g) \"72616E64-5069-6E44-6576-557569643030\" (4-2-2-2-6)");
224 OIC_LOG(ERROR, TAG, "e.g) \"\"");
226 return OC_STACK_INVALID_PARAM;
229 memcpy(uuid->id, convertedUuid, UUID_LENGTH);
234 #if defined(__WITH_DTLS__) || defined (__WITH_TLS__)
235 OCStackResult SetDeviceIdSeed(const uint8_t* seed, size_t seedSize)
237 return SetDoxmDeviceIDSeed(seed, seedSize);
240 static OicSecOtmEventHandler_t gOtmEventHandler = NULL;
241 static char ptAddr[256] = {0};
242 static uint16_t ptPort = 0;
244 void SetOtmEventHandler(OicSecOtmEventHandler_t otmEventHandler)
246 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
248 memset(ptAddr, 0x00, sizeof(ptAddr));
250 gOtmEventHandler = otmEventHandler;
251 OIC_LOG_V(DEBUG, TAG, "Out%s", __func__);
255 * Function to handle the handshake result in OTM.
256 * This function will be invoked after DTLS handshake
257 * @param endPoint [IN] The remote endpoint.
258 * @param errorInfo [IN] Error information from the endpoint.
261 static void DTLSHandshakeServerCB(const CAEndpoint_t *endpoint, const CAErrorInfo_t *info)
263 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
264 if(NULL != endpoint && NULL != info)
266 OIC_LOG_V(INFO, TAG, "Received status from remote device(%s:%d) : %d",
267 endpoint->addr, endpoint->port, info->result);
269 //We can't know about PT's secure port, so compare only adress to identify the PT.
270 if (strncmp(endpoint->addr, ptAddr, strlen(ptAddr)) == 0)
272 OIC_LOG_V(INFO, TAG, "Normal port is [%s:%d]", ptAddr, ptPort);
274 //If DTLS handshake error occurred, revert secure resource and notify error event to application.
275 if (CA_STATUS_OK != info->result)
277 OIC_LOG(ERROR, TAG, "Failed to establish a secure session with owner device.");
278 OIC_LOG(ERROR, TAG, "Doxm/Pstat resource will be reverted to init state.");
279 RestoreDoxmToInitState();
280 RestorePstatToInitState();
281 InvokeOtmEventHandler(endpoint->addr, endpoint->port, NULL, OIC_OTM_ERROR);
286 OIC_LOG_V(WARNING, TAG, "[%s:%d] is not a owner device", endpoint->addr, endpoint->port);
291 OIC_LOG(WARNING, TAG, "Invalid param.");
293 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
297 void InvokeOtmEventHandler(const char* addr, uint16_t port,
298 const OicUuid_t* uuid, OicSecOtmEvent_t event)
300 char* strUuid = NULL;
301 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
303 //addr can be NULL for init state
304 //port can be '0' for BLE and init state
305 //uuid can be NULL for init state & coap
310 case OIC_OTM_STARTED:
313 OICStrcpy(ptAddr, sizeof(ptAddr), addr);
318 memset(ptAddr, 0x00, sizeof(ptAddr));
321 //Register TLS event handler to catch the tls event while handshake
322 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(DTLSHandshakeServerCB))
324 OIC_LOG(WARNING, TAG, "Failed to register (D)TLS handshake callback.");
329 memset(ptAddr, 0x00, sizeof(ptAddr));
331 //Register TLS event handler to catch the tls event while handshake
332 if(CA_STATUS_OK != CAregisterSslHandshakeCallback(NULL))
334 OIC_LOG(WARNING, TAG, "Failed to register (D)TLS handshake callback.");
336 //Restore Pkix handler to initial state
337 CAregisterPkixInfoHandler(GetPkixInfo);
338 CAregisterGetCredentialTypesHandler(InitCipherSuiteList);
341 OIC_LOG_V(ERROR, TAG, "Unknow OTM event : %d", event);
347 if(OC_STACK_OK != ConvertUuidToStr(uuid, &strUuid))
349 OIC_LOG(ERROR, TAG, "Failed to convert UUID to String.");
354 OIC_LOG(DEBUG, TAG, "=================================");
355 OIC_LOG(DEBUG, TAG, "[OTM Event]");
356 OIC_LOG_V(DEBUG, TAG, "PT UUID : %s", (strUuid ? strUuid : "NULL"));
357 OIC_LOG_V(DEBUG, TAG, "PT Addr=%s:%d", (addr ? addr : "NULL"), port);
358 OIC_LOG_V(DEBUG, TAG, "Event Code=%d", event);
359 OIC_LOG(DEBUG, TAG, "=================================");
361 if (NULL == gOtmEventHandler)
363 OIC_LOG(WARNING, TAG, "OTM event handler is not registered.");
367 OIC_LOG(DEBUG, TAG, "Invoking callback to notify OTM state..");
368 gOtmEventHandler(addr, port, strUuid, (int)event);
375 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);