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
29 #include "cainterface.h"
31 #include "ocpayload.h"
32 #include "ocpayloadcbor.h"
34 #include "oic_malloc.h"
35 #include "payload_logging.h"
36 #include "resourcemanager.h"
37 #include "secureresourcemanager.h"
38 #include "srmresourcestrings.h"
39 #include "srmutility.h"
40 #include "pstatresource.h"
41 #include "doxmresource.h"
42 #include "ocresourcehandler.h"
44 #define TAG "OIC_SRM_PSI"
47 * Helps cover adding the name of the resource, map addition, and ending while
48 * performing CBOR encoding.
50 #define CBOR_ENCODING_SIZE_ADDITION 255
53 * Virtual database buffer block size
56 #define DB_FILE_SIZE_BLOCK 1023
58 const size_t DB_FILE_SIZE_BLOCK = 1023;
61 typedef enum _PSDatabase
63 PS_DATABASE_SECURITY = 0,
64 PS_DATABASE_DEVICEPROPERTIES
68 * Writes CBOR payload to the specified database in persistent storage.
70 * @param databaseName is the name of the database to access through persistent storage.
71 * @param payload is the CBOR payload to write to the database in persistent storage.
72 * @param size is the size of payload.
74 * @return ::OC_STACK_OK for Success, otherwise some error value
76 static OCStackResult WritePayloadToPS(const char *databaseName, uint8_t *payload, size_t size)
78 if (!databaseName || !payload || (size <= 0))
80 OC_STACK_INVALID_PARAM;
83 OCStackResult result = OC_STACK_ERROR;
85 OIC_LOG_V(DEBUG, TAG, "Writing in the file: %" PRIuPTR, size);
87 OCPersistentStorage* ps = OCGetPersistentStorageHandler();
90 FILE *fp = ps->open(databaseName, "wb");
93 size_t numberItems = ps->write(payload, 1, size, fp);
94 if (size == numberItems)
96 OIC_LOG_V(DEBUG, TAG, "Written %" PRIuPTR " bytes into %s", size, databaseName);
101 OIC_LOG_V(ERROR, TAG, "Failed writing %" PRIuPTR " in %s", numberItems, databaseName);
107 OIC_LOG(ERROR, TAG, "File open failed.");
115 * Gets the database size
117 * @param ps is a pointer to OCPersistentStorage for the Virtual Resource(s).
118 * @param databaseName is the name of the database to access through persistent storage.
120 * @return size_t - total size of the database
122 static size_t GetDatabaseSize(const OCPersistentStorage *ps, const char *databaseName)
129 char buffer[DB_FILE_SIZE_BLOCK]; // can not initialize with declaration
130 // but maybe not needed to initialize
131 FILE *fp = ps->open(databaseName, "rb");
134 size_t bytesRead = 0;
137 bytesRead = ps->read(buffer, 1, DB_FILE_SIZE_BLOCK, fp);
146 * Reads the database from PS
148 * @note Caller of this method MUST use OICFree() method to release memory
149 * referenced by the data argument.
151 * @param databaseName is the name of the database to access through persistent storage.
152 * @param resourceName is the name of the field for which file content are read.
153 * if the value is NULL it will send the content of the whole file.
154 * @param data is the pointer to the file contents read from the database.
155 * @param size is the size of the file contents read.
157 * @return ::OC_STACK_OK for Success, otherwise some error value
159 OCStackResult ReadDatabaseFromPS(const char *databaseName, const char *resourceName, uint8_t **data, size_t *size)
161 OIC_LOG(DEBUG, TAG, "ReadDatabaseFromPS IN");
163 if (!databaseName || !data || *data || !size)
165 return OC_STACK_INVALID_PARAM;
169 uint8_t *fsData = NULL;
171 OCStackResult ret = OC_STACK_ERROR;
173 OCPersistentStorage *ps = OCGetPersistentStorageHandler();
174 VERIFY_NOT_NULL(TAG, ps, ERROR);
176 fileSize = GetDatabaseSize(ps, databaseName);
177 OIC_LOG_V(DEBUG, TAG, "File Read Size: %zu", fileSize);
180 fsData = (uint8_t *) OICCalloc(1, fileSize);
181 VERIFY_NOT_NULL(TAG, fsData, ERROR);
183 fp = ps->open(databaseName, "rb");
184 VERIFY_NOT_NULL(TAG, fp, ERROR);
185 if (ps->read(fsData, 1, fileSize, fp) == fileSize)
189 CborParser parser; // will be initialized in |cbor_parser_init|
190 CborValue cbor; // will be initialized in |cbor_parser_init|
191 cbor_parser_init(fsData, fileSize, 0, &parser, &cbor);
192 CborValue cborValue = {0};
193 CborError cborFindResult = cbor_value_map_find_value(&cbor, resourceName, &cborValue);
194 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&cborValue))
196 cborFindResult = cbor_value_dup_byte_string(&cborValue, data, size, NULL);
197 VERIFY_SUCCESS(TAG, CborNoError == cborFindResult, ERROR);
200 // in case of |else (...)|, svr_data not found
202 // return everything in case resourceName is NULL
206 *data = (uint8_t *) OICCalloc(1, fileSize);
207 VERIFY_NOT_NULL(TAG, *data, ERROR);
208 memcpy(*data, fsData, fileSize);
213 OIC_LOG(DEBUG, TAG, "ReadDatabaseFromPS OUT");
225 * This method updates the database in PS
227 * @param databaseName is the name of the database to access through persistent storage.
228 * @param resourceName is the name of the resource that will be updated.
229 * @param payload is the pointer to memory where the CBOR payload is located.
230 * @param size is the size of the CBOR payload.
232 * @return ::OC_STACK_OK for Success, otherwise some error value
234 OCStackResult UpdateResourceInPS(const char *databaseName, const char *resourceName, const uint8_t *payload, size_t size)
236 OIC_LOG(DEBUG, TAG, "UpdateResourceInPS IN");
237 if (!databaseName || !resourceName)
239 return OC_STACK_INVALID_PARAM;
244 uint8_t *dbData = NULL;
245 uint8_t *outPayload = NULL;
247 uint8_t *aclCbor = NULL;
248 uint8_t *pstatCbor = NULL;
249 uint8_t *doxmCbor = NULL;
250 uint8_t *amaclCbor = NULL;
251 uint8_t *credCbor = NULL;
252 uint8_t *pconfCbor = NULL;
253 uint8_t *resetPfCbor = NULL;
254 uint8_t *crlCbor = NULL;
255 uint8_t *dpCbor = NULL;
257 int64_t cborEncoderResult = CborNoError;
258 OCStackResult ret = ReadDatabaseFromPS(databaseName, NULL, &dbData, &dbSize);
259 if (dbData && dbSize)
261 PSDatabase database = PS_DATABASE_SECURITY;
262 size_t allocSize = 0;
263 size_t aclCborLen = 0;
264 size_t pstatCborLen = 0;
265 size_t doxmCborLen = 0;
266 size_t amaclCborLen = 0;
267 size_t credCborLen = 0;
268 size_t pconfCborLen = 0;
269 size_t resetPfCborLen = 0;
270 size_t crlCborLen = 0;
271 size_t dpCborLen = 0;
273 // Determine which database we are working with so we can scope our operations
274 if (0 == strcmp(OC_DEVICE_PROPS_FILE_NAME, databaseName))
276 database = PS_DATABASE_DEVICEPROPERTIES;
279 // Gets each secure virtual resource from persistent storage
280 // this local scoping intended, for destroying large cbor instances after use
282 CborParser parser; // will be initialized in |cbor_parser_init|
283 CborValue cbor; // will be initialized in |cbor_parser_init|
284 cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
285 CborValue curVal = {0};
286 CborError cborFindResult = CborNoError;
288 // Only search for and copy resources owned by the target database
289 if (PS_DATABASE_SECURITY == database)
292 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal);
293 if ((CborNoError == cborFindResult) && cbor_value_is_byte_string(&curVal))
295 cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL);
296 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
298 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal);
299 if ((CborNoError == cborFindResult) && cbor_value_is_byte_string(&curVal))
301 cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL);
302 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
304 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal);
305 if ((CborNoError == cborFindResult) && cbor_value_is_byte_string(&curVal))
307 cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL);
308 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DOXM Name Value.");
310 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_AMACL_NAME, &curVal);
311 if ((CborNoError == cborFindResult) && cbor_value_is_byte_string(&curVal))
313 cborFindResult = cbor_value_dup_byte_string(&curVal, &amaclCbor, &amaclCborLen, NULL);
314 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding AMACL Name Value.");
316 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRED_NAME, &curVal);
317 if ((CborNoError == cborFindResult) && cbor_value_is_byte_string(&curVal))
319 cborFindResult = cbor_value_dup_byte_string(&curVal, &credCbor, &credCborLen, NULL);
320 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CRED Name Value.");
322 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PCONF_NAME, &curVal);
323 if ((CborNoError == cborFindResult) && cbor_value_is_byte_string(&curVal))
325 cborFindResult = cbor_value_dup_byte_string(&curVal, &pconfCbor, &pconfCborLen, NULL);
326 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PCONF Name Value.");
328 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_RESET_PF_NAME, &curVal);
329 if ((CborNoError == cborFindResult) && cbor_value_is_byte_string(&curVal))
331 cborFindResult = cbor_value_dup_byte_string(&curVal, &resetPfCbor, &resetPfCborLen, NULL);
332 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Reset Profile Name Value.");
334 int64_t cborFindCrlResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRL_NAME, &curVal);
335 if ((CborNoError == cborFindCrlResult) && cbor_value_is_byte_string(&curVal))
337 cborFindCrlResult = cbor_value_dup_byte_string(&curVal, &crlCbor, &crlCborLen, NULL);
338 if ((CborNoError != cborFindCrlResult) && (CborErrorOutOfMemory != cborFindCrlResult))
340 OIC_LOG(ERROR, TAG, "Failed Finding optional CRL Name Value.");
344 OIC_LOG(INFO, TAG, "Successfully Finding optional CRL Name Value.");
350 // Device Properties database
351 cborFindResult = cbor_value_map_find_value(&cbor, OC_JSON_DEVICE_PROPS_NAME, &curVal);
352 if ((CborNoError == cborFindResult) && cbor_value_is_byte_string(&curVal))
354 cborFindResult = cbor_value_dup_byte_string(&curVal, &dpCbor, &dpCborLen, NULL);
355 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Device Properties Name Value.");
360 // Updates the added |payload| with the existing secure virtual resource(s)
361 // this local scoping intended, for destroying large cbor instances after use
363 if (PS_DATABASE_SECURITY == database)
365 allocSize = aclCborLen + pstatCborLen + doxmCborLen + amaclCborLen
366 + credCborLen + pconfCborLen + resetPfCborLen + crlCborLen
367 + size + CBOR_ENCODING_SIZE_ADDITION;
371 allocSize = dpCborLen + size + CBOR_ENCODING_SIZE_ADDITION;
375 outPayload = (uint8_t *)OICCalloc(1, allocSize);
376 VERIFY_NOT_NULL(TAG, outPayload, ERROR);
377 CborEncoder encoder; // will be initialized in |cbor_parser_init|
378 cbor_encoder_init(&encoder, outPayload, allocSize, 0);
379 CborEncoder resource; // will be initialized in |cbor_encoder_create_map|
380 cborEncoderResult |= cbor_encoder_create_map(&encoder, &resource, CborIndefiniteLength);
381 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map.");
385 cborEncoderResult |= cbor_encode_text_string(&resource, resourceName, strlen(resourceName));
386 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag");
387 cborEncoderResult |= cbor_encode_byte_string(&resource, payload, size);
388 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value.");
391 if (PS_DATABASE_SECURITY == database)
394 if (strcmp(OIC_JSON_ACL_NAME, resourceName) && aclCborLen)
396 cborEncoderResult |= cbor_encode_text_string(&resource, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
397 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
398 cborEncoderResult |= cbor_encode_byte_string(&resource, aclCbor, aclCborLen);
399 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
401 else if (strcmp(OIC_JSON_PSTAT_NAME, resourceName) && pstatCborLen)
403 cborEncoderResult |= cbor_encode_text_string(&resource, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
404 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
405 cborEncoderResult |= cbor_encode_byte_string(&resource, pstatCbor, pstatCborLen);
406 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
408 else if (strcmp(OIC_JSON_DOXM_NAME, resourceName) && doxmCborLen)
410 cborEncoderResult |= cbor_encode_text_string(&resource, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
411 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Name.");
412 cborEncoderResult |= cbor_encode_byte_string(&resource, doxmCbor, doxmCborLen);
413 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Value.");
415 else if (strcmp(OIC_JSON_AMACL_NAME, resourceName) && amaclCborLen)
417 cborEncoderResult |= cbor_encode_text_string(&resource, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
418 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Name.");
419 cborEncoderResult |= cbor_encode_byte_string(&resource, amaclCbor, amaclCborLen);
420 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Value.");
422 else if (strcmp(OIC_JSON_CRED_NAME, resourceName) && credCborLen)
424 cborEncoderResult |= cbor_encode_text_string(&resource, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
425 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Name.");
426 cborEncoderResult |= cbor_encode_byte_string(&resource, credCbor, credCborLen);
427 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Value.");
429 else if (strcmp(OIC_JSON_PCONF_NAME, resourceName) && pconfCborLen)
431 cborEncoderResult |= cbor_encode_text_string(&resource, OIC_JSON_PCONF_NAME, strlen(OIC_JSON_PCONF_NAME));
432 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Name.");
433 cborEncoderResult |= cbor_encode_byte_string(&resource, pconfCbor, pconfCborLen);
434 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Value.");
436 else if (strcmp(OIC_JSON_RESET_PF_NAME, resourceName) && resetPfCborLen)
438 cborEncoderResult |= cbor_encode_text_string(&resource, OIC_JSON_RESET_PF_NAME, strlen(OIC_JSON_RESET_PF_NAME));
439 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Reset Profile Name.");
440 cborEncoderResult |= cbor_encode_byte_string(&resource, resetPfCbor, resetPfCborLen);
441 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Reset Profile Value.");
443 else if (strcmp(OIC_JSON_CRL_NAME, resourceName) && crlCborLen)
445 cborEncoderResult |= cbor_encode_text_string(&resource, OIC_JSON_CRL_NAME, strlen(OIC_JSON_CRL_NAME));
446 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Crl Name.");
447 cborEncoderResult |= cbor_encode_byte_string(&resource, crlCbor, crlCborLen);
448 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Crl Value.");
453 // Device Properties database
454 if (strcmp(OC_JSON_DEVICE_PROPS_NAME, resourceName) && dpCborLen)
456 cborEncoderResult |= cbor_encode_text_string(&resource, OC_JSON_DEVICE_PROPS_NAME, strlen(OC_JSON_DEVICE_PROPS_NAME));
457 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Properties Name.");
458 cborEncoderResult |= cbor_encode_byte_string(&resource, dpCbor, dpCborLen);
459 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Device Properties Value.");
463 cborEncoderResult |= cbor_encoder_close_container(&encoder, &resource);
464 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
465 outSize = cbor_encoder_get_buffer_size(&encoder, outPayload);
468 else if (payload && size)
470 size_t allocSize = size + CBOR_ENCODING_SIZE_ADDITION;
472 outPayload = (uint8_t *)OICCalloc(1, allocSize);
473 VERIFY_NOT_NULL(TAG, outPayload, ERROR);
474 CborEncoder encoder; // will be initialized in |cbor_parser_init|
475 cbor_encoder_init(&encoder, outPayload, allocSize, 0);
476 CborEncoder resource; // will be initialized in |cbor_encoder_create_map|
477 cborEncoderResult |= cbor_encoder_create_map(&encoder, &resource, CborIndefiniteLength);
478 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map.");
480 cborEncoderResult |= cbor_encode_text_string(&resource, resourceName, strlen(resourceName));
481 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag");
482 cborEncoderResult |= cbor_encode_byte_string(&resource, payload, size);
483 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value.");
485 cborEncoderResult |= cbor_encoder_close_container(&encoder, &resource);
486 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
487 outSize = cbor_encoder_get_buffer_size(&encoder, outPayload);
490 ret = WritePayloadToPS(databaseName, outPayload, outSize);
491 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR);
493 OIC_LOG(DEBUG, TAG, "UpdateResourceInPS OUT");
504 OICFree(resetPfCbor);
511 * Reads the Secure Virtual Database from PS
513 * @note Caller of this method MUST use OICFree() method to release memory
514 * referenced by return value.
516 * @param resourceName is the name of the field for which file content are read.
517 * if the value is NULL it will send the content of the whole file.
518 * @param data is the pointer to the file contents read from the database.
519 * @param size is the size of the file contents read.
521 * @return ::OC_STACK_OK for Success, otherwise some error value
523 OCStackResult GetSecureVirtualDatabaseFromPS(const char *resourceName, uint8_t **data, size_t *size)
525 return ReadDatabaseFromPS(SVR_DB_DAT_FILE_NAME, resourceName, data, size);
529 * This method updates the Secure Virtual Database in PS
531 * @param resourceName is the name of the secure resource that will be updated.
532 * @param payload is the pointer to memory where the CBOR payload is located.
533 * @param size is the size of the CBOR payload.
535 * @return ::OC_STACK_OK for Success, otherwise some error value
537 OCStackResult UpdateSecureResourceInPS(const char *resourceName, const uint8_t *payload, size_t size)
539 return UpdateResourceInPS(SVR_DB_DAT_FILE_NAME, resourceName, payload, size);
543 * Resets the Secure Virtual Resource(s).
544 * This function reads the Reset Profile
545 * and resets the secure virtual resources accordingly.
547 * @return OCStackResult - result of updating Secure Virtual Resource(s)
549 OCStackResult ResetSecureResourceInPS(void)
551 OIC_LOG(DEBUG, TAG, "ResetSecureResourceInPS IN");
555 uint8_t *dbData = NULL;
556 uint8_t *outPayload = NULL;
558 uint8_t *aclCbor = NULL;
559 uint8_t *credCbor = NULL;
560 uint8_t *pstatCbor = NULL;
561 uint8_t *doxmCbor = NULL;
562 uint8_t *resetPfCbor = NULL;
564 int64_t cborEncoderResult = CborNoError;
565 OCStackResult ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
568 size_t aclCborLen = 0;
569 size_t credCborLen = 0;
570 size_t pstatCborLen = 0;
571 size_t doxmCborLen = 0;
572 size_t resetPfCborLen = 0;
574 // Gets the reset profile from persistent storage
576 CborParser parser; // will be initialized in |cbor_parser_init|
577 CborValue cbor; // will be initialized in |cbor_parser_init|
578 cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
579 CborValue curVal = {0};
580 CborError cborFindResult = CborNoError;
581 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_RESET_PF_NAME, &curVal);
582 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
584 cborFindResult = cbor_value_dup_byte_string(&curVal, &resetPfCbor, &resetPfCborLen, NULL);
585 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Reset Profile Name Value.");
589 // Gets each secure virtual resource from the reset profile
591 CborParser parser; // will be initialized in |cbor_parser_init|
592 CborValue cbor; // will be initialized in |cbor_parser_init|
593 cbor_parser_init(resetPfCbor, resetPfCborLen, 0, &parser, &cbor);
594 CborValue curVal = {0};
595 CborError cborFindResult = CborNoError;
596 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal);
597 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
599 cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL);
600 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
602 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRED_NAME, &curVal);
603 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
605 cborFindResult = cbor_value_dup_byte_string(&curVal, &credCbor, &credCborLen, NULL);
606 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
608 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal);
609 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
611 cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL);
612 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
614 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal);
615 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
617 cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL);
618 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DOXM Name Value.");
623 size_t size = aclCborLen + credCborLen + pstatCborLen + doxmCborLen + resetPfCborLen + CBOR_ENCODING_SIZE_ADDITION;
625 outPayload = (uint8_t *) OICCalloc(1, size);
626 VERIFY_NOT_NULL(TAG, outPayload, ERROR);
628 cbor_encoder_init(&encoder, outPayload, size, 0);
630 cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
632 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
633 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
634 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, aclCbor, aclCborLen);
635 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
639 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
640 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
641 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, credCbor, credCborLen);
642 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
645 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
646 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
647 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pstatCbor, pstatCborLen);
648 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
650 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
651 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name.");
652 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, doxmCbor, doxmCborLen);
653 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value.");
655 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_RESET_PF_NAME, strlen(OIC_JSON_RESET_PF_NAME));
656 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Reset Profile Name.");
657 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, resetPfCbor, resetPfCborLen);
658 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Reset Profile Value.");
660 cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
661 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
662 outSize = cbor_encoder_get_buffer_size(&encoder, outPayload);
665 ret = WritePayloadToPS(SVR_DB_DAT_FILE_NAME, outPayload, outSize);
666 VERIFY_SUCCESS(TAG, (OC_STACK_OK == ret), ERROR);
669 SRMDeInitSecureResources();
670 InitSecureResources();
671 OIC_LOG(DEBUG, TAG, "ResetSecureResourceINPS OUT");
680 OICFree(resetPfCbor);
685 * Creates Reset Profile from the initial secure virtual resources.
686 * This function copies the secure resources
687 * and creates the Reset Profile in the Persistent Storage.
688 * Device ID in doxm and pstat remains as same after reset.
690 * @return OCStackResult - result of updating Secure Virtual Resource(s)
692 OCStackResult CreateResetProfile(void)
694 OIC_LOG(DEBUG, TAG, "CreateResetProfile IN");
697 uint8_t *dbData = NULL;
699 uint8_t *aclCbor = NULL;
700 uint8_t *credCbor = NULL;
701 uint8_t *pstatCbor = NULL;
702 uint8_t *doxmCbor = NULL;
703 uint8_t *resetPfCbor = NULL;
705 OCStackResult ret = OC_STACK_ERROR;
706 int64_t cborEncoderResult = CborNoError;
707 ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
708 if (dbData && dbSize)
710 size_t aclCborLen = 0;
711 size_t credCborLen = 0;
712 size_t pstatCborLen = 0;
713 size_t doxmCborLen = 0;
714 size_t resetPfCborLen = 0;
719 cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
720 CborValue curVal = {0};
721 CborError cborFindResult = CborNoError;
723 // abort if reset profile exists
724 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_RESET_PF_NAME, &curVal);
725 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
727 OIC_LOG(DEBUG, TAG, "Reset Profile already exists!!");
731 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal);
732 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
734 cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL);
735 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
737 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRED_NAME, &curVal);
738 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
740 cborFindResult = cbor_value_dup_byte_string(&curVal, &credCbor, &credCborLen, NULL);
741 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CRED Name Value.");
743 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal);
744 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
746 cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL);
747 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
749 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal);
750 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
752 cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL);
753 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DOXM Name Value.");
758 size_t size = aclCborLen + credCborLen + pstatCborLen + doxmCborLen + CBOR_ENCODING_SIZE_ADDITION;
759 resetPfCbor = (uint8_t *) OICCalloc(1, size);
760 VERIFY_NOT_NULL(TAG, resetPfCbor, ERROR);
761 CborEncoder encoder; // will be initialized in |cbor_parser_init|
762 cbor_encoder_init(&encoder, resetPfCbor, size, 0);
763 CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map|
764 cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
766 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
767 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
768 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, aclCbor, aclCborLen);
769 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
773 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
774 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
775 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, credCbor, credCborLen);
776 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
779 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
780 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
781 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pstatCbor, pstatCborLen);
782 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
784 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
785 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Name.");
786 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, doxmCbor, doxmCborLen);
787 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Value.");
789 cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
790 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
791 resetPfCborLen = cbor_encoder_get_buffer_size(&encoder, resetPfCbor);
794 UpdateSecureResourceInPS(OIC_JSON_RESET_PF_NAME, resetPfCbor, resetPfCborLen);
797 OIC_LOG(DEBUG, TAG, "CreateResetProfile OUT");
805 OICFree(resetPfCbor);