Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers storage_common.c Source File

storage_common.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //     http://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 // ----------------------------------------------------------------------------
00016 
00017 
00018 #include <stdbool.h>
00019 #include "pv_error_handling.h"
00020 #include "pv_macros.h"
00021 #include "storage.h"
00022 #include "esfs.h"
00023 #include "fcc_malloc.h"
00024 
00025 extern bool g_kcm_initialized;
00026 
00027 /**
00028 *   The function returns prefix, according to kcm type and data source type
00029 *    @param[in] kcm_item_type     type of KCM item.
00030 *    @param[in] item_source_type  type of source type (original or backup)
00031 *    @param[out] prefix           returned prefix
00032 *    @returns
00033 *       KCM_STATUS_SUCCESS in case of success or one of the `::kcm_status_e` errors otherwise.
00034 */
00035 kcm_status_e  storage_item_name_get_prefix(kcm_item_type_e kcm_item_type, kcm_data_source_type_e item_source_type, const char** prefix)
00036 {
00037     kcm_status_e  status = KCM_STATUS_SUCCESS;
00038 
00039     SA_PV_ERR_RECOVERABLE_RETURN_IF((item_source_type != KCM_ORIGINAL_ITEM && item_source_type != KCM_BACKUP_ITEM), KCM_STATUS_INVALID_PARAMETER, "Invalid item_source_type");
00040 
00041     switch (kcm_item_type) {
00042     case KCM_PRIVATE_KEY_ITEM:
00043         (item_source_type == KCM_ORIGINAL_ITEM) ? (*prefix = KCM_FILE_PREFIX_PRIVATE_KEY) : (*prefix = KCM_RENEWAL_FILE_PREFIX_PRIVATE_KEY);
00044         break;
00045     case KCM_PUBLIC_KEY_ITEM:
00046         (item_source_type == KCM_ORIGINAL_ITEM) ? (*prefix = KCM_FILE_PREFIX_PUBLIC_KEY) : (*prefix = KCM_RENEWAL_FILE_PREFIX_PUBLIC_KEY);
00047         break;
00048     case KCM_SYMMETRIC_KEY_ITEM:
00049         (item_source_type == KCM_ORIGINAL_ITEM) ? (*prefix = KCM_FILE_PREFIX_SYMMETRIC_KEY) : (*prefix = KCM_RENEWAL_FILE_PREFIX_SYMMETRIC_KEY);
00050         break;
00051     case KCM_CERTIFICATE_ITEM:
00052         (item_source_type == KCM_ORIGINAL_ITEM) ? (*prefix = KCM_FILE_PREFIX_CERTIFICATE) : (*prefix = KCM_RENEWAL_FILE_PREFIX_CERTIFICATE);
00053         break;
00054     case KCM_CONFIG_ITEM:
00055         (item_source_type == KCM_ORIGINAL_ITEM) ? (*prefix = KCM_FILE_PREFIX_CONFIG_PARAM) : (*prefix = KCM_RENEWAL_FILE_PREFIX_CONFIG_PARAM);
00056         break;
00057     default:
00058         status = KCM_STATUS_INVALID_PARAMETER;
00059         break;
00060     }
00061     return status;
00062 }
00063 
00064 
00065 /** Writes a new item to storage
00066 *
00067 *    @param[in] kcm_item_name KCM item name.
00068 *    @param[in] kcm_item_name_len KCM item name length.
00069 *    @param[in] kcm_item_type KCM item type as defined in `::kcm_item_type_e`
00070 *    @param[in] kcm_item_is_factory True if the KCM item is a factory item, otherwise false.
00071 *    @param[in] data_source_type KCM item data source (original or backup).
00072 *    @param[in] kcm_item_data KCM item data buffer. Can be NULL if `kcm_item_data_size` is 0.
00073 *    @param[in] kcm_item_data_size KCM item data buffer size in bytes. Can be 0 if you wish to
00074 *     store an empty file.
00075 *
00076 *  @returns
00077 *        KCM_STATUS_SUCCESS in case of success or one of the `::kcm_status_e` errors otherwise.*/
00078 kcm_status_e  storage_data_write(const uint8_t * kcm_item_name,
00079     size_t kcm_item_name_len,
00080     kcm_item_type_e kcm_item_type,
00081     bool kcm_item_is_factory,
00082     kcm_data_source_type_e data_source_type,
00083     const uint8_t * kcm_item_data,
00084     size_t kcm_item_data_size)
00085 {
00086     kcm_status_e  kcm_status = KCM_STATUS_SUCCESS;
00087     bool kcm_item_is_encrypted = true;
00088 
00089 
00090     // Validate function parameters
00091     SA_PV_ERR_RECOVERABLE_RETURN_IF((kcm_item_name == NULL), KCM_STATUS_INVALID_PARAMETER, "Invalid kcm_item_name");
00092     SA_PV_ERR_RECOVERABLE_RETURN_IF((kcm_item_name_len == 0), KCM_STATUS_INVALID_PARAMETER, "Invalid kcm_item_name_len");
00093     SA_PV_LOG_INFO_FUNC_ENTER("item name =  %.*s len=%" PRIu32 ", data size=%" PRIu32 "", (int)kcm_item_name_len, (char*)kcm_item_name, (uint32_t)kcm_item_name_len, (uint32_t)kcm_item_data_size);
00094     SA_PV_ERR_RECOVERABLE_RETURN_IF(((kcm_item_data == NULL) && (kcm_item_data_size > 0)), KCM_STATUS_INVALID_PARAMETER, "Provided kcm_item_data NULL and kcm_item_data_size greater than 0");
00095     SA_PV_ERR_RECOVERABLE_RETURN_IF((kcm_item_type != KCM_CONFIG_ITEM && kcm_item_data_size == 0), KCM_STATUS_ITEM_IS_EMPTY, "The data of current item is empty!");
00096     SA_PV_ERR_RECOVERABLE_RETURN_IF((data_source_type != KCM_ORIGINAL_ITEM && data_source_type != KCM_BACKUP_ITEM), KCM_STATUS_INVALID_PARAMETER, "Invalid data_source_type");
00097     SA_PV_ERR_RECOVERABLE_RETURN_IF((data_source_type == KCM_BACKUP_ITEM && kcm_item_is_factory == true), KCM_STATUS_INVALID_PARAMETER, "Invalid kcm_item_is_factory parameter");
00098 
00099     // Check if KCM initialized, if not initialize it
00100     if (!g_kcm_initialized) {
00101         kcm_status = kcm_init();
00102         SA_PV_ERR_RECOVERABLE_RETURN_IF((kcm_status != KCM_STATUS_SUCCESS), kcm_status, "KCM initialization failed\n");
00103     }
00104 
00105     switch (kcm_item_type) {
00106     case KCM_PRIVATE_KEY_ITEM:
00107         break;
00108     case KCM_PUBLIC_KEY_ITEM:
00109         kcm_item_is_encrypted = false; //do not encrypt public key
00110         break;
00111     case KCM_CERTIFICATE_ITEM:
00112         kcm_item_is_encrypted = false; //do not encrypt certificates
00113         break;
00114     case  KCM_SYMMETRIC_KEY_ITEM:
00115         break;
00116     case KCM_CONFIG_ITEM:
00117         break;
00118     default:
00119         SA_PV_ERR_RECOVERABLE_RETURN_IF((true), KCM_STATUS_INVALID_PARAMETER, "Invalid kcm_item_type");
00120     }
00121 
00122     kcm_status = storage_data_write_impl(kcm_item_name, kcm_item_name_len, kcm_item_type, kcm_item_is_factory, kcm_item_is_encrypted, data_source_type, kcm_item_data, kcm_item_data_size);
00123     SA_PV_ERR_RECOVERABLE_RETURN_IF((kcm_status != KCM_STATUS_SUCCESS), kcm_status, "storage_data_write_impl failed\n");
00124 
00125     return kcm_status;
00126 
00127 }
00128