Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fcc_bundle_utils.h Source File

fcc_bundle_utils.h

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 #ifndef __FCC_BUNDLE_UTILS_H__
00017 #define __FCC_BUNDLE_UTILS_H__
00018 
00019 #include <stdlib.h>
00020 #include <stdbool.h>
00021 #include <inttypes.h>
00022 #include "fcc_status.h"
00023 #include "key_config_manager.h"
00024 #include "cn-cbor.h"
00025 #include "fcc_bundle_fields.h"
00026 
00027 #ifdef __cplusplus
00028 extern "C" {
00029 #endif
00030 
00031 #define FCC_CBOR_MAP_LENGTH 2
00032 #define CSR_MAX_NUMBER_OF_CSRS 5
00033 
00034     /**
00035 * Types of key parameters
00036 */
00037 typedef enum {
00038     FCC_BUNDLE_DATA_PARAM_NAME_TYPE,
00039     FCC_BUNDLE_DATA_PARAM_SCHEME_TYPE,
00040     FCC_BUNDLE_DATA_PARAM_FORMAT_TYPE,
00041     FCC_BUNDLE_DATA_PARAM_DATA_TYPE,
00042     FCC_BUNDLE_DATA_PARAM_ACL_TYPE,
00043     FCC_BUNDLE_DATA_PARAM_ARRAY_TYPE,
00044     FCC_BUNDLE_DATA_PARAMETER_PRIVATE_KEY_NAME_TYPE,
00045     FCC_BUNDLE_DATA_PARAM_MAX_TYPE
00046 } fcc_bundle_data_param_type_e;
00047 
00048 /**
00049 * Key lookup record, correlating key's param type and name
00050 */
00051 typedef struct fcc_bundle_data_param_lookup_record_ {
00052     fcc_bundle_data_param_type_e data_param_type;
00053     const char *data_param_name;
00054 } fcc_bundle_data_param_lookup_record_s;
00055 
00056 /**
00057 * Key lookup table, correlating for each key its param type and param name
00058 */
00059 static const fcc_bundle_data_param_lookup_record_s fcc_bundle_data_param_lookup_table[FCC_BUNDLE_DATA_PARAM_MAX_TYPE] = {
00060     { FCC_BUNDLE_DATA_PARAM_NAME_TYPE,          FCC_BUNDLE_DATA_PARAMETER_NAME },
00061     { FCC_BUNDLE_DATA_PARAM_SCHEME_TYPE,        FCC_BUNDLE_DATA_PARAMETER_SCHEME },
00062     { FCC_BUNDLE_DATA_PARAM_FORMAT_TYPE,        FCC_BUNDLE_DATA_PARAMETER_FORMAT },
00063     { FCC_BUNDLE_DATA_PARAM_DATA_TYPE,          FCC_BUNDLE_DATA_PARAMETER_DATA },
00064     { FCC_BUNDLE_DATA_PARAM_ACL_TYPE,           FCC_BUNDLE_DATA_PARAMETER_ACL },
00065     { FCC_BUNDLE_DATA_PARAM_ARRAY_TYPE,         FCC_BUNDLE_DATA_PARAMETER_ARRAY },
00066     { FCC_BUNDLE_DATA_PARAMETER_PRIVATE_KEY_NAME_TYPE, FCC_BUNDLE_DATA_PARAMETER_PRIVATE_KEY_NAME }
00067 };
00068 
00069 /**
00070 * Source type of buffer
00071 */
00072 typedef enum {
00073     FCC_EXTERNAL_BUFFER_TYPE,
00074     FCC_INTERNAL_BUFFER_TYPE,
00075     FCC_MAX_BUFFER_TYPE
00076 } fcc_bundle_buffer_type_e;
00077 
00078 /**
00079 * Data formats supported by FC
00080 */
00081 typedef enum {
00082     FCC_INVALID_DATA_FORMAT,
00083     FCC_DER_DATA_FORMAT,
00084     FCC_PEM_DATA_FORMAT,
00085     FCC_MAX_DATA_FORMAT
00086 } fcc_bundle_data_format_e;
00087 
00088 /**
00089 * Group lookup record, correlating group's type and name
00090 */
00091 typedef struct fcc_bundle_data_format_lookup_record_ {
00092     fcc_bundle_data_format_e data_format_type;
00093     const char *data_format_name;
00094 } fcc_bundle_data_format_lookup_record_s;
00095 
00096 /**
00097 * Group lookup table, correlating for each group its type and name
00098 */
00099 static const fcc_bundle_data_format_lookup_record_s fcc_bundle_data_format_lookup_table[FCC_MAX_DATA_FORMAT] = {
00100     { FCC_DER_DATA_FORMAT,          FCC_BUNDLE_DER_DATA_FORMAT_NAME },
00101     { FCC_PEM_DATA_FORMAT,          FCC_BUNDLE_PEM_DATA_FORMAT_NAME },
00102 };
00103 
00104 /**
00105 * Key types supported by FC
00106 */
00107 typedef enum {
00108     FCC_INVALID_KEY_TYPE,
00109     FCC_ECC_PRIVATE_KEY_TYPE,//do not change this type's place.FCC_ECC_PRIVATE_KEY_TYPE should be at first place.
00110     FCC_ECC_PUBLIC_KEY_TYPE,
00111     FCC_RSA_PRIVATE_KEY_TYPE,
00112     FCC_RSA_PUBLIC_KEY_TYPE,
00113     FCC_SYM_KEY_TYPE,
00114     FCC_MAX_KEY_TYPE
00115 } fcc_bundle_key_type_e;
00116 
00117 typedef struct fcc_bundle_data_param_ {
00118     uint8_t                          *name;
00119     size_t                           name_len;
00120     fcc_bundle_data_format_e         format;
00121     fcc_bundle_key_type_e            type;
00122     uint8_t                          *data;
00123     size_t                           data_size;
00124     uint8_t                          *data_der;
00125     size_t                           data_der_size;
00126     fcc_bundle_buffer_type_e         data_type;
00127     uint8_t                          *acl;
00128     size_t                           acl_size;
00129     cn_cbor                          *array_cn;
00130     uint8_t                           *private_key_name;
00131     size_t                           private_key_name_len;
00132 } fcc_bundle_data_param_s;
00133 
00134 typedef enum {
00135     FCC_BUNDLE_BUFFER_TYPE_ENTROPY,
00136     FCC_BUNDLE_BUFFER_TYPE_ROT
00137 } fcc_bundle_data_buffer_type_e;
00138 
00139 /** Frees all allocated memory of data parameter struct and sets initial values.
00140 *
00141 * @param data_param[in/out]    The data parameter structure
00142 */
00143 void fcc_bundle_clean_and_free_data_param(fcc_bundle_data_param_s *data_param);
00144 
00145 /** Gets data buffer from cbor struct.
00146 *
00147 * @param data_cb[in]          The cbor text structure
00148 * @param out_data_buffer[out] The out buffer for string data
00149 * @param out_size[out]        The actual size of output buffer
00150 *
00151 * @return
00152 *     true for success, false otherwise.
00153 */
00154 bool get_data_buffer_from_cbor(const cn_cbor *data_cb, uint8_t **out_data_buffer, size_t *out_size);
00155 
00156 /** Processes  keys list.
00157 * The function extracts data parameters for each key and stores its according to it type.
00158 *
00159 * @param keys_list_cb[in]   The cbor structure with keys list.
00160 *
00161 * @return
00162 *     fcc_status_e status.
00163 */
00164 fcc_status_e  fcc_bundle_process_keys(const cn_cbor *keys_list_cb);
00165 
00166 /** Processes  certificate list.
00167 * The function extracts data parameters for each certificate and stores it.
00168 *
00169 * @param certs_list_cb[in]   The cbor structure with certificate list.
00170 *
00171 * @return
00172 *      fcc_status_e status.
00173 */
00174 fcc_status_e  fcc_bundle_process_certificates(const cn_cbor *certs_list_cb);
00175 /** Processes  certificate chain list.
00176 * The function extracts data parameters for each certificate chain and stores it.
00177 *
00178 * @param certs_list_cb[in]   The cbor structure with certificate chain list.
00179 *
00180 * @return
00181 *      fcc_status_e status.
00182 */
00183 fcc_status_e  fcc_bundle_process_certificate_chains(const cn_cbor *cert_chains_list_cb);
00184 
00185 /** Processes  configuration parameters list.
00186 * The function extracts data parameters for each config param and stores it.
00187 *
00188 * @param config_params_list_cb[in]   The cbor structure with config param list.
00189 *
00190 * @return
00191 *      fcc_status_e status.
00192 */
00193 fcc_status_e  fcc_bundle_process_config_params(const cn_cbor *config_params_list_cb);
00194 
00195 /** Gets data parameters.
00196 *
00197 * The function goes over all existing parameters (name,type,format,data,acl and etc) and
00198 * tries to find correlating parameter in cbor structure and saves it to data parameter structure.
00199 *
00200 * @param data_param_cb[in]   The cbor structure with relevant data parameters.
00201 * @param data_param[out]     The data parameter structure
00202 *
00203 * @return
00204 *     true for success, false otherwise.
00205 */
00206 bool fcc_bundle_get_data_param(const cn_cbor *data_param_list_cb, fcc_bundle_data_param_s *data_param);
00207 
00208 /**  Gets type of key form cbor structure
00209 *
00210 * The function goes over all key types and compares it with type inside cbor structure.
00211 *
00212 * @param key_type_cb[in]   The cbor structure with key type data.
00213 * @param key_type[out]     The key type
00214 *
00215 * @return
00216 *     true for success, false otherwise.
00217 */
00218 bool fcc_bundle_get_key_type(const cn_cbor *key_type_cb, fcc_bundle_key_type_e *key_type);
00219 
00220 /** Writes buffer to SOTP
00221 *
00222 * @param cbor_bytes[in]     The pointer to a cn_cbor object of type CN_CBOR_BYTES.
00223 * @param rbp_item_name[in]  Item name to be stored.
00224 * @param buffer_type        Buffer type. different types are stored in a different way.
00225 * @return
00226 *     true for success, false otherwise.
00227 */
00228 
00229 fcc_status_e  fcc_bundle_process_buffer(cn_cbor *cbor_bytes, const char *rbp_item_name, fcc_bundle_data_buffer_type_e buffer_type);
00230 
00231 /** Gets the status groups value
00232 *
00233 * - if value is '0' - set status to false
00234 * - if value is '1' - set status to true
00235 *
00236 * @param cbor_blob[in]             The pointer to main CBOR blob.
00237 * @param cbor_group_name[in]       CBOT group name.
00238 * @param cbor_group_name_size[in]  CBOR group name size .
00239 * @param fcc_field_status[out]     Status of the field.
00240 *
00241 * @return
00242 *     One of FCC_STATUS_* error codes
00243 */
00244 fcc_status_e  bundle_process_status_field(const cn_cbor *cbor_blob, char *cbor_group_name, size_t cbor_group_name_size, bool *fcc_field_status);
00245 
00246 /** The function sets factory disable flag to sotp.
00247 *
00248 * @return
00249 *     One of FCC_STATUS_* error codes
00250 */
00251 fcc_status_e  fcc_bundle_factory_disable(void);
00252 
00253 /** Process the the CSR requests from the incoming message, generate the keys (and store them) and CSRs, and append the CSRs to the encoder in the proper format.
00254 * 
00255 *
00256 * @param csrs_list_cb[in]   The pointer to a cn_cbor object of type CN_CBOR_ARRAY which is an array of CSR request maps.
00257 * @param response_encoder[in/out]    encoder that points to the response map.
00258 * @return
00259 *     One of FCC_STATUS_* error codes
00260 */
00261 fcc_status_e  fcc_bundle_process_csrs(const cn_cbor *csrs_list_cb, cn_cbor *response_encoder);
00262 
00263 #ifdef __cplusplus
00264 }
00265 #endif
00266 
00267 #endif //__FCC_BUNDLE_UTILS_H__