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"
42 #include "psinterface.h"
43 #include "doxmresource.h"
45 #include "aclresource.h"
46 #include "credresource.h"
48 #define TAG "OIC_SRM_PSI"
50 // SVR database buffer block size
52 #define DB_FILE_SIZE_BLOCK 1023
54 const size_t DB_FILE_SIZE_BLOCK = 1023;
56 //the Secure Virtual Database file size
57 static size_t g_svrDbFileSize = 0;
58 //mutex for the Secure Virtual Database
59 static oc_mutex g_mutexDb = NULL;
61 // Persistent Storage status
62 static PSStatus_t g_psStatus = PS_NO_EXTERNAL_DB_SET;
64 //SVR DB Validation status
65 static SVRDBValidStatus_t g_svrdbValidStatus = SVRDB_NOT_CHECKED;
67 static resetSVRDBCB_t g_resetSVRDBCB = {0};
69 resetSVRDBCB_t* GetResetSVRDBCB(void)
71 return &g_resetSVRDBCB;
75 * Set status of SVR DB after call CheckSVRDBValidity() API
77 static void SetSVRDBValidStatus(SVRDBValidStatus_t status)
79 g_svrdbValidStatus = status;
83 * Update the Persistent Storage Database size.
85 void UpdateSizePSI(size_t size)
87 oc_mutex_lock(g_mutexDb);
88 g_svrDbFileSize = size;
89 oc_mutex_unlock(g_mutexDb);
92 * Init the Persistent Storage Database.
94 OCStackResult InitPersistentStorageInterface(void)
98 g_mutexDb = oc_mutex_new();
101 return OC_STACK_ERROR;
111 * DeInit the Persistent Storage Database.
113 void DeinitPersistentStorageInterface(void)
116 oc_mutex_free(g_mutexDb);
121 * Gets the database size
123 * @param ps is a pointer to OCPersistentStorage for the Virtual Resource(s).
125 * @return size_t - total size of the database
127 static size_t GetPSIDatabaseSizeWithoutCaching(const OCPersistentStorage *ps)
134 char buffer[DB_FILE_SIZE_BLOCK]; // can not initialize with declaration
135 // but maybe not needed to initialize
136 FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
139 size_t bytesRead = 0;
142 bytesRead = ps->read(buffer, 1, DB_FILE_SIZE_BLOCK, fp);
152 * Gets the the Persistent Storage Database size
154 * @param ps - pointer of OCPersistentStorage for the Secure Virtual Resource(s)
156 * @return size_t - total size of the SVR database
158 static size_t GetPSIDatabaseSize(const OCPersistentStorage *ps)
160 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
167 if (!g_svrDbFileSize)
170 char buffer[DB_FILE_SIZE_BLOCK]; // can not initialize with declaration
171 // but maybe not needed to initialize
172 FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
175 size_t bytesRead = 0;
178 bytesRead = ps->read(buffer, 1, DB_FILE_SIZE_BLOCK, fp);
185 OIC_LOG_V(ERROR, TAG, "%s: File open failed.", __func__);
192 OIC_LOG_V(INFO, TAG, "%s get size from cache", __func__);
195 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
197 return g_svrDbFileSize;
200 #ifdef __SECURE_PSI__
201 static OCStackResult getPlaintextFromDB(const OCPersistentStorage *ps, uint8_t **pt,
204 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
205 OCStackResult ret = OC_STACK_ERROR;
207 uint8_t *plaintext = NULL;
210 fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
213 OIC_LOG(ERROR, TAG, "ps->open() Failed");
214 return OC_STACK_ERROR;
217 // Get fileSize of plaintext
218 char buffer[DB_FILE_SIZE_BLOCK];
219 size_t bytesRead = 0;
223 bytesRead = ps->read(buffer, 1, DB_FILE_SIZE_BLOCK, fp);
224 fileSize += bytesRead;
228 ret = OC_STACK_NO_MEMORY;
229 plaintext = (uint8_t*)OICCalloc(1, fileSize);
230 VERIFY_NON_NULL(TAG, plaintext, ERROR);
233 fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
237 OIC_LOG(ERROR, TAG, "ps->open() Failed");
238 return OC_STACK_ERROR;
241 if (fileSize != ps->read(plaintext, 1, fileSize, fp))
243 OIC_LOG_V(ERROR, TAG, "ps->read() Failed");
244 ret = OC_STACK_ERROR;
252 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
264 static OCStackResult OTEncrypt(const OCPersistentStorage *psForPlain,
265 const OCPersistentStorage *psForEncrypted)
267 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
269 uint8_t *plaintext = NULL;
271 OCStackResult ret = getPlaintextFromDB(psForPlain, &plaintext, &pt_len);
272 if (OC_STACK_OK != ret)
274 OIC_LOG(ERROR, TAG, "getPlaintextFromDB() Failed");
279 uint8_t *ciphertext = NULL;
281 if (0 != psForEncrypted->encrypt(plaintext, pt_len, &ciphertext, &ct_len))
283 OIC_LOG(ERROR, TAG, "psForEncrypted->encrypt() Failed");
285 return OC_STACK_ERROR;
290 FILE *fp2 = psForEncrypted->open(SVR_DB_DAT_FILE_NAME, "wb");
293 OIC_LOG(ERROR, TAG, "psForEncrypted->open() Failed");
295 return OC_STACK_ERROR;
298 if (ct_len != psForEncrypted->write(ciphertext, 1, ct_len, fp2))
300 OIC_LOG(ERROR, TAG, "psForEncrypted->write() Failed");
302 return OC_STACK_ERROR;
304 psForEncrypted->close(fp2);
307 if (psForPlain->unlink)
309 psForPlain->unlink(SVR_DB_DAT_FILE_NAME);
313 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
318 OCStackResult setSecurePSI(const unsigned char *key, const OCPersistentStorage *psPlain,
319 const OCPersistentStorage *psEnc, const OCPersistentStorage *psRescue)
321 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
325 OIC_LOG_V(ERROR, TAG, "%s: %s is NULL", __func__, !key ? "key" : "psEnc");
326 return OC_STACK_INVALID_PARAM;
328 if (!(psEnc->encrypt && psEnc->decrypt))
330 OIC_LOG(ERROR, TAG, "psEnc->encrypt && psEnc->decrypt should be set");
331 return OC_STACK_INVALID_PARAM;
333 if (psPlain && !(psPlain->open && psPlain->read && psPlain->close
336 OIC_LOG(ERROR, TAG, "open/read/close/unlink funcion for plain should be set");
337 return OC_STACK_INVALID_PARAM;
339 if (psRescue && !(psRescue->open && psRescue->read && psRescue->close))
341 OIC_LOG(ERROR, TAG, "open/read/close funcion for rescue should be set");
342 return OC_STACK_INVALID_PARAM;
346 ret = psiSetKey(key);
347 if (OC_STACK_OK != ret)
349 OIC_LOG(ERROR, TAG, "psiSetKey() Failed");
352 OIC_LOG(DEBUG, TAG, "key is set");
354 // if there is new plain db
356 if (psPlain && (fp = psPlain->open(SVR_DB_DAT_FILE_NAME, "rb")))
361 ret = OTEncrypt(psPlain, psEnc);
362 if (OC_STACK_OK != ret)
364 OIC_LOG(ERROR, TAG, "OTEncrypt() Failed");
372 ret = CheckPersistentStorage((OCPersistentStorage*)psEnc);
373 if (OC_STACK_OK != ret)
375 fp = psRescue->open(SVR_DB_DAT_FILE_NAME, "rb");
378 OIC_LOG(ERROR, TAG, "psRescue->open() Failed");
379 return OC_STACK_ERROR;
384 ret = OTEncrypt(psRescue, psEnc);
385 if (OC_STACK_OK != ret)
387 OIC_LOG_V(ERROR, TAG, "ps is currupted but NOT rescued(%d)", ret);
390 OIC_LOG(INFO, TAG, "ps is currupted and rescued");
394 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
399 #endif // __SECURE_PSI__
402 * Write the Persistent Storage
404 * @param payload - pointer of payload
405 * @param psize - size of payload
407 * @return OCStackResult - OC_STACK_OK sucsess, other - OC_STACK_ERROR
409 OCStackResult WritePSIDatabase(const uint8_t *payload, size_t size)
411 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
413 if (!payload || !size || !g_mutexDb)
415 OIC_LOG_V(ERROR, TAG, "%s: %s is NULL",
416 __func__, !payload ? "payload" : !size ? "size" : "mutex");
417 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
418 return OC_STACK_INVALID_PARAM;
421 OCPersistentStorage *ps = NULL;
423 OCStackResult ret = OC_STACK_SVR_DB_NOT_EXIST;
425 ps = SRMGetPersistentStorageHandler();
426 VERIFY_NON_NULL(TAG, ps, ERROR);
428 #ifdef __SECURE_PSI__
429 if (psiIsKeySet() && ps->encrypt && ps->decrypt)
431 OIC_LOG(DEBUG, TAG, "ps->encrypt !");
433 uint8_t *ciphertext = NULL;
436 if (0 != ps->encrypt(payload, size, &ciphertext, &ct_len))
438 OIC_LOG(ERROR, TAG, "ps->encrypt() Failed");
439 ret = OC_STACK_ERROR;
443 payload = ciphertext;
446 #endif // __SECURE_PSI__
448 OIC_LOG_V(INFO, TAG, "Writing in the file: %zu", size);
450 fp = ps->open(SVR_DB_DAT_FILE_NAME, "wb");
451 VERIFY_NON_NULL(TAG, fp, ERROR);
453 oc_mutex_lock(g_mutexDb);
454 g_svrDbFileSize = ps->write(payload, 1, size, fp);
456 oc_mutex_unlock(g_mutexDb);
458 #ifdef __SECURE_PSI__
459 if (psiIsKeySet() && ps->encrypt && ps->decrypt)
461 OICFree((uint8_t*)payload);
463 #endif // __SECURE_PSI__
464 if (size == g_svrDbFileSize)
466 OIC_LOG_V(INFO, TAG, "Written %zu bytes into SVR database file", size);
471 OIC_LOG_V(ERROR, TAG, "Failed writing %zu in the database", g_svrDbFileSize);
475 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
480 * Gets the Secure Virtual Database from the Persistent Storage
482 * @param ps - Persistent Storage handler
483 * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
484 * @param data - pointer of the returned Secure Virtual Resource(s)
485 * @param size - pointer of the returned size of Secure Virtual Resource(s)
487 * @return OCStackResult - result of getting Secure Virtual Resource(s)
489 OCStackResult GetSecureVirtualDatabaseFromPS2(OCPersistentStorage* ps, const char *rsrcName, uint8_t **data, size_t *size)
491 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
493 if (!data || *data || !size || !ps)
495 OIC_LOG_V(ERROR, TAG, "%s: %s is NULL",
496 __func__, !data || *data ? "data" : !size ? "size" : "ps");
497 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
498 return OC_STACK_INVALID_PARAM;
502 uint8_t *fsData = NULL;
504 OCStackResult ret = OC_STACK_ERROR;
506 fileSize = GetPSIDatabaseSizeWithoutCaching(ps);
507 OIC_LOG_V(INFO, TAG, "File Read Size: %zu", fileSize);
510 fsData = (uint8_t *) OICCalloc(1, fileSize + 1);
511 VERIFY_NON_NULL(TAG, fsData, ERROR);
513 fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
514 VERIFY_NON_NULL(TAG, fp, ERROR);
515 if (ps->read(fsData, 1, fileSize, fp) == fileSize)
517 #ifdef __SECURE_PSI__
518 if (psiIsKeySet() && ps->encrypt && ps->decrypt)
520 OIC_LOG(DEBUG, TAG, "ps->decrypt !");
522 unsigned char *plainData = NULL;
523 size_t plainSize = 0;
525 if (0 != ps->decrypt(fsData, fileSize, &plainData, &plainSize))
527 OIC_LOG(ERROR, TAG, "ps->decrypt() Failed");
528 ret = OC_STACK_ERROR;
533 fileSize = plainSize;
535 #endif // __SECURE_PSI__
538 CborParser parser; // will be initialized in |cbor_parser_init|
539 CborValue cbor; // will be initialized in |cbor_parser_init|
540 cbor_parser_init(fsData, fileSize, 0, &parser, &cbor);
541 CborValue cborValue = {0};
542 CborError cborFindResult = cbor_value_map_find_value(&cbor, rsrcName, &cborValue);
543 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&cborValue))
545 cborFindResult = cbor_value_dup_byte_string(&cborValue, data, size, NULL);
546 VERIFY_SUCCESS(TAG, CborNoError==cborFindResult, ERROR);
549 // in case of |else (...)|, svr_data not found
551 // return everything in case rsrcName is NULL
555 *data = (uint8_t *) OICCalloc(1, fileSize);
556 VERIFY_NON_NULL(TAG, *data, ERROR);
557 memcpy(*data, fsData, fileSize);
562 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
574 * Gets the Secure Virtual Database from the Persistent Storage
576 * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
577 * @param data - pointer of the returned Secure Virtual Resource(s)
578 * @param size - pointer of the returned size of Secure Virtual Resource(s)
580 * @return OCStackResult - result of getting Secure Virtual Resource(s)
582 OCStackResult GetSecureVirtualDatabaseFromPS(const char *rsrcName, uint8_t **data, size_t *size)
584 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
586 // Logs for Persistent Storage status
589 if (!data || *data || !size || !g_mutexDb)
591 OIC_LOG_V(ERROR, TAG, "%s: %s is NULL",
592 __func__, !data || *data ? "data" : !size ? "size" : "mutex");
593 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
594 return OC_STACK_INVALID_PARAM;
598 uint8_t *fsData = NULL;
600 OCStackResult ret = OC_STACK_ERROR;
602 OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
603 VERIFY_NON_NULL(TAG, ps, ERROR);
605 fileSize = GetPSIDatabaseSize(ps);
606 OIC_LOG_V(INFO, TAG, "File Read Size: %zu", fileSize);
609 fsData = (uint8_t *) OICCalloc(1, fileSize);
610 VERIFY_NON_NULL(TAG, fsData, ERROR);
612 fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
613 VERIFY_NON_NULL(TAG, fp, ERROR);
614 if (ps->read(fsData, 1, fileSize, fp) == fileSize)
616 #ifdef __SECURE_PSI__
617 if (psiIsKeySet() && ps->encrypt && ps->decrypt)
619 OIC_LOG(DEBUG, TAG, "ps->decrypt !");
621 unsigned char *plainData = NULL;
622 size_t plainSize = 0;
624 if (0 != ps->decrypt(fsData, fileSize, &plainData, &plainSize))
626 OIC_LOG(ERROR, TAG, "ps->decrypt() Failed");
627 ret = OC_STACK_ERROR;
632 fileSize = plainSize;
634 #endif // __SECURE_PSI__
637 CborParser parser; // will be initialized in |cbor_parser_init|
638 CborValue cbor; // will be initialized in |cbor_parser_init|
639 cbor_parser_init(fsData, fileSize, 0, &parser, &cbor);
640 CborValue cborValue = {0};
641 CborError cborFindResult = cbor_value_map_find_value(&cbor, rsrcName, &cborValue);
642 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&cborValue))
644 cborFindResult = cbor_value_dup_byte_string(&cborValue, data, size, NULL);
645 VERIFY_SUCCESS(TAG, CborNoError==cborFindResult, ERROR);
648 // in case of |else (...)|, svr_data not found
650 // return everything in case rsrcName is NULL
654 *data = (uint8_t *) OICCalloc(1, fileSize);
655 VERIFY_NON_NULL(TAG, *data, ERROR);
656 memcpy(*data, fsData, fileSize);
661 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
673 * Updates the Secure Virtual Resource(s) into the Persistent Storage.
674 * This function stores cbor-payload of each resource by appending resource name,
675 * and empty payload implies deleting the value
677 * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
678 * @param psPayload - pointer of the updated Secure Virtual Resource(s)
679 * @param psSize - the updated size of Secure Virtual Resource(s)
681 * @return OCStackResult - result of updating Secure Virtual Resource(s)
683 OCStackResult UpdateSecureResourceInPS(const char *rsrcName, const uint8_t *psPayload, size_t psSize)
685 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
689 return OC_STACK_INVALID_PARAM;
694 uint8_t *dbData = NULL;
695 uint8_t *outPayload = NULL;
697 uint8_t *aclCbor = NULL;
698 uint8_t *pstatCbor = NULL;
699 uint8_t *doxmCbor = NULL;
700 uint8_t *amaclCbor = NULL;
701 uint8_t *credCbor = NULL;
702 uint8_t *pconfCbor = NULL;
703 uint8_t *resetPfCbor = NULL;
704 uint8_t *crlCbor = NULL;
706 int64_t cborEncoderResult = CborNoError;
707 OCStackResult ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
708 if (OC_STACK_OK != ret)
710 OIC_LOG_V(ERROR, TAG, "GetSecureVirtualDatabaseFromPS() is failed(%d)", ret);
712 if (dbData && dbSize)
714 size_t aclCborLen = 0;
715 size_t pstatCborLen = 0;
716 size_t doxmCborLen = 0;
717 size_t amaclCborLen = 0;
718 size_t credCborLen = 0;
719 size_t pconfCborLen = 0;
720 size_t resetPfCborLen = 0;
721 size_t crlCborLen = 0;
723 ret = OC_STACK_ERROR;
725 // Gets each secure virtual resource from persistent storage
726 // this local scoping intended, for destroying large cbor instances after use
728 CborParser parser; // will be initialized in |cbor_parser_init|
729 CborValue cbor; // will be initialized in |cbor_parser_init|
730 cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
731 CborValue curVal = {0};
732 CborError cborFindResult = CborNoError;
734 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal);
735 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
737 cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL);
738 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
740 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal);
741 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
743 cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL);
744 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
746 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal);
747 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
749 cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL);
750 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DOXM Name Value.");
752 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_AMACL_NAME, &curVal);
753 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
755 cborFindResult = cbor_value_dup_byte_string(&curVal, &amaclCbor, &amaclCborLen, NULL);
756 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding AMACL Name Value.");
759 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRED_NAME, &curVal);
760 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
762 cborFindResult = cbor_value_dup_byte_string(&curVal, &credCbor, &credCborLen, NULL);
763 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CRED Name Value.");
765 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PCONF_NAME, &curVal);
766 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
768 cborFindResult = cbor_value_dup_byte_string(&curVal, &pconfCbor, &pconfCborLen, NULL);
769 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PCONF Name Value.");
771 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_RESET_PF_NAME, &curVal);
772 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
774 cborFindResult = cbor_value_dup_byte_string(&curVal, &resetPfCbor, &resetPfCborLen, NULL);
775 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Reset Profile Name Value.");
777 int64_t cborFindCrlResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRL_NAME, &curVal);
778 if (CborNoError == cborFindCrlResult && cbor_value_is_byte_string(&curVal))
780 cborFindCrlResult = cbor_value_dup_byte_string(&curVal, &crlCbor, &crlCborLen, NULL);
781 if (CborNoError != cborFindCrlResult && CborErrorOutOfMemory != cborFindCrlResult)
783 OIC_LOG(ERROR, TAG, "Failed Finding optional CRL Name Value.");
787 OIC_LOG(INFO, TAG, "Successfully Finding optional CRL Name Value.");
792 // Updates the added |psPayload| with the existing secure virtual resource(s)
793 // this local scoping intended, for destroying large cbor instances after use
795 size_t size = aclCborLen + pstatCborLen + doxmCborLen + amaclCborLen
796 + credCborLen + pconfCborLen + resetPfCborLen + crlCborLen
798 // This added '255' is arbitrary value that is added to cover the name of the resource, map addition and ending
800 outPayload = (uint8_t *) OICCalloc(1, size);
801 VERIFY_NON_NULL(TAG, outPayload, ERROR);
802 CborEncoder encoder; // will be initialized in |cbor_parser_init|
803 cbor_encoder_init(&encoder, outPayload, size, 0);
804 CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map|
805 cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
806 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map.");
808 if (psPayload && psSize)
810 cborEncoderResult |= cbor_encode_text_string(&secRsrc, rsrcName, strlen(rsrcName));
811 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag");
812 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, psPayload, psSize);
813 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value.");
815 if (strcmp(OIC_JSON_ACL_NAME, rsrcName) && aclCborLen)
817 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
818 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
819 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, aclCbor, aclCborLen);
820 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
822 if (strcmp(OIC_JSON_PSTAT_NAME, rsrcName) && pstatCborLen)
824 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
825 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
826 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pstatCbor, pstatCborLen);
827 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
829 if (strcmp(OIC_JSON_DOXM_NAME, rsrcName) && doxmCborLen)
831 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
832 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Name.");
833 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, doxmCbor, doxmCborLen);
834 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Value.");
836 if (strcmp(OIC_JSON_AMACL_NAME, rsrcName) && amaclCborLen)
838 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
839 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Name.");
840 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, amaclCbor, amaclCborLen);
841 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Value.");
843 if (strcmp(OIC_JSON_CRED_NAME, rsrcName) && credCborLen)
845 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
846 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Name.");
847 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, credCbor, credCborLen);
848 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Value.");
850 if (strcmp(OIC_JSON_PCONF_NAME, rsrcName) && pconfCborLen)
852 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PCONF_NAME, strlen(OIC_JSON_PCONF_NAME));
853 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Name.");
854 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pconfCbor, pconfCborLen);
855 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Value.");
857 if (strcmp(OIC_JSON_RESET_PF_NAME, rsrcName) && resetPfCborLen)
859 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_RESET_PF_NAME, strlen(OIC_JSON_RESET_PF_NAME));
860 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Reset Profile Name.");
861 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, resetPfCbor, resetPfCborLen);
862 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Reset Profile Value.");
864 if (strcmp(OIC_JSON_CRL_NAME, rsrcName) && crlCborLen)
866 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_CRL_NAME, strlen(OIC_JSON_CRL_NAME));
867 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Crl Name.");
868 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, crlCbor, crlCborLen);
869 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Crl Value.");
872 cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
873 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
874 outSize = cbor_encoder_get_buffer_size(&encoder, outPayload);
877 else if (psPayload && psSize)
879 size_t size = psSize + 255;
880 // This added '255' is arbitrary value that is added to cover the name of the resource, map addition and ending
882 outPayload = (uint8_t *) OICCalloc(1, size);
883 VERIFY_NON_NULL(TAG, outPayload, ERROR);
884 CborEncoder encoder; // will be initialized in |cbor_parser_init|
885 cbor_encoder_init(&encoder, outPayload, size, 0);
886 CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map|
887 cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
888 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map.");
890 cborEncoderResult |= cbor_encode_text_string(&secRsrc, rsrcName, strlen(rsrcName));
891 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag");
892 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, psPayload, psSize);
893 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value.");
895 cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
896 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
897 outSize = cbor_encoder_get_buffer_size(&encoder, outPayload);
900 ret = WritePSIDatabase(outPayload, outSize);
901 if (OC_STACK_OK != ret)
903 OIC_LOG_V(ERROR, TAG, "WritePSIDatabase() is failed(%d)", ret);
905 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
916 OICFree(resetPfCbor);
922 * Resets the Secure Virtual Resource(s).
923 * This function reads the Reset Profile
924 * and resets the secure virtual resources accordingly.
926 * @return OCStackResult - result of updating Secure Virtual Resource(s)
928 OCStackResult ResetSecureResourceInPS(void)
930 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
934 uint8_t *dbData = NULL;
935 uint8_t *outPayload = NULL;
937 uint8_t *aclCbor = NULL;
938 uint8_t *credCbor = NULL;
939 uint8_t *pstatCbor = NULL;
940 uint8_t *doxmCbor = NULL;
941 uint8_t *resetPfCbor = NULL;
943 int64_t cborEncoderResult = CborNoError;
944 OCStackResult ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
945 if (OC_STACK_OK != ret)
947 OIC_LOG_V(ERROR, TAG, "GetSecureVirtualDatabaseFromPS() is failed(%d)", ret);
951 size_t aclCborLen = 0;
952 size_t credCborLen = 0;
953 size_t pstatCborLen = 0;
954 size_t doxmCborLen = 0;
955 size_t resetPfCborLen = 0;
957 ret = OC_STACK_ERROR;
959 // Gets the reset profile from persistent storage
961 CborParser parser; // will be initialized in |cbor_parser_init|
962 CborValue cbor; // will be initialized in |cbor_parser_init|
963 cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
964 CborValue curVal = {0};
965 CborError cborFindResult = CborNoError;
966 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_RESET_PF_NAME, &curVal);
967 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
969 cborFindResult = cbor_value_dup_byte_string(&curVal, &resetPfCbor, &resetPfCborLen, NULL);
970 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding Reset Profile Name Value.");
972 else if (CborNoError == cborFindResult && CborInvalidType == curVal.type)
974 OIC_LOG(ERROR, TAG, "resetpf is not found");
979 OIC_LOG_V(ERROR, TAG, "cbor_value_map_find_value() Failed(%d)",
985 // Gets each secure virtual resource from the reset profile
987 CborParser parser; // will be initialized in |cbor_parser_init|
988 CborValue cbor; // will be initialized in |cbor_parser_init|
989 cbor_parser_init(resetPfCbor, resetPfCborLen, 0, &parser, &cbor);
990 CborValue curVal = {0};
991 CborError cborFindResult = CborNoError;
992 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal);
993 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
995 cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL);
996 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
998 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRED_NAME, &curVal);
999 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
1001 cborFindResult = cbor_value_dup_byte_string(&curVal, &credCbor, &credCborLen, NULL);
1002 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
1004 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal);
1005 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
1007 cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL);
1008 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
1010 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal);
1011 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
1013 cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL);
1014 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DOXM Name Value.");
1019 size_t size = aclCborLen + credCborLen + pstatCborLen + doxmCborLen + resetPfCborLen + 255;
1020 // This added '255' is arbitrary value added to cover the name of the resource, map addition, and ending
1022 outPayload = (uint8_t *) OICCalloc(1, size);
1023 VERIFY_NON_NULL(TAG, outPayload, ERROR);
1024 CborEncoder encoder;
1025 cbor_encoder_init(&encoder, outPayload, size, 0);
1026 CborEncoder secRsrc;
1027 cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
1029 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
1030 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
1031 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, aclCbor, aclCborLen);
1032 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
1036 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
1037 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
1038 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, credCbor, credCborLen);
1039 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
1042 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
1043 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
1044 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pstatCbor, pstatCborLen);
1045 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
1047 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
1048 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Name.");
1049 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, doxmCbor, doxmCborLen);
1050 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding DOXM Value.");
1052 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_RESET_PF_NAME, strlen(OIC_JSON_RESET_PF_NAME));
1053 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Reset Profile Name.");
1054 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, resetPfCbor, resetPfCborLen);
1055 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Reset Profile Value.");
1057 cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
1058 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
1059 outSize = cbor_encoder_get_buffer_size(&encoder, outPayload);
1062 ret = WritePSIDatabase(outPayload, outSize);
1063 if (OC_STACK_OK != ret)
1065 OIC_LOG_V(ERROR, TAG, "WritePSIDatabase() is failed(%d)", ret);
1069 SRMDeInitSecureResources();
1070 InitSecureResources();
1071 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
1075 OICFree(outPayload);
1080 OICFree(resetPfCbor);
1082 if (NULL != g_resetSVRDBCB.callback)
1084 g_resetSVRDBCB.callback(ret);
1088 OIC_LOG_V(DEBUG, TAG, "%s - Notifier resetSVRDB callback isn't registered.", __func__);
1095 * Creates Reset Profile from the initial secure virtual resources.
1096 * This function copies the secure resources
1097 * and creates the Reset Profile in the Persistent Storage.
1098 * Device ID in doxm and pstat remains as same after reset.
1100 * @return OCStackResult - result of updating Secure Virtual Resource(s)
1102 OCStackResult CreateResetProfile(void)
1104 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
1107 uint8_t *dbData = NULL;
1109 uint8_t *aclCbor = NULL;
1110 uint8_t *credCbor = NULL;
1111 uint8_t *pstatCbor = NULL;
1112 uint8_t *doxmCbor = NULL;
1113 uint8_t *resetPfCbor = NULL;
1115 OCStackResult ret = OC_STACK_ERROR;
1116 int64_t cborEncoderResult = CborNoError;
1117 ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
1118 if (OC_STACK_OK != ret)
1120 OIC_LOG_V(ERROR, TAG, "GetSecureVirtualDatabaseFromPS() is failed(%d)", ret);
1122 if (dbData && dbSize)
1124 size_t aclCborLen = 0;
1125 size_t credCborLen = 0;
1126 size_t pstatCborLen = 0;
1127 size_t doxmCborLen = 0;
1128 size_t resetPfCborLen = 0;
1130 ret = OC_STACK_ERROR;
1134 cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
1135 CborValue curVal = {0};
1136 CborError cborFindResult = CborNoError;
1138 // abort if reset profile exists
1139 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_RESET_PF_NAME, &curVal);
1140 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
1142 OIC_LOG(DEBUG, TAG, "Reset Profile already exists!!");
1147 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal);
1148 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
1150 cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL);
1151 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
1153 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRED_NAME, &curVal);
1154 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
1156 cborFindResult = cbor_value_dup_byte_string(&curVal, &credCbor, &credCborLen, NULL);
1157 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CRED Name Value.");
1159 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal);
1160 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
1162 cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL);
1163 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
1165 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal);
1166 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
1168 cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL);
1169 VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DOXM Name Value.");
1174 size_t size = aclCborLen + credCborLen + pstatCborLen + doxmCborLen + 255;
1175 resetPfCbor = (uint8_t *) OICCalloc(1, size);
1176 VERIFY_NON_NULL(TAG, resetPfCbor, ERROR);
1177 CborEncoder encoder; // will be initialized in |cbor_parser_init|
1178 cbor_encoder_init(&encoder, resetPfCbor, size, 0);
1179 CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map|
1180 cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
1182 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
1183 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
1184 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, aclCbor, aclCborLen);
1185 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
1189 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
1190 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Name.");
1191 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, credCbor, credCborLen);
1192 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding CRED Value.");
1195 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
1196 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
1197 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pstatCbor, pstatCborLen);
1198 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
1200 cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
1201 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Name.");
1202 cborEncoderResult |= cbor_encode_byte_string(&secRsrc, doxmCbor, doxmCborLen);
1203 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Value.");
1205 cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
1206 VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
1207 resetPfCborLen = cbor_encoder_get_buffer_size(&encoder, resetPfCbor);
1210 ret = UpdateSecureResourceInPS(OIC_JSON_RESET_PF_NAME, resetPfCbor, resetPfCborLen);
1211 if (OC_STACK_OK != ret)
1213 OIC_LOG(ERROR, TAG, "Error in UpdateSecureResourceInPS");
1217 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
1225 OICFree(resetPfCbor);
1229 void SetPSStatus(PSStatus_t status)
1231 g_psStatus = status;
1234 SVRDBValidStatus_t GetSVRDBValidStatus()
1236 return g_svrdbValidStatus;
1239 void PrintPSStatus(void)
1244 OIC_LOG(INFO, TAG, "PS Status: Normal - using external DB");
1247 OIC_LOG(INFO, TAG, "PS Status: Failed to open external DB! - using default DB");
1250 OIC_LOG(INFO, TAG, "PS Status: Failed to CBOR parse external DB! - using default DB");
1253 OIC_LOG(INFO, TAG, "PS Status: No external DB set - using internal memory");
1258 OCStackResult CheckSVRDBValidity(void)
1260 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
1262 OCStackResult res = OC_STACK_ERROR;
1263 OicUuid_t emptyUuid = OC_DEFAULT_OICUUID;
1264 OicUuid_t devOwnerUuid = OC_DEFAULT_OICUUID;
1265 OicUuid_t devRownerUuid = OC_DEFAULT_OICUUID;
1266 OicUuid_t resRowneruuid = OC_DEFAULT_OICUUID;
1267 bool isDevOwnerUuidEmpty = false;
1268 bool isDevRownerUuidEmpty = false;
1269 bool isPstatRownerUuidEmpty = false;
1270 bool isAclRownerUuidEmpty = false;
1271 bool isCredRownerUuidEmpty = false;
1272 OicSecDpm_t cm = NORMAL;
1274 bool isOwned = false;
1275 res = GetDoxmIsOwned(&isOwned);
1277 if( OC_STACK_OK != res)
1279 OIC_LOG_V(ERROR, TAG, "%s - doxm is NULL!", __func__);
1283 if(OC_STACK_OK != GetDoxmDevOwnerId(&devOwnerUuid) ||
1284 OC_STACK_OK != GetDoxmRownerId(&devRownerUuid))
1286 res = OC_STACK_ERROR;
1290 if (0 == memcmp(&devOwnerUuid, &emptyUuid, sizeof(devOwnerUuid)))
1292 isDevOwnerUuidEmpty = true;
1296 isDevOwnerUuidEmpty = false;
1299 if (0 == memcmp(&devRownerUuid, &emptyUuid, sizeof(devRownerUuid)))
1301 isDevRownerUuidEmpty = true;
1305 isDevRownerUuidEmpty = false;
1308 if (OC_STACK_OK != GetPstatRownerId(&resRowneruuid))
1310 res = OC_STACK_ERROR;
1314 if (0 == memcmp(&resRowneruuid, &emptyUuid, sizeof(resRowneruuid)))
1316 isPstatRownerUuidEmpty = true;
1320 isPstatRownerUuidEmpty = false;
1323 VERIFY_SUCCESS(TAG, OC_STACK_OK == GetPstatCm(&cm), ERROR);
1325 if (OC_STACK_OK != GetAclRownerId(&resRowneruuid))
1327 res = OC_STACK_ERROR;
1331 if (0 == memcmp(&resRowneruuid, &emptyUuid, sizeof(resRowneruuid)))
1333 isAclRownerUuidEmpty = true;
1337 isAclRownerUuidEmpty = false;
1340 if (OC_STACK_OK != GetCredRownerId(&resRowneruuid))
1342 res = OC_STACK_ERROR;
1346 if (0 == memcmp(&resRowneruuid, &emptyUuid, sizeof(resRowneruuid)))
1348 isCredRownerUuidEmpty = true;
1352 isCredRownerUuidEmpty = false;
1357 if ( isDevOwnerUuidEmpty ||isDevRownerUuidEmpty )
1359 OIC_LOG_V(ERROR, TAG, "%s - uuid is empty for owned device!", __func__);
1360 res = OC_STACK_ERROR;
1364 if(!GetPstatIsop() || (cm & TAKE_OWNER))
1366 OIC_LOG_V(ERROR, TAG, "%s - isop or cm properties are incorrect!", __func__);
1367 res = OC_STACK_ERROR;
1371 if (isPstatRownerUuidEmpty)
1373 OIC_LOG_V(ERROR, TAG, "%s - pstatRownerUuid is empty for owned device!", __func__);
1374 res = OC_STACK_ERROR;;
1378 if (isAclRownerUuidEmpty)
1380 OIC_LOG_V(ERROR, TAG, "%s - aclRownerUuid is empty for owned device!", __func__);
1381 res = OC_STACK_ERROR;
1385 if (isCredRownerUuidEmpty)
1387 OIC_LOG_V(ERROR, TAG, "%s - credRownerUuid is empty for owned device!", __func__);
1388 res = OC_STACK_ERROR;
1392 if (OC_STACK_OK != CheckSecurityACEPermission(PERMISSION_READ))
1394 OIC_LOG_V(ERROR, TAG, "%s - ACE permission doesn't match to READ", __func__);
1395 res = OC_STACK_ERROR;
1401 if ( !isDevOwnerUuidEmpty ||!isDevRownerUuidEmpty )
1403 OIC_LOG_V(ERROR, TAG, "%s - uuid is not empty for unOwned device!", __func__);
1404 res = OC_STACK_ERROR;
1408 if (!isPstatRownerUuidEmpty)
1410 OIC_LOG_V(ERROR, TAG, "%s - pstatRownerUuid is not empty for unOwned device!", __func__);
1411 res = OC_STACK_ERROR;
1415 if(GetPstatIsop() || !(cm & TAKE_OWNER))
1417 res = OC_STACK_ERROR;
1421 if (!isAclRownerUuidEmpty)
1423 OIC_LOG_V(ERROR, TAG, "%s - aclRownerUuid is not empty for unOwned device!", __func__);
1424 res = OC_STACK_ERROR;
1428 if (!isCredRownerUuidEmpty)
1430 OIC_LOG_V(ERROR, TAG, "%s - credRownerUuid is not empty for unOwned device!", __func__);
1431 res = OC_STACK_ERROR;
1435 if (OC_STACK_OK != CheckSecurityACEPermission(PERMISSION_READ | PERMISSION_WRITE))
1437 OIC_LOG_V(ERROR, TAG, "%s - ACE permission doesn't match to READ|WRITE", __func__);
1438 res = OC_STACK_ERROR;
1447 if (OC_STACK_OK != res)
1449 if (isResetPFExist() && !isOwned)
1451 res = ResetSecureResourceInPS();
1452 OIC_LOG_V(INFO, TAG, "%s - Secure resources reseted (%d)", __func__, res);
1456 res = OC_STACK_INCONSISTENT_DB;
1460 if(OC_STACK_OK == res)
1462 SetSVRDBValidStatus(SVRDB_VALID);
1466 SetSVRDBValidStatus(SVRDB_NOT_VALID);
1468 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);
1473 bool isResetPFExist(void)
1475 OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
1479 uint8_t *dbData = NULL;
1481 OCStackResult ocRes = OC_STACK_ERROR;
1483 ocRes = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
1484 if (OC_STACK_OK != ocRes)
1486 OIC_LOG_V(ERROR, TAG, "GetSecureVirtualDatabaseFromPS() is failed(%d)", ocRes);
1488 if (dbData && dbSize)
1490 ocRes = OC_STACK_ERROR;
1492 CborParser parser = OC_DEFAULT_CBOR_PARSER;
1493 CborValue cbor = OC_DEFAULT_CBOR_VALUE;
1494 cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
1495 CborValue curVal = OC_DEFAULT_CBOR_VALUE;
1496 CborError cborFindResult = CborNoError;
1498 cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_RESET_PF_NAME, &curVal);
1499 if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
1501 OIC_LOG(DEBUG, TAG, "Reset Profile already exists!");
1506 OIC_LOG(DEBUG, TAG, "Reset Profile doesn't exists!");
1512 OIC_LOG_V(DEBUG, TAG, "Out %s", __func__);