Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

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