Pfp Cybersecurity (Aka Power Fingerprinting, Inc.) / Mbed OS pfp-emon-nxp

Dependencies:   FXAS21002 FXOS8700Q

Committer:
vithyat
Date:
Wed Aug 28 19:24:56 2019 +0000
Revision:
0:977e87915078
init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vithyat 0:977e87915078 1 // ----------------------------------------------------------------------------
vithyat 0:977e87915078 2 // Copyright 2018 ARM Ltd.
vithyat 0:977e87915078 3 //
vithyat 0:977e87915078 4 // Licensed under the Apache License, Version 2.0 (the "License");
vithyat 0:977e87915078 5 // you may not use this file except in compliance with the License.
vithyat 0:977e87915078 6 // You may obtain a copy of the License at
vithyat 0:977e87915078 7 //
vithyat 0:977e87915078 8 // http://www.apache.org/licenses/LICENSE-2.0
vithyat 0:977e87915078 9 //
vithyat 0:977e87915078 10 // Unless required by applicable law or agreed to in writing, software
vithyat 0:977e87915078 11 // distributed under the License is distributed on an "AS IS" BASIS,
vithyat 0:977e87915078 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vithyat 0:977e87915078 13 // See the License for the specific language governing permissions and
vithyat 0:977e87915078 14 // limitations under the License.
vithyat 0:977e87915078 15 // ----------------------------------------------------------------------------
vithyat 0:977e87915078 16
vithyat 0:977e87915078 17 #include <stdio.h>
vithyat 0:977e87915078 18 #include <stdbool.h>
vithyat 0:977e87915078 19 #include "pv_error_handling.h"
vithyat 0:977e87915078 20 #include "certificate_enrollment.h"
vithyat 0:977e87915078 21 #include "key_config_manager.h"
vithyat 0:977e87915078 22 #include "pv_macros.h"
vithyat 0:977e87915078 23 #include "fcc_defs.h"
vithyat 0:977e87915078 24 #include "ce_internal.h"
vithyat 0:977e87915078 25 #include "storage.h"
vithyat 0:977e87915078 26
vithyat 0:977e87915078 27 extern const char g_renewal_status_file[];
vithyat 0:977e87915078 28
vithyat 0:977e87915078 29 ce_status_e ce_init(void)
vithyat 0:977e87915078 30 {
vithyat 0:977e87915078 31 return kcm_init() == KCM_STATUS_SUCCESS ? CE_STATUS_SUCCESS : CE_STATUS_ERROR;
vithyat 0:977e87915078 32 }
vithyat 0:977e87915078 33
vithyat 0:977e87915078 34
vithyat 0:977e87915078 35 ce_status_e ce_error_handler(kcm_status_e kcm_status)
vithyat 0:977e87915078 36 {
vithyat 0:977e87915078 37 switch (kcm_status) {
vithyat 0:977e87915078 38 case KCM_STATUS_SUCCESS:
vithyat 0:977e87915078 39 return CE_STATUS_SUCCESS;
vithyat 0:977e87915078 40 case KCM_STATUS_INVALID_PARAMETER:
vithyat 0:977e87915078 41 return CE_STATUS_INVALID_PARAMETER;
vithyat 0:977e87915078 42 case KCM_STATUS_OUT_OF_MEMORY:
vithyat 0:977e87915078 43 return CE_STATUS_OUT_OF_MEMORY;
vithyat 0:977e87915078 44 case KCM_STATUS_INSUFFICIENT_BUFFER:
vithyat 0:977e87915078 45 return CE_STATUS_INSUFFICIENT_BUFFER;
vithyat 0:977e87915078 46 case KCM_STATUS_ITEM_NOT_FOUND:
vithyat 0:977e87915078 47 return CE_STATUS_ITEM_NOT_FOUND;
vithyat 0:977e87915078 48 case KCM_STATUS_ITEM_IS_EMPTY:
vithyat 0:977e87915078 49 return CE_STATUS_ITEM_IS_EMPTY;
vithyat 0:977e87915078 50 default:
vithyat 0:977e87915078 51 return CE_STATUS_ERROR;
vithyat 0:977e87915078 52 }
vithyat 0:977e87915078 53 }
vithyat 0:977e87915078 54
vithyat 0:977e87915078 55 ce_status_e ce_generate_keys_and_create_csr_from_certificate(
vithyat 0:977e87915078 56 const char *certificate_name, const cs_key_handle_t key_h,
vithyat 0:977e87915078 57 uint8_t **csr_out, size_t *csr_size_out)
vithyat 0:977e87915078 58 {
vithyat 0:977e87915078 59 bool success;
vithyat 0:977e87915078 60 ce_status_e ce_status = CE_STATUS_SUCCESS;
vithyat 0:977e87915078 61 kcm_status_e kcm_status = KCM_STATUS_SUCCESS;
vithyat 0:977e87915078 62 uint8_t *certificate_buff = NULL;
vithyat 0:977e87915078 63 size_t certificate_buff_max_size = 0, certificate_buff_size = 0, certificate_private_key_size = 0;
vithyat 0:977e87915078 64 uint8_t *csr_buff = NULL;
vithyat 0:977e87915078 65 size_t csr_buff_size = 0, csr_buff_max_size;
vithyat 0:977e87915078 66 char *kcm_crt_name = NULL, *kcm_priv_key_name = NULL;
vithyat 0:977e87915078 67 uint32_t kcm_crt_name_size = (uint32_t)strlen(certificate_name) + 1; // append null termination
vithyat 0:977e87915078 68
vithyat 0:977e87915078 69
vithyat 0:977e87915078 70 SA_PV_ERR_RECOVERABLE_RETURN_IF((certificate_name == NULL), CE_STATUS_INVALID_PARAMETER, "Invalid certificate_name");
vithyat 0:977e87915078 71 SA_PV_ERR_RECOVERABLE_RETURN_IF((key_h == 0), CE_STATUS_INVALID_PARAMETER, "Invalid key_h");
vithyat 0:977e87915078 72 SA_PV_LOG_INFO_FUNC_ENTER("certificate_name = %s key_h = %" PRIuPTR "", certificate_name, key_h);
vithyat 0:977e87915078 73 SA_PV_ERR_RECOVERABLE_RETURN_IF((csr_out == NULL), CE_STATUS_INVALID_PARAMETER, "Invalid csr_out");
vithyat 0:977e87915078 74 SA_PV_ERR_RECOVERABLE_RETURN_IF((csr_size_out == NULL), CE_STATUS_INVALID_PARAMETER, "Invalid csr_size_out");
vithyat 0:977e87915078 75
vithyat 0:977e87915078 76 // assert NOT a bootstrap device certificate
vithyat 0:977e87915078 77 success = pv_str_equals(g_fcc_bootstrap_device_certificate_name, certificate_name, kcm_crt_name_size);
vithyat 0:977e87915078 78 SA_PV_ERR_RECOVERABLE_RETURN_IF((success), CE_STATUS_FORBIDDEN_REQUEST, "device bootstrap certificate renewal is not allowed");
vithyat 0:977e87915078 79
vithyat 0:977e87915078 80 // assert NOT a bootstrap device key
vithyat 0:977e87915078 81 success = pv_str_equals(g_fcc_bootstrap_device_private_key_name, certificate_name, kcm_crt_name_size);
vithyat 0:977e87915078 82 SA_PV_ERR_RECOVERABLE_RETURN_IF((success), CE_STATUS_FORBIDDEN_REQUEST, "device bootstrap certificate renewal is not allowed");
vithyat 0:977e87915078 83
vithyat 0:977e87915078 84 success = ce_set_item_names(certificate_name, &kcm_priv_key_name, NULL, &kcm_crt_name);
vithyat 0:977e87915078 85 SA_PV_ERR_RECOVERABLE_RETURN_IF((!success), CE_STATUS_ITEM_NOT_FOUND, "failed for ce_set_item_names()");
vithyat 0:977e87915078 86
vithyat 0:977e87915078 87 // getting the private key size successfully signifies that the certificate's private key exist and we're okay to continue
vithyat 0:977e87915078 88 kcm_status = kcm_item_get_data_size((const uint8_t *)kcm_priv_key_name, strlen(kcm_priv_key_name), KCM_PRIVATE_KEY_ITEM, &certificate_private_key_size);
vithyat 0:977e87915078 89 SA_PV_ERR_RECOVERABLE_RETURN_IF((kcm_status != KCM_STATUS_SUCCESS), ce_error_handler(kcm_status), "failed to get the certificate private key length");
vithyat 0:977e87915078 90 SA_PV_ERR_RECOVERABLE_RETURN_IF((certificate_private_key_size == 0), CE_STATUS_ITEM_IS_EMPTY, "got empty private key for certificate %s", kcm_crt_name);
vithyat 0:977e87915078 91
vithyat 0:977e87915078 92 // get the certificate octet length
vithyat 0:977e87915078 93 kcm_status = kcm_item_get_data_size((const uint8_t *)kcm_crt_name, strlen(kcm_crt_name), KCM_CERTIFICATE_ITEM, &certificate_buff_max_size);
vithyat 0:977e87915078 94 SA_PV_ERR_RECOVERABLE_RETURN_IF((kcm_status != KCM_STATUS_SUCCESS), CE_STATUS_ERROR, "failed to get certificate octet length");
vithyat 0:977e87915078 95 SA_PV_ERR_RECOVERABLE_RETURN_IF((certificate_buff_max_size == 0), CE_STATUS_ITEM_IS_EMPTY, "got 0 length for certificate");
vithyat 0:977e87915078 96
vithyat 0:977e87915078 97 certificate_buff = (uint8_t *)malloc(certificate_buff_max_size);
vithyat 0:977e87915078 98 SA_PV_ERR_RECOVERABLE_RETURN_IF((certificate_buff == NULL), CE_STATUS_OUT_OF_MEMORY, "failed allocating certificate buffer");
vithyat 0:977e87915078 99
vithyat 0:977e87915078 100 // get the certificate bytes
vithyat 0:977e87915078 101 kcm_status = kcm_item_get_data((const uint8_t *)kcm_crt_name, strlen(kcm_crt_name), KCM_CERTIFICATE_ITEM, certificate_buff, certificate_buff_max_size, &certificate_buff_size);
vithyat 0:977e87915078 102 SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS), (ce_status = ce_error_handler(kcm_status)), exit, "failed to get certificate buffer");
vithyat 0:977e87915078 103 SA_PV_ERR_RECOVERABLE_GOTO_IF((certificate_buff_size == 0), (ce_status = CE_STATUS_ITEM_IS_EMPTY), exit, "got 0 length for certificate");
vithyat 0:977e87915078 104
vithyat 0:977e87915078 105 // we assume that the CSR size would not exceed the certificate size
vithyat 0:977e87915078 106 csr_buff_max_size = certificate_buff_size;
vithyat 0:977e87915078 107
vithyat 0:977e87915078 108 csr_buff = (uint8_t *)malloc(csr_buff_max_size);
vithyat 0:977e87915078 109 SA_PV_ERR_RECOVERABLE_GOTO_IF((csr_buff == NULL), (ce_status = CE_STATUS_OUT_OF_MEMORY), exit, "Failed allocating CSR buffer");
vithyat 0:977e87915078 110
vithyat 0:977e87915078 111 kcm_status = cs_generate_keys_and_create_csr_from_certificate(certificate_buff, certificate_buff_size, key_h, csr_buff, csr_buff_max_size, &csr_buff_size);
vithyat 0:977e87915078 112 SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS), (ce_status = ce_error_handler(kcm_status)), exit, "failed to generate keys and create CSR");
vithyat 0:977e87915078 113 SA_PV_ERR_RECOVERABLE_GOTO_IF((csr_buff == NULL), (ce_status = CE_STATUS_ERROR), exit, "failed creating CSR or generating keys for certificate (%s)", kcm_crt_name);
vithyat 0:977e87915078 114
vithyat 0:977e87915078 115
vithyat 0:977e87915078 116 // the calling user is responsible to free csr_out buffer
vithyat 0:977e87915078 117 *csr_out = csr_buff;
vithyat 0:977e87915078 118 *csr_size_out = csr_buff_size;
vithyat 0:977e87915078 119
vithyat 0:977e87915078 120 SA_PV_LOG_INFO_FUNC_EXIT("csr_size_out = %" PRIu32 "", (uint32_t)(*csr_size_out));
vithyat 0:977e87915078 121
vithyat 0:977e87915078 122 exit:
vithyat 0:977e87915078 123 if (certificate_buff != NULL) {
vithyat 0:977e87915078 124 free(certificate_buff);
vithyat 0:977e87915078 125 }
vithyat 0:977e87915078 126 if (ce_status != CE_STATUS_SUCCESS) {
vithyat 0:977e87915078 127 free(csr_buff);
vithyat 0:977e87915078 128 }
vithyat 0:977e87915078 129
vithyat 0:977e87915078 130 return ce_status;
vithyat 0:977e87915078 131 }
vithyat 0:977e87915078 132 ce_status_e ce_safe_renewal(const char *item_name, ce_renewal_params_s *renewal_data)
vithyat 0:977e87915078 133 {
vithyat 0:977e87915078 134 bool success;
vithyat 0:977e87915078 135 ce_status_e ce_status = CE_STATUS_SUCCESS;
vithyat 0:977e87915078 136 kcm_status_e kcm_status = KCM_STATUS_SUCCESS;
vithyat 0:977e87915078 137 char *priv_key_name = NULL, *pub_key_name = NULL, *certificate_name = NULL;
vithyat 0:977e87915078 138 size_t data_size_out;
vithyat 0:977e87915078 139 bool is_public_key = false;
vithyat 0:977e87915078 140 cs_ec_key_context_s *ec_key_ctx = NULL;
vithyat 0:977e87915078 141 struct cert_chain_context_s *certificate_chain_data = NULL;
vithyat 0:977e87915078 142
vithyat 0:977e87915078 143 //Check parameters
vithyat 0:977e87915078 144 SA_PV_ERR_RECOVERABLE_RETURN_IF((item_name == NULL), CE_STATUS_INVALID_PARAMETER, "Invalid item_name");
vithyat 0:977e87915078 145 SA_PV_ERR_RECOVERABLE_RETURN_IF((renewal_data == NULL), CE_STATUS_INVALID_PARAMETER, "Invalid renewal_data");
vithyat 0:977e87915078 146 SA_PV_ERR_RECOVERABLE_RETURN_IF((renewal_data->crypto_handle ==(cs_key_handle_t) NULL), CE_STATUS_INVALID_PARAMETER, "Invalid crypto handle");
vithyat 0:977e87915078 147 SA_PV_ERR_RECOVERABLE_RETURN_IF((renewal_data->cert_data == NULL), CE_STATUS_INVALID_PARAMETER, "Invalid cert_data");
vithyat 0:977e87915078 148 certificate_chain_data = (struct cert_chain_context_s*)renewal_data->cert_data;
vithyat 0:977e87915078 149 SA_PV_ERR_RECOVERABLE_RETURN_IF((certificate_chain_data->certs == NULL || certificate_chain_data->chain_length == 0), CE_STATUS_INVALID_PARAMETER, "Invalid certificate data");
vithyat 0:977e87915078 150 SA_PV_LOG_INFO_FUNC_ENTER("item_name = %s ", item_name);
vithyat 0:977e87915078 151
vithyat 0:977e87915078 152 //Set item names
vithyat 0:977e87915078 153 success = ce_set_item_names(item_name, &priv_key_name, &pub_key_name, &certificate_name);
vithyat 0:977e87915078 154 SA_PV_ERR_RECOVERABLE_RETURN_IF((!success), CE_STATUS_ITEM_NOT_FOUND, "failed for ce_set_item_names()");
vithyat 0:977e87915078 155
vithyat 0:977e87915078 156 if (pub_key_name != NULL) { //If not lwm2m items
vithyat 0:977e87915078 157 //Check if public key is present
vithyat 0:977e87915078 158 kcm_status = kcm_item_get_data_size((const uint8_t *)pub_key_name, strlen(pub_key_name), KCM_PUBLIC_KEY_ITEM, &data_size_out);
vithyat 0:977e87915078 159 SA_PV_ERR_RECOVERABLE_RETURN_IF((kcm_status != KCM_STATUS_SUCCESS && kcm_status != KCM_STATUS_ITEM_NOT_FOUND), CE_STATUS_STORAGE_ERROR, "failed to get public key size");
vithyat 0:977e87915078 160
vithyat 0:977e87915078 161 //Set public key flag
vithyat 0:977e87915078 162 if (kcm_status == KCM_STATUS_SUCCESS) {
vithyat 0:977e87915078 163 is_public_key = true;
vithyat 0:977e87915078 164 }
vithyat 0:977e87915078 165 }
vithyat 0:977e87915078 166
vithyat 0:977e87915078 167 //Verify items correlation
vithyat 0:977e87915078 168 kcm_status = cs_verify_items_correlation(renewal_data->crypto_handle, renewal_data->cert_data->certs->cert, renewal_data->cert_data->certs->cert_length);
vithyat 0:977e87915078 169 SA_PV_ERR_RECOVERABLE_RETURN_IF((kcm_status != KCM_STATUS_SUCCESS), CE_STATUS_RENEWAL_ITEM_VALIDATION_ERROR, "failed to validate renewal items");
vithyat 0:977e87915078 170
vithyat 0:977e87915078 171 //Create backup items
vithyat 0:977e87915078 172 kcm_status = ce_create_backup_items(item_name, is_public_key);
vithyat 0:977e87915078 173 if (kcm_status == KCM_STATUS_ITEM_NOT_FOUND) {
vithyat 0:977e87915078 174 ce_status = CE_STATUS_ORIGINAL_ITEM_ERROR;
vithyat 0:977e87915078 175 }
vithyat 0:977e87915078 176 if (kcm_status != KCM_STATUS_SUCCESS && kcm_status != KCM_STATUS_ITEM_NOT_FOUND) {
vithyat 0:977e87915078 177 ce_status = CE_STATUS_BACKUP_ITEM_ERROR;
vithyat 0:977e87915078 178 }
vithyat 0:977e87915078 179 SA_PV_ERR_RECOVERABLE_GOTO_IF((ce_status != CE_STATUS_SUCCESS), ce_status = ce_status, exit_and_delete_renewal_data,"failed to create backup items");
vithyat 0:977e87915078 180
vithyat 0:977e87915078 181 //Create renewal status file and write item_name to the file
vithyat 0:977e87915078 182 kcm_status = ce_create_renewal_status(item_name);
vithyat 0:977e87915078 183 if (kcm_status == KCM_STATUS_FILE_EXIST) {
vithyat 0:977e87915078 184 //Assumption : in case of existing active renewal process ->ce_safe_renewal api blocked by event loop.
vithyat 0:977e87915078 185 // So we assume that it is ok to delete renewal status file, as it is impossible that it used by another active renewal process.
vithyat 0:977e87915078 186 //try to delete existing renewal status file and create new one
vithyat 0:977e87915078 187 ce_delete_renewal_status();
vithyat 0:977e87915078 188 kcm_status = ce_create_renewal_status(item_name);
vithyat 0:977e87915078 189 }
vithyat 0:977e87915078 190 SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS), ce_status = CE_STATUS_RENEWAL_STATUS_ERROR, exit_and_delete_renewal_data, "failed to create renewal status file");
vithyat 0:977e87915078 191
vithyat 0:977e87915078 192 //Clean original items
vithyat 0:977e87915078 193 kcm_status = ce_clean_items(item_name, KCM_ORIGINAL_ITEM, is_public_key );
vithyat 0:977e87915078 194 SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS), ce_status = CE_STATUS_STORAGE_ERROR, restore_backup_data, "Falid to clean original items");
vithyat 0:977e87915078 195
vithyat 0:977e87915078 196 ec_key_ctx = (cs_ec_key_context_s*)renewal_data->crypto_handle;
vithyat 0:977e87915078 197
vithyat 0:977e87915078 198 //Save new items
vithyat 0:977e87915078 199 kcm_status = kcm_item_store((const uint8_t*)priv_key_name, strlen(priv_key_name), KCM_PRIVATE_KEY_ITEM, false, ec_key_ctx->priv_key, ec_key_ctx->priv_key_size, NULL);
vithyat 0:977e87915078 200 SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS), ce_status = CE_STATUS_STORAGE_ERROR, restore_backup_data, "Falid to store new private key");
vithyat 0:977e87915078 201
vithyat 0:977e87915078 202 if (is_public_key == true) {
vithyat 0:977e87915078 203 kcm_status = kcm_item_store((const uint8_t*)pub_key_name, strlen(pub_key_name), KCM_PUBLIC_KEY_ITEM, false, ec_key_ctx->pub_key, ec_key_ctx->pub_key_size, NULL);
vithyat 0:977e87915078 204 SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS), ce_status = CE_STATUS_STORAGE_ERROR, restore_backup_data, "Falid to store new public key");
vithyat 0:977e87915078 205 }
vithyat 0:977e87915078 206
vithyat 0:977e87915078 207 //Save new certificate/certificate chain
vithyat 0:977e87915078 208 kcm_status = ce_store_new_certificate((const char*)certificate_name, certificate_chain_data);
vithyat 0:977e87915078 209 SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS), ce_status = CE_STATUS_STORAGE_ERROR, restore_backup_data, "Falid to store new certificate/certificate chain");
vithyat 0:977e87915078 210
vithyat 0:977e87915078 211
vithyat 0:977e87915078 212 restore_backup_data:
vithyat 0:977e87915078 213 if (ce_status != CE_STATUS_SUCCESS) {
vithyat 0:977e87915078 214 //the restore here done only in case of some error, and at this stage we are not still want to return an original error
vithyat 0:977e87915078 215 //this is the reason why we don't read the returned error of ce_restore_backup_items api
vithyat 0:977e87915078 216 ce_restore_backup_items(item_name);
vithyat 0:977e87915078 217 }
vithyat 0:977e87915078 218
vithyat 0:977e87915078 219 exit_and_delete_renewal_data:
vithyat 0:977e87915078 220
vithyat 0:977e87915078 221 //Delete renewal status file
vithyat 0:977e87915078 222 ce_delete_renewal_status();
vithyat 0:977e87915078 223
vithyat 0:977e87915078 224 //Clean backup items
vithyat 0:977e87915078 225 ce_clean_items(item_name, KCM_BACKUP_ITEM, is_public_key);
vithyat 0:977e87915078 226
vithyat 0:977e87915078 227 return ce_status;
vithyat 0:977e87915078 228 }
vithyat 0:977e87915078 229
vithyat 0:977e87915078 230 /*! The API called during kcm_init() in case of error during renewal_certificate API.
vithyat 0:977e87915078 231 * The functions checks status of the renewal process, restores original data and deletes redundant files.
vithyat 0:977e87915078 232 * The APIs checks the status based on renewal file and its data.
vithyat 0:977e87915078 233 * @void
vithyat 0:977e87915078 234 */
vithyat 0:977e87915078 235 void ce_check_and_restore_backup_status(void)
vithyat 0:977e87915078 236 {
vithyat 0:977e87915078 237 kcm_status_e kcm_status = KCM_STATUS_SUCCESS;
vithyat 0:977e87915078 238 size_t renewal_item_data_len = 0;
vithyat 0:977e87915078 239 size_t act_renewal_item_data_len = 0;
vithyat 0:977e87915078 240 uint8_t renewal_item_name[CE_MAX_SIZE_OF_KCM_ITEM_NAME] = { 0 };
vithyat 0:977e87915078 241
vithyat 0:977e87915078 242
vithyat 0:977e87915078 243 //Get renewal status file size
vithyat 0:977e87915078 244 kcm_status = storage_data_size_read((const uint8_t *)g_renewal_status_file, strlen(g_renewal_status_file), KCM_CONFIG_ITEM, KCM_BACKUP_ITEM, &renewal_item_data_len);
vithyat 0:977e87915078 245
vithyat 0:977e87915078 246 //If renewal status file is not found or failed to get data size -> exit , no data to restore
vithyat 0:977e87915078 247 if (kcm_status != KCM_STATUS_SUCCESS) {
vithyat 0:977e87915078 248 if (kcm_status != KCM_STATUS_ITEM_NOT_FOUND) {
vithyat 0:977e87915078 249 SA_PV_LOG_ERR("Failed to read renewal status");//Add error print, as this case is exceptional
vithyat 0:977e87915078 250 }
vithyat 0:977e87915078 251 return;
vithyat 0:977e87915078 252 }
vithyat 0:977e87915078 253 if (renewal_item_data_len + 1 > sizeof(renewal_item_name)) {
vithyat 0:977e87915078 254 SA_PV_LOG_ERR("Renewal item name is too big");//Add error print, as this case is exceptional
vithyat 0:977e87915078 255 return;
vithyat 0:977e87915078 256 }
vithyat 0:977e87915078 257
vithyat 0:977e87915078 258 //Read renewal status data
vithyat 0:977e87915078 259 kcm_status = storage_data_read((const uint8_t *)g_renewal_status_file, strlen(g_renewal_status_file), KCM_CONFIG_ITEM, KCM_BACKUP_ITEM, renewal_item_name, renewal_item_data_len, &act_renewal_item_data_len);
vithyat 0:977e87915078 260 SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS || act_renewal_item_data_len != renewal_item_data_len), kcm_status = kcm_status, exit, "Failed to read renewal status data");
vithyat 0:977e87915078 261
vithyat 0:977e87915078 262 //Set null terminator
vithyat 0:977e87915078 263 // renewal_item_data[renewal_item_data_len] ='\0';
vithyat 0:977e87915078 264 renewal_item_name[renewal_item_data_len] = '\0';
vithyat 0:977e87915078 265
vithyat 0:977e87915078 266 //Restore backup items - this will clean all unnecessary data
vithyat 0:977e87915078 267 kcm_status = ce_restore_backup_items((const char *)renewal_item_name);
vithyat 0:977e87915078 268 SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS && kcm_status!= KCM_STATUS_ITEM_NOT_FOUND), kcm_status = kcm_status, exit, "Failed to restore backup items");
vithyat 0:977e87915078 269
vithyat 0:977e87915078 270
vithyat 0:977e87915078 271 exit:
vithyat 0:977e87915078 272 //Delete renewal status file
vithyat 0:977e87915078 273 kcm_status = storage_data_delete((const uint8_t *)g_renewal_status_file, (size_t)strlen(g_renewal_status_file), KCM_CONFIG_ITEM, KCM_BACKUP_ITEM);
vithyat 0:977e87915078 274 if (kcm_status != KCM_STATUS_SUCCESS) {
vithyat 0:977e87915078 275 SA_PV_LOG_ERR("Failed to delete renewal status");//Add error print, as this case is exceptional
vithyat 0:977e87915078 276 }
vithyat 0:977e87915078 277
vithyat 0:977e87915078 278 SA_PV_LOG_INFO_FUNC_EXIT_NO_ARGS();
vithyat 0:977e87915078 279 return;
vithyat 0:977e87915078 280 }
vithyat 0:977e87915078 281