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"
39 #include "pstatresource.h"
40 #include "doxmresource.h"
44 //SVR database buffer block size
46 #define DB_FILE_SIZE_BLOCK 1023
48 const size_t DB_FILE_SIZE_BLOCK = 1023;
52 * Gets the Secure Virtual Database size
54 * @param ps - pointer of OCPersistentStorage for the Secure Virtual Resource(s)
56 * @return size_t - total size of the SVR database
58 static size_t GetSVRDatabaseSize(const OCPersistentStorage *ps)
65 char buffer[DB_FILE_SIZE_BLOCK]; // can not initialize with declaration
66 // but maybe not needed to initialize
67 FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
73 bytesRead = ps->read(buffer, 1, DB_FILE_SIZE_BLOCK, fp);
82 * Gets the Secure Virtual Database from the Persistent Storage
84 * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
85 * @param data - pointer of the returned Secure Virtual Resource(s)
86 * @param size - pointer of the returned size of Secure Virtual Resource(s)
88 * @return OCStackResult - result of getting Secure Virtual Resource(s)
90 OCStackResult GetSecureVirtualDatabaseFromPS(const char *rsrcName, uint8_t **data, size_t *size)
92 OIC_LOG(DEBUG, TAG, "GetSecureVirtualDatabaseFromPS IN");
93 if (!data || *data || !size)
95 return OC_STACK_INVALID_PARAM;
99 uint8_t *fsData = NULL;
101 OCStackResult ret = OC_STACK_ERROR;
103 OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
104 VERIFY_NON_NULL(TAG, ps, ERROR);
106 fileSize = GetSVRDatabaseSize(ps);
107 OIC_LOG_V(DEBUG, TAG, "File Read Size: %zu", fileSize);
110 fsData = (uint8_t *) OICCalloc(1, fileSize);
111 VERIFY_NON_NULL(TAG, fsData, ERROR);
113 fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
114 VERIFY_NON_NULL(TAG, fp, ERROR);
115 if (ps->read(fsData, 1, fileSize, fp) == fileSize)
119 CborParser parser; // will be initialized in |cbor_parser_init|
120 CborValue cbor; // will be initialized in |cbor_parser_init|
121 cbor_parser_init(fsData, fileSize, 0, &parser, &cbor);
122 CborValue cborValue = {0};
123 CborError cborFindResult = cbor_value_map_find_value(&cbor, rsrcName, &cborValue);
124 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&cborValue))
126 cborFindResult = cbor_value_dup_byte_string(&cborValue, data, size, NULL);
127 VERIFY_SUCCESS(TAG, CborNoError==cborFindResult, ERROR);
130 // in case of |else (...)|, svr_data not found
132 // return everything in case rsrcName is NULL
136 *data = (uint8_t *) OICCalloc(1, fileSize);
137 VERIFY_NON_NULL(TAG, *data, ERROR);
138 memcpy(*data, fsData, fileSize);
143 OIC_LOG(DEBUG, TAG, "GetSecureVirtualDatabaseFromPS OUT");
155 * Updates the Secure Virtual Resource(s) into the Persistent Storage.
156 * This function stores cbor-payload of each resource by appending resource name,
157 * and empty payload implies deleting the value
159 * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
160 * @param psPayload - pointer of the updated Secure Virtual Resource(s)
161 * @param psSize - the updated size of Secure Virtual Resource(s)
163 * @return OCStackResult - result of updating Secure Virtual Resource(s)
165 OCStackResult UpdateSecureResourceInPS(const char *rsrcName, const uint8_t *psPayload, size_t psSize)
167 OIC_LOG(DEBUG, TAG, "UpdateSecureResourceInPS IN");
170 return OC_STACK_INVALID_PARAM;
175 uint8_t *dbData = NULL;
176 uint8_t *outPayload = NULL;
178 uint8_t *aclCbor = NULL;
179 uint8_t *pstatCbor = NULL;
180 uint8_t *doxmCbor = NULL;
181 uint8_t *amaclCbor = NULL;
182 uint8_t *svcCbor = NULL;
183 uint8_t *credCbor = NULL;
184 uint8_t *pconfCbor = NULL;
185 uint8_t *resetPfCbor = NULL;
186 uint8_t *crlCbor = NULL;
188 int64_t cborEncoderResult = CborNoError;
189 OCStackResult ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
190 if (dbData && dbSize)
192 size_t aclCborLen = 0;
193 size_t pstatCborLen = 0;
194 size_t doxmCborLen = 0;
195 size_t amaclCborLen = 0;
196 size_t svcCborLen = 0;
197 size_t credCborLen = 0;
198 size_t pconfCborLen = 0;
199 size_t resetPfCborLen = 0;
200 size_t crlCborLen = 0;
202 // Gets each secure virtual resource from persistent storage
203 // this local scoping intended, for destroying large cbor instances after use
205 CborParser parser; // will be initialized in |cbor_parser_init|
206 CborValue cbor; // will be initialized in |cbor_parser_init|
207 cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
208 CborValue curVal = {0};
209 CborError cborFindResult = CborNoError;
211 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal);
212 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
214 cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL);
215 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
217 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal);
218 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
220 cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL);
221 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
223 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal);
224 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
226 cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL);
227 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DOXM Name Value.");
229 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_AMACL_NAME, &curVal);
230 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
232 cborFindResult = cbor_value_dup_byte_string(&curVal, &amaclCbor, &amaclCborLen, NULL);
233 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding AMACL Name Value.");
235 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_SVC_NAME, &curVal);
236 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
238 cborFindResult = cbor_value_dup_byte_string(&curVal, &svcCbor, &svcCborLen, NULL);
239 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SVC Name Value.");
241 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRED_NAME, &curVal);
242 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
244 cborFindResult = cbor_value_dup_byte_string(&curVal, &credCbor, &credCborLen, NULL);
245 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CRED Name Value.");
247 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PCONF_NAME, &curVal);
248 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
250 cborFindResult = cbor_value_dup_byte_string(&curVal, &pconfCbor, &pconfCborLen, NULL);
251 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PCONF Name Value.");
253 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_RESET_PF_NAME, &curVal);
254 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
256 cborFindResult = cbor_value_dup_byte_string(&curVal, &resetPfCbor, &resetPfCborLen, NULL);
257 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Reset Profile Name Value.");
259 int64_t cborFindCrlResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRL_NAME, &curVal);
260 if (CborNoError == cborFindCrlResult && cbor_value_is_byte_string(&curVal))
262 cborFindCrlResult = cbor_value_dup_byte_string(&curVal, &crlCbor, &crlCborLen, NULL);
263 if (CborNoError != cborFindCrlResult && CborErrorOutOfMemory != cborFindCrlResult)
265 OIC_LOG(ERROR, TAG, "Failed Finding optional CRL Name Value.");
269 OIC_LOG(INFO, TAG, "Successfully Finding optional CRL Name Value.");
274 // Updates the added |psPayload| with the existing secure virtual resource(s)
275 // this local scoping intended, for destroying large cbor instances after use
277 size_t size = aclCborLen + pstatCborLen + doxmCborLen + amaclCborLen
278 + svcCborLen + credCborLen + pconfCborLen + resetPfCborLen + crlCborLen
280 // This added '255' is arbitrary value that is added to cover the name of the resource, map addition and ending
282 outPayload = (uint8_t *) OICCalloc(1, size);
283 VERIFY_NON_NULL(TAG, outPayload, ERROR);
284 CborEncoder encoder; // will be initialized in |cbor_parser_init|
285 cbor_encoder_init(&encoder, outPayload, size, 0);
286 CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map|
287 cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
288 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map.");
290 if (psPayload && psSize)
292 cborEncoderResult |= cbor_encode_text_string(&secRsrc, rsrcName, strlen(rsrcName));
293 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag");
294 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, psPayload, psSize);
295 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value.");
297 if (strcmp(OIC_JSON_ACL_NAME, rsrcName) && aclCborLen)
299 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
300 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
301 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, aclCbor, aclCborLen);
302 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
304 if (strcmp(OIC_JSON_PSTAT_NAME, rsrcName) && pstatCborLen)
306 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
307 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
308 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pstatCbor, pstatCborLen);
309 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
311 if (strcmp(OIC_JSON_DOXM_NAME, rsrcName) && doxmCborLen)
313 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
314 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Name.");
315 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, doxmCbor, doxmCborLen);
316 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Value.");
318 if (strcmp(OIC_JSON_AMACL_NAME, rsrcName) && amaclCborLen)
320 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
321 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Name.");
322 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, amaclCbor, amaclCborLen);
323 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Value.");
325 if (strcmp(OIC_JSON_SVC_NAME, rsrcName) && svcCborLen)
327 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
328 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
329 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, svcCbor, svcCborLen);
330 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
332 if (strcmp(OIC_JSON_CRED_NAME, rsrcName) && credCborLen)
334 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
335 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Name.");
336 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, credCbor, credCborLen);
337 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Value.");
339 if (strcmp(OIC_JSON_PCONF_NAME, rsrcName) && pconfCborLen)
341 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PCONF_NAME, strlen(OIC_JSON_PCONF_NAME));
342 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Name.");
343 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pconfCbor, pconfCborLen);
344 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Value.");
346 if (strcmp(OIC_JSON_RESET_PF_NAME, rsrcName) && resetPfCborLen)
348 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_RESET_PF_NAME, strlen(OIC_JSON_RESET_PF_NAME));
349 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Reset Profile Name.");
350 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, resetPfCbor, resetPfCborLen);
351 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Reset Profile Value.");
353 if (strcmp(OIC_JSON_CRL_NAME, rsrcName) && crlCborLen)
355 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_CRL_NAME, strlen(OIC_JSON_CRL_NAME));
356 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Crl Name.");
357 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, crlCbor, crlCborLen);
358 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Crl Value.");
361 cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
362 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
363 outSize = encoder.ptr - outPayload;
366 else if (psPayload && psSize)
368 size_t size = psSize + 255;
369 // This added '255' is arbitrary value that is added to cover the name of the resource, map addition and ending
371 outPayload = (uint8_t *) OICCalloc(1, size);
372 VERIFY_NON_NULL(TAG, outPayload, ERROR);
373 CborEncoder encoder; // will be initialized in |cbor_parser_init|
374 cbor_encoder_init(&encoder, outPayload, size, 0);
375 CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map|
376 cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
377 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map.");
379 cborEncoderResult |= cbor_encode_text_string(&secRsrc, rsrcName, strlen(rsrcName));
380 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag");
381 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, psPayload, psSize);
382 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value.");
384 cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
385 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
386 outSize = encoder.ptr - outPayload;
389 if (outPayload && outSize)
391 OIC_LOG_V(DEBUG, TAG, "Writing in the file: %zu", outSize);
392 OCPersistentStorage* ps = SRMGetPersistentStorageHandler();
395 FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "wb");
398 size_t numberItems = ps->write(outPayload, 1, outSize, fp);
399 if (outSize == numberItems)
401 OIC_LOG_V(DEBUG, TAG, "Written %zu bytes into SVR database file", outSize);
406 OIC_LOG_V(ERROR, TAG, "Failed writing %zu in the database", numberItems);
412 OIC_LOG(ERROR, TAG, "File open failed.");
417 OIC_LOG(DEBUG, TAG, "UpdateSecureResourceInPS OUT");
429 OICFree(resetPfCbor);
435 * Resets the Secure Virtual Resource(s).
436 * This function reads the Reset Profile
437 * and resets the secure virtual resources accordingly.
439 * @return OCStackResult - result of updating Secure Virtual Resource(s)
441 OCStackResult ResetSecureResourceInPS(void)
443 OIC_LOG(DEBUG, TAG, "ResetSecureResourceInPS IN");
447 uint8_t *dbData = NULL;
448 uint8_t *outPayload = NULL;
450 uint8_t *aclCbor = NULL;
451 uint8_t *pstatCbor = NULL;
452 uint8_t *doxmCbor = NULL;
453 uint8_t *resetPfCbor = NULL;
455 int64_t cborEncoderResult = CborNoError;
456 OCStackResult ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
460 size_t aclCborLen = 0;
461 size_t pstatCborLen = 0;
462 size_t doxmCborLen = 0;
463 size_t resetPfCborLen = 0;
465 // Gets the reset profile from persistent storage
467 CborParser parser; // will be initialized in |cbor_parser_init|
468 CborValue cbor; // will be initialized in |cbor_parser_init|
469 cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
470 CborValue curVal = {0};
471 CborError cborFindResult = CborNoError;
472 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_RESET_PF_NAME, &curVal);
473 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
475 cborFindResult = cbor_value_dup_byte_string(&curVal, &resetPfCbor, &resetPfCborLen, NULL);
476 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Reset Profile Name Value.");
480 // Gets each secure virtual resource from the reset profile
482 CborParser parser; // will be initialized in |cbor_parser_init|
483 CborValue cbor; // will be initialized in |cbor_parser_init|
484 cbor_parser_init(resetPfCbor, resetPfCborLen, 0, &parser, &cbor);
485 CborValue curVal = {0};
486 CborError cborFindResult = CborNoError;
487 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal);
488 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
490 cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL);
491 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
493 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal);
494 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
496 cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL);
497 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
499 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal);
500 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
502 cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL);
503 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DOXM Name Value.");
508 size_t size = aclCborLen + pstatCborLen + doxmCborLen + resetPfCborLen + 255;
509 // This added '255' is arbitrary value added to cover the name of the resource, map addition, and ending
511 outPayload = (uint8_t *) OICCalloc(1, size);
512 VERIFY_NON_NULL(TAG, outPayload, ERROR);
514 cbor_encoder_init(&encoder, outPayload, size, 0);
516 cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
518 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
519 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
520 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, aclCbor, aclCborLen);
521 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
523 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
524 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
525 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pstatCbor, pstatCborLen);
526 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
528 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
529 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name.");
530 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, doxmCbor, doxmCborLen);
531 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value.");
533 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_RESET_PF_NAME, strlen(OIC_JSON_RESET_PF_NAME));
534 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Reset Profile Name.");
535 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, resetPfCbor, resetPfCborLen);
536 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Reset Profile Value.");
538 cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
539 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
540 outSize = encoder.ptr - outPayload;
543 if (outPayload && outSize)
545 OIC_LOG_V(DEBUG, TAG, "Writing in the file: %zu", outSize);
546 OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
549 FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "wb");
552 size_t numberItems = ps->write(outPayload, 1, outSize, fp);
553 if (outSize == numberItems)
555 OIC_LOG_V(DEBUG, TAG, "Written %zu bytes into SVR database file", outSize);
560 OIC_LOG_V(ERROR, TAG, "Failed writing %zu in the database", numberItems);
566 OIC_LOG(ERROR, TAG, "File open failed.");
573 SRMDeInitSecureResources();
574 InitSecureResources();
575 OIC_LOG(DEBUG, TAG, "ResetSecureResourceINPS OUT");
583 OICFree(resetPfCbor);
588 * Creates Reset Profile from the initial secure virtual resources.
589 * This function copies the secure resources
590 * and creates the Reset Profile in the Persistent Storage.
591 * Device ID in doxm and pstat are left empty as it will be renewed after reset.
593 * @return OCStackResult - result of updating Secure Virtual Resource(s)
595 OCStackResult CreateResetProfile(void)
597 OIC_LOG(DEBUG, TAG, "CreateResetProfile IN");
600 uint8_t *dbData = NULL;
602 uint8_t *aclCbor = NULL;
603 uint8_t *pstatCbor = NULL;
604 uint8_t *doxmCbor = NULL;
605 uint8_t *resetPfCbor = NULL;
607 OCStackResult ret = OC_STACK_ERROR;
608 int64_t cborEncoderResult = CborNoError;
609 ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
610 if (dbData && dbSize)
612 size_t aclCborLen = 0;
613 size_t pstatCborLen = 0;
614 size_t doxmCborLen = 0;
615 size_t resetPfCborLen = 0;
620 cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
621 CborValue curVal = {0};
622 CborError cborFindResult = CborNoError;
623 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal);
624 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
626 cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL);
627 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
629 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal);
630 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
632 cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL);
633 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
635 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal);
636 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
638 cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL);
639 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DOXM Name Value.");
643 // Set the Device ID in doxm and pstat to empty
646 OicSecPstat_t *pstat = NULL;
647 ret = CBORPayloadToPstat(pstatCbor, pstatCborLen, &pstat);
652 OicUuid_t emptyUuid = {.id = {0} };
653 memcpy(&pstat->deviceID, &emptyUuid, sizeof(OicUuid_t));
654 memcpy(&pstat->rownerID, &emptyUuid, sizeof(OicUuid_t));
656 ret = PstatToCBORPayload(pstat, &pstatCbor, &pstatCborLen, false);
661 OicSecDoxm_t *doxm = NULL;
662 ret = CBORPayloadToDoxm(doxmCbor, doxmCborLen, &doxm);
667 OicUuid_t emptyUuid = {.id = {0} };
668 memcpy(&doxm->deviceID, &emptyUuid, sizeof(OicUuid_t));
669 memcpy(&doxm->rownerID, &emptyUuid, sizeof(OicUuid_t));
671 ret = DoxmToCBORPayload(doxm, &doxmCbor, &doxmCborLen, false);
676 size_t size = aclCborLen + pstatCborLen + doxmCborLen + 255;
677 resetPfCbor = (uint8_t *) OICCalloc(1, size);
678 VERIFY_NON_NULL(TAG, resetPfCbor, ERROR);
679 CborEncoder encoder; // will be initialized in |cbor_parser_init|
680 cbor_encoder_init(&encoder, resetPfCbor, size, 0);
681 CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map|
682 cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
684 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
685 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
686 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, aclCbor, aclCborLen);
687 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
689 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
690 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
691 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pstatCbor, pstatCborLen);
692 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
694 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
695 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Name.");
696 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, doxmCbor, doxmCborLen);
697 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Value.");
699 cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
700 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
701 resetPfCborLen = encoder.ptr - resetPfCbor;
704 UpdateSecureResourceInPS(OIC_JSON_RESET_PF_NAME, resetPfCbor, resetPfCborLen);
707 OIC_LOG(DEBUG, TAG, "CreateResetProfile OUT");
714 OICFree(resetPfCbor);