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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #define __STDC_LIMIT_MACROS
28 #include "cainterface.h"
30 #include "ocpayload.h"
31 #include "ocpayloadcbor.h"
33 #include "oic_malloc.h"
34 #include "payload_logging.h"
35 #include "resourcemanager.h"
36 #include "secureresourcemanager.h"
37 #include "srmresourcestrings.h"
38 #include "srmutility.h"
42 //SVR database buffer block size
44 #define DB_FILE_SIZE_BLOCK 1023
46 const size_t DB_FILE_SIZE_BLOCK = 1023;
50 * Gets the Secure Virtual Database size
52 * @param ps - pointer of OCPersistentStorage for the Secure Virtual Resource(s)
54 * @return size_t - total size of the SVR database
56 static size_t GetSVRDatabaseSize(const OCPersistentStorage *ps)
63 char buffer[DB_FILE_SIZE_BLOCK]; // can not initialize with declaration
64 // but maybe not needed to initialize
65 FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
71 bytesRead = ps->read(buffer, 1, DB_FILE_SIZE_BLOCK, fp);
80 * Gets the Secure Virtual Database from the Persistent Storage
82 * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
83 * @param data - pointer of the returned Secure Virtual Resource(s)
84 * @param size - pointer of the returned size of Secure Virtual Resource(s)
86 * @return OCStackResult - result of getting Secure Virtual Resource(s)
88 OCStackResult GetSecureVirtualDatabaseFromPS(const char *rsrcName, uint8_t **data, size_t *size)
90 OIC_LOG(DEBUG, TAG, "GetSecureVirtualDatabaseFromPS IN");
91 if (!data || *data || !size)
93 return OC_STACK_INVALID_PARAM;
97 uint8_t *fsData = NULL;
99 OCStackResult ret = OC_STACK_ERROR;
101 OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
102 VERIFY_NON_NULL(TAG, ps, ERROR);
104 fileSize = GetSVRDatabaseSize(ps);
105 OIC_LOG_V(DEBUG, TAG, "File Read Size: %zu", fileSize);
108 fsData = (uint8_t *) OICCalloc(1, fileSize);
109 VERIFY_NON_NULL(TAG, fsData, ERROR);
111 fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
112 VERIFY_NON_NULL(TAG, fp, ERROR);
113 if (ps->read(fsData, 1, fileSize, fp) == fileSize)
117 CborParser parser; // will be initialized in |cbor_parser_init|
118 CborValue cbor; // will be initialized in |cbor_parser_init|
119 cbor_parser_init(fsData, fileSize, 0, &parser, &cbor);
120 CborValue cborValue = {0};
121 CborError cborFindResult = cbor_value_map_find_value(&cbor, rsrcName, &cborValue);
122 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&cborValue))
124 cborFindResult = cbor_value_dup_byte_string(&cborValue, data, size, NULL);
125 VERIFY_SUCCESS(TAG, CborNoError==cborFindResult, ERROR);
128 // in case of |else (...)|, svr_data not found
130 // return everything in case rsrcName is NULL
134 *data = (uint8_t *) OICCalloc(1, fileSize);
135 VERIFY_NON_NULL(TAG, *data, ERROR);
136 memcpy(*data, fsData, fileSize);
141 OIC_LOG(DEBUG, TAG, "GetSecureVirtualDatabaseFromPS OUT");
153 * Updates the Secure Virtual Resource(s) into the Persistent Storage.
154 * This function stores cbor-payload of each resource by appending resource name,
155 * and empty payload implies deleting the value
157 * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
158 * @param psPayload - pointer of the updated Secure Virtual Resource(s)
159 * @param psSize - the updated size of Secure Virtual Resource(s)
161 * @return OCStackResult - result of updating Secure Virtual Resource(s)
163 OCStackResult UpdateSecureResourceInPS(const char *rsrcName, const uint8_t *psPayload, size_t psSize)
165 OIC_LOG(DEBUG, TAG, "UpdateSecureResourceInPS IN");
168 return OC_STACK_INVALID_PARAM;
173 uint8_t *dbData = NULL;
174 uint8_t *outPayload = NULL;
176 uint8_t *aclCbor = NULL;
177 uint8_t *pstatCbor = NULL;
178 uint8_t *doxmCbor = NULL;
179 uint8_t *amaclCbor = NULL;
180 uint8_t *svcCbor = NULL;
181 uint8_t *credCbor = NULL;
182 uint8_t *pconfCbor = NULL;
184 int64_t cborEncoderResult = CborNoError;
185 OCStackResult ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
186 if (dbData && dbSize)
188 size_t aclCborLen = 0;
189 size_t pstatCborLen = 0;
190 size_t doxmCborLen = 0;
191 size_t amaclCborLen = 0;
192 size_t svcCborLen = 0;
193 size_t credCborLen = 0;
194 size_t pconfCborLen = 0;
196 // Gets each secure virtual resource from persistent storage
197 // this local scoping intended, for destroying large cbor instances after use
199 CborParser parser; // will be initialized in |cbor_parser_init|
200 CborValue cbor; // will be initialized in |cbor_parser_init|
201 cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
202 CborValue curVal = {0};
203 CborError cborFindResult = CborNoError;
205 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal);
206 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
208 cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL);
209 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
211 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal);
212 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
214 cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL);
215 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
217 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal);
218 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
220 cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL);
221 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DOXM Name Value.");
223 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_AMACL_NAME, &curVal);
224 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
226 cborFindResult = cbor_value_dup_byte_string(&curVal, &amaclCbor, &amaclCborLen, NULL);
227 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding AMACL Name Value.");
229 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_SVC_NAME, &curVal);
230 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
232 cborFindResult = cbor_value_dup_byte_string(&curVal, &svcCbor, &svcCborLen, NULL);
233 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SVC Name Value.");
235 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRED_NAME, &curVal);
236 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
238 cborFindResult = cbor_value_dup_byte_string(&curVal, &credCbor, &credCborLen, NULL);
239 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CRED Name Value.");
241 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PCONF_NAME, &curVal);
242 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
244 cborFindResult = cbor_value_dup_byte_string(&curVal, &pconfCbor, &pconfCborLen, NULL);
245 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PCONF Name Value.");
249 // Updates the added |psPayload| with the existing secure virtual resource(s)
250 // this local scoping intended, for destroying large cbor instances after use
252 size_t size = aclCborLen + pstatCborLen + doxmCborLen + amaclCborLen
253 + svcCborLen + credCborLen + pconfCborLen + psSize + 255;
254 // This added '255' is arbitrary value that is added to cover the name of the resource, map addition and ending
256 outPayload = (uint8_t *) OICCalloc(1, size);
257 VERIFY_NON_NULL(TAG, outPayload, ERROR);
258 CborEncoder encoder; // will be initialized in |cbor_parser_init|
259 cbor_encoder_init(&encoder, outPayload, size, 0);
260 CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map|
261 cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
262 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map.");
264 if (psPayload && psSize)
266 cborEncoderResult |= cbor_encode_text_string(&secRsrc, rsrcName, strlen(rsrcName));
267 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag");
268 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, psPayload, psSize);
269 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value.");
271 if (strcmp(OIC_JSON_ACL_NAME, rsrcName) && aclCborLen)
273 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
274 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
275 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, aclCbor, aclCborLen);
276 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
278 if (strcmp(OIC_JSON_PSTAT_NAME, rsrcName) && pstatCborLen)
280 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
281 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
282 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pstatCbor, pstatCborLen);
283 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
285 if (strcmp(OIC_JSON_DOXM_NAME, rsrcName) && doxmCborLen)
287 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
288 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Name.");
289 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, doxmCbor, doxmCborLen);
290 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Value.");
292 if (strcmp(OIC_JSON_AMACL_NAME, rsrcName) && amaclCborLen)
294 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
295 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Name.");
296 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, amaclCbor, amaclCborLen);
297 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Value.");
299 if (strcmp(OIC_JSON_SVC_NAME, rsrcName) && svcCborLen)
301 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
302 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
303 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, svcCbor, svcCborLen);
304 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
306 if (strcmp(OIC_JSON_CRED_NAME, rsrcName) && credCborLen)
308 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
309 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Name.");
310 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, credCbor, credCborLen);
311 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Value.");
313 if (strcmp(OIC_JSON_PCONF_NAME, rsrcName) && pconfCborLen)
315 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PCONF_NAME, strlen(OIC_JSON_PCONF_NAME));
316 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Name.");
317 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pconfCbor, pconfCborLen);
318 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Value.");
321 cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
322 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
323 outSize = encoder.ptr - outPayload;
326 else if (psPayload && psSize)
328 size_t size = psSize + 255;
329 // This added '255' is arbitrary value that is added to cover the name of the resource, map addition and ending
331 outPayload = (uint8_t *) OICCalloc(1, size);
332 VERIFY_NON_NULL(TAG, outPayload, ERROR);
333 CborEncoder encoder; // will be initialized in |cbor_parser_init|
334 cbor_encoder_init(&encoder, outPayload, size, 0);
335 CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map|
336 cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
337 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map.");
339 cborEncoderResult |= cbor_encode_text_string(&secRsrc, rsrcName, strlen(rsrcName));
340 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag");
341 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, psPayload, psSize);
342 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value.");
344 cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
345 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
346 outSize = encoder.ptr - outPayload;
349 if (outPayload && outSize)
351 OIC_LOG_V(DEBUG, TAG, "Writting in the file: %zu", outSize);
352 OCPersistentStorage* ps = SRMGetPersistentStorageHandler();
355 FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "wb");
358 size_t numberItems = ps->write(outPayload, 1, outSize, fp);
359 if (outSize == numberItems)
361 OIC_LOG_V(DEBUG, TAG, "Written %zu bytes into SVR database file", outSize);
366 OIC_LOG_V(ERROR, TAG, "Failed writing %zu in the database", numberItems);
372 OIC_LOG(ERROR, TAG, "File open failed.");
377 OIC_LOG(DEBUG, TAG, "UpdateSecureResourceInPS OUT");