Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / security / src / srmutility.c
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #define _POSIX_C_SOURCE 200112L
21 #include <string.h>
22
23 #include "srmutility.h"
24 #include "srmresourcestrings.h"
25 #include "logger.h"
26 #include "oic_malloc.h"
27 #include "base64.h"
28
29 #define TAG  "SRM-UTILITY"
30
31 /**
32  * This method initializes the OicParseQueryIter_t struct
33  *
34  *@param query     - REST query, to be parsed
35  *@param parseIter - OicParseQueryIter_t struct, to be initialized
36  *
37  */
38 void ParseQueryIterInit(unsigned char * query, OicParseQueryIter_t * parseIter)
39 {
40     OC_LOG (INFO, TAG, "Initializing coap iterator");
41     if((NULL == query) || (NULL == parseIter))
42         return;
43
44     parseIter->attrPos = NULL;
45     parseIter->attrLen = 0;
46     parseIter->valPos = NULL;
47     parseIter->valLen = 0;
48     coap_parse_iterator_init(query, strlen((char *)query),
49           (unsigned char *)OIC_SEC_REST_QUERY_SEPARATOR, (unsigned char *) "", 0, &parseIter->pi);
50 }
51
52 /**
53  * This method fills the OicParseQueryIter_t struct with next REST query's
54  * attribute's and value's information
55  *
56  *@param parseIter - OicParseQueryIter_t struct, has next query's attribute's & value's info
57  *
58  * @retval
59  *     OicParseQueryIter_t *  - has parsed query info
60  *     NULL                   - has no query to parse
61  */
62 OicParseQueryIter_t * GetNextQuery(OicParseQueryIter_t * parseIter)
63 {
64     OC_LOG (INFO, TAG, "Getting Next Query");
65     if(NULL == parseIter)
66         return NULL;
67
68     unsigned char * qrySeg = NULL;
69     char * delimPos;
70
71     //Get the next query. Querys are separated by OIC_SEC_REST_QUERY_SEPARATOR
72     qrySeg = coap_parse_next(&parseIter->pi);
73
74     if(qrySeg)
75     {
76         delimPos = strchr((char *)qrySeg, OIC_SEC_REST_QUERY_DELIMETER);
77         if(delimPos)
78         {
79             parseIter->attrPos = parseIter->pi.pos;
80             parseIter->attrLen = (unsigned char *)delimPos - parseIter->pi.pos;
81             parseIter->valPos  = (unsigned char *)delimPos + 1;
82             parseIter->valLen  = &qrySeg[parseIter->pi.segment_length] - parseIter->valPos;
83             return parseIter;
84         }
85     }
86     return NULL;
87 }
88
89
90 // TODO This functionality is replicated in all SVR's and therefore we need
91 // to encapsulate it in a common method. However, this may not be the right
92 // file for this method.
93 OCStackResult AddUuidArray(cJSON* jsonRoot, const char* arrayItem,
94                            size_t *numUuids, OicUuid_t** uuids )
95 {
96     size_t idxx = 0;
97     cJSON* jsonObj = cJSON_GetObjectItem(jsonRoot, arrayItem);
98     VERIFY_NON_NULL(TAG, jsonObj, ERROR);
99     VERIFY_SUCCESS(TAG, cJSON_Array == jsonObj->type, ERROR);
100
101     *numUuids = cJSON_GetArraySize(jsonObj);
102     VERIFY_SUCCESS(TAG, *numUuids > 0, ERROR);
103     *uuids = (OicUuid_t*)OICCalloc(*numUuids, sizeof(OicUuid_t));
104     VERIFY_NON_NULL(TAG, *uuids, ERROR);
105
106     do
107     {
108         unsigned char base64Buff[sizeof(((OicUuid_t*)0)->id)] = {};
109         uint32_t outLen = 0;
110         B64Result b64Ret = B64_OK;
111
112         cJSON *jsonOwnr = cJSON_GetArrayItem(jsonObj, idxx);
113         VERIFY_NON_NULL(TAG, jsonOwnr, ERROR);
114         VERIFY_SUCCESS(TAG, cJSON_String == jsonOwnr->type, ERROR);
115
116         outLen = 0;
117         b64Ret = b64Decode(jsonOwnr->valuestring, strlen(jsonOwnr->valuestring), base64Buff,
118                 sizeof(base64Buff), &outLen);
119
120         VERIFY_SUCCESS(TAG, (b64Ret == B64_OK && outLen <= sizeof((*uuids)[idxx].id)),
121                 ERROR);
122         memcpy((*uuids)[idxx].id, base64Buff, outLen);
123     } while ( ++idxx < *numUuids);
124
125     return OC_STACK_OK;
126
127 exit:
128     return OC_STACK_ERROR;
129
130 }