Jim Carver / Mbed OS mbed-cloud-workshop-connect
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers common_utils.c Source File

common_utils.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 #include "factory_configurator_client.h"
00018 #include "fcc_status.h"
00019 #include "fcc_verification.h"
00020 #include "key_config_manager.h"
00021 #include "pv_error_handling.h"
00022 #include "cs_der_certs.h"
00023 #include "cs_utils.h"
00024 #include "fcc_output_info_handler.h"
00025 #include "fcc_malloc.h"
00026 #include "time.h"
00027 #include "cs_utils.h"
00028 #include "fcc_sotp.h"
00029 
00030 
00031 fcc_status_e  fcc_get_kcm_data(const uint8_t *parameter_name, size_t size_of_parameter_name, kcm_item_type_e kcm_type, uint8_t **kcm_data, size_t *kcm_data_size)
00032 {
00033 
00034     kcm_status_e  kcm_status = KCM_STATUS_SUCCESS;
00035     fcc_status_e  fcc_status = FCC_STATUS_SUCCESS;
00036 
00037     SA_PV_LOG_INFO_FUNC_ENTER_NO_ARGS();
00038     SA_PV_ERR_RECOVERABLE_RETURN_IF((parameter_name == NULL), fcc_status = FCC_STATUS_INVALID_PARAMETER, "Wrong parameter_name pointer");
00039     SA_PV_ERR_RECOVERABLE_RETURN_IF((size_of_parameter_name == 0), fcc_status = FCC_STATUS_INVALID_PARAMETER, "Wrong parameter_name size.");
00040     SA_PV_ERR_RECOVERABLE_RETURN_IF((*kcm_data != NULL), fcc_status = FCC_STATUS_INVALID_PARAMETER, "Wrong *kcm_data pointer, should be NULL");
00041     SA_PV_ERR_RECOVERABLE_RETURN_IF((kcm_data_size == NULL), fcc_status = FCC_STATUS_INVALID_PARAMETER, "Wrong kcm_data_size pointer.");
00042 
00043     //Get size of kcm data
00044     kcm_status = kcm_item_get_data_size(parameter_name,
00045         size_of_parameter_name,
00046         kcm_type,
00047         kcm_data_size);
00048     if (kcm_status == KCM_STATUS_ITEM_NOT_FOUND) {
00049         return FCC_STATUS_ITEM_NOT_EXIST;
00050     }
00051     SA_PV_ERR_RECOVERABLE_RETURN_IF((kcm_status != KCM_STATUS_SUCCESS), fcc_status = FCC_STATUS_KCM_STORAGE_ERROR, "Failed to get kcm data size");
00052     SA_PV_ERR_RECOVERABLE_RETURN_IF((*kcm_data_size == 0), fcc_status = FCC_STATUS_EMPTY_ITEM, "KCM item is empty");
00053 
00054     //Alocate memory and get device certificate data
00055     *kcm_data = fcc_malloc(*kcm_data_size);
00056     SA_PV_ERR_RECOVERABLE_RETURN_IF((*kcm_data == NULL), fcc_status = FCC_STATUS_MEMORY_OUT, "Failed to allocate buffer for kcm data");
00057 
00058     kcm_status = kcm_item_get_data(parameter_name,
00059         size_of_parameter_name,
00060         kcm_type,
00061         *kcm_data, *kcm_data_size, kcm_data_size);
00062     SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status == KCM_STATUS_ITEM_NOT_FOUND), fcc_status = FCC_STATUS_ITEM_NOT_EXIST, exit, "KCM is not found");
00063     SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS), fcc_status = FCC_STATUS_KCM_STORAGE_ERROR, exit, "Failed to get device certificate data");
00064 
00065 exit:
00066     if (fcc_status != FCC_STATUS_SUCCESS) {
00067         fcc_free(*kcm_data);
00068         *kcm_data = NULL;
00069     }
00070     SA_PV_LOG_TRACE_FUNC_EXIT_NO_ARGS();
00071     return fcc_status;
00072 }
00073 
00074 fcc_status_e  fcc_get_certificate_attribute(palX509Handle_t x509_cert, cs_certificate_attribute_type_e attribute_type, uint8_t **attribute_data, size_t *attribute_act_data_size)
00075 {
00076 
00077     kcm_status_e  kcm_status = KCM_STATUS_SUCCESS;
00078     fcc_status_e  fcc_status = FCC_STATUS_SUCCESS;
00079 
00080     SA_PV_LOG_INFO_FUNC_ENTER_NO_ARGS();
00081 
00082     SA_PV_ERR_RECOVERABLE_RETURN_IF((x509_cert == NULLPTR), fcc_status = FCC_STATUS_INVALID_PARAMETER, "Wrong x509 handle.");
00083     SA_PV_ERR_RECOVERABLE_RETURN_IF((*attribute_data != NULL), fcc_status = FCC_STATUS_INVALID_PARAMETER, "Wrong attribute_data pointer.");
00084     SA_PV_ERR_RECOVERABLE_RETURN_IF((attribute_act_data_size == NULL), fcc_status = FCC_STATUS_INVALID_PARAMETER, "attribute_act_data_size pointer is NULL.");
00085 
00086     //Get attribute size
00087     kcm_status = cs_attr_get_data_size_x509_cert(x509_cert, attribute_type, attribute_act_data_size);
00088     SA_PV_ERR_RECOVERABLE_RETURN_IF((kcm_status != KCM_STATUS_SUCCESS), fcc_status = FCC_STATUS_INVALID_CERT_ATTRIBUTE, "Failed to get size of attribute");
00089 
00090     *attribute_data = fcc_malloc(*attribute_act_data_size);
00091     SA_PV_ERR_RECOVERABLE_RETURN_IF((*attribute_data == NULL), fcc_status = FCC_STATUS_MEMORY_OUT, "Failed to allocate memory for attribute");
00092 
00093     //Get data of "CN" attribute
00094     kcm_status = cs_attr_get_data_x509_cert(x509_cert,
00095         attribute_type,
00096         *attribute_data,
00097         *attribute_act_data_size,
00098         attribute_act_data_size);
00099     SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS || *attribute_act_data_size == 0), fcc_status = FCC_STATUS_INVALID_CERT_ATTRIBUTE, exit, "Failed to get attribute data");
00100 
00101 
00102 exit:
00103     if (fcc_status != FCC_STATUS_SUCCESS) {
00104         fcc_free(*attribute_data);
00105         *attribute_data = NULL;
00106     }
00107     SA_PV_LOG_TRACE_FUNC_EXIT_NO_ARGS();
00108     return fcc_status;
00109 }
00110 
00111 fcc_status_e  fcc_get_certificate_attribute_by_name(const uint8_t *cert_name, size_t size_of_cert_name, cs_certificate_attribute_type_e attribute_type, uint8_t *attribute_data,size_t attribute_data_size, size_t *attribute_act_data_size)
00112 {
00113     kcm_status_e  kcm_status = KCM_STATUS_SUCCESS;
00114     fcc_status_e  fcc_status = FCC_STATUS_SUCCESS;
00115     uint8_t *kcm_data = NULL;
00116     size_t kcm_data_size = 0;
00117     palX509Handle_t x509_cert = NULLPTR;
00118 
00119     SA_PV_LOG_INFO_FUNC_ENTER_NO_ARGS();
00120 
00121     SA_PV_ERR_RECOVERABLE_GOTO_IF((cert_name == NULL), fcc_status = FCC_STATUS_INVALID_PARAMETER, exit, "Wrong cert name");
00122     SA_PV_ERR_RECOVERABLE_GOTO_IF((size_of_cert_name == 0), fcc_status = FCC_STATUS_INVALID_PARAMETER, exit, "Wrong cert name size");
00123     SA_PV_ERR_RECOVERABLE_GOTO_IF((attribute_data == NULL), fcc_status = FCC_STATUS_INVALID_PARAMETER, exit, "Wrong attribute data buffer pointer");
00124     SA_PV_ERR_RECOVERABLE_GOTO_IF((attribute_data_size == 0), fcc_status = FCC_STATUS_INVALID_PARAMETER, exit, "Wrong attribute data buffer size");
00125     SA_PV_ERR_RECOVERABLE_GOTO_IF((attribute_act_data_size == NULL), fcc_status = FCC_STATUS_INVALID_PARAMETER, exit, "Wrong attribute_act_data_size pointer");
00126 
00127     //For now we save ca id only for bootstrap server
00128     fcc_status = fcc_get_kcm_data((const uint8_t*)cert_name, size_of_cert_name, KCM_CERTIFICATE_ITEM, &kcm_data, &kcm_data_size);
00129     SA_PV_ERR_RECOVERABLE_GOTO_IF((fcc_status != FCC_STATUS_SUCCESS), fcc_status = fcc_status, exit, "Failed to read cert data");
00130 
00131     //Create device certificate handle
00132     kcm_status = cs_create_handle_from_der_x509_cert(kcm_data, kcm_data_size, &x509_cert);
00133     SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS), fcc_status = FCC_STATUS_INVALID_CERTIFICATE, exit, "Failed to get device certificate descriptor");
00134 
00135     //Get certificate attribute data
00136     kcm_status = cs_attr_get_data_x509_cert(x509_cert,
00137         attribute_type,
00138         attribute_data,
00139         attribute_data_size,
00140         attribute_act_data_size);
00141     SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_status != KCM_STATUS_SUCCESS), fcc_status = FCC_STATUS_INVALID_CERT_ATTRIBUTE, exit, "Failed to get attribute data");
00142 
00143 
00144 exit:
00145     fcc_free(kcm_data);
00146     cs_close_handle_x509_cert(&x509_cert);
00147     SA_PV_LOG_TRACE_FUNC_EXIT_NO_ARGS();
00148     return fcc_status;
00149 }