Mbed Cloud example program for workshop in W27 2018.

Dependencies:   MMA7660 LM75B

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fcc_bundle_key_utils.c Source File

fcc_bundle_key_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 "fcc_bundle_handler.h"
00018 #include "cn-cbor.h"
00019 #include "pv_error_handling.h"
00020 #include "fcc_bundle_utils.h"
00021 #include "key_config_manager.h"
00022 #include "fcc_output_info_handler.h"
00023 #include "general_utils.h"
00024 #include "fcc_time_profiling.h"
00025 
00026 #define  FCC_MAX_PEM_KEY_SIZE 1024*2
00027 /**
00028 * Names of key types
00029 */
00030 #define FCC_ECC_PRIVATE_KEY_TYPE_NAME  "ECCPrivate"
00031 #define FCC_ECC_PUBLIC_KEY_TYPE_NAME   "ECCPublic"
00032 #define FCC_RSA_PRIVATE_KEY_TYPE_NAME  "RSAPrivate"
00033 #define FCC_RSA_PUBLIC_KEY_TYPE_NAME   "RSAPublic"
00034 #define FCC_SYMMETRIC_KEY_TYPE_NAME    "Symmetric"
00035 /**
00036 * Group lookup record, correlating group's type and name
00037 */
00038 typedef struct fcc_bundle_key_type_lookup_record_ {
00039     fcc_bundle_key_type_e key_type;
00040     const char *key_type_name;
00041 } fcc_bundle_key_type_lookup_record_s;
00042 /**
00043 * Group lookup table, correlating for each group its type and name
00044 */
00045 static const fcc_bundle_key_type_lookup_record_s fcc_bundle_key_type_lookup_table[FCC_MAX_KEY_TYPE] = {
00046     { FCC_ECC_PRIVATE_KEY_TYPE,          FCC_ECC_PRIVATE_KEY_TYPE_NAME },
00047     { FCC_ECC_PUBLIC_KEY_TYPE,           FCC_ECC_PUBLIC_KEY_TYPE_NAME },
00048     { FCC_RSA_PRIVATE_KEY_TYPE,          FCC_RSA_PRIVATE_KEY_TYPE_NAME },
00049     { FCC_RSA_PUBLIC_KEY_TYPE,           FCC_RSA_PUBLIC_KEY_TYPE_NAME },
00050     { FCC_SYM_KEY_TYPE,                  FCC_SYMMETRIC_KEY_TYPE_NAME }
00051 };
00052 /**  Gets type of key form cbor structure
00053 *
00054 * The function goes over all key types and compares it with type inside cbor structure.
00055 *
00056 * @param key_type_cb[in]   The cbor structure with key type data.
00057 * @param key_type[out]     The key type
00058 *
00059 * @return
00060 *     true for success, false otherwise.
00061 */
00062 bool fcc_bundle_get_key_type(const cn_cbor *key_type_cb, fcc_bundle_key_type_e *key_type)
00063 {
00064 
00065     int key_type_index;
00066     bool res;
00067 
00068     SA_PV_LOG_TRACE_FUNC_ENTER_NO_ARGS();
00069 
00070     SA_PV_ERR_RECOVERABLE_RETURN_IF((key_type_cb == NULL), false, "key_type_cb is null");
00071     SA_PV_ERR_RECOVERABLE_RETURN_IF((key_type == NULL), false, "key_type is null");
00072     SA_PV_ERR_RECOVERABLE_RETURN_IF((*key_type != FCC_INVALID_KEY_TYPE), false, "wrong key type value");
00073 
00074     for (key_type_index = 0; key_type_index < FCC_MAX_KEY_TYPE -1; key_type_index++) {
00075         res = is_memory_equal(fcc_bundle_key_type_lookup_table[key_type_index].key_type_name,
00076                              strlen(fcc_bundle_key_type_lookup_table[key_type_index].key_type_name),
00077                              key_type_cb->v.bytes,
00078                              (size_t)key_type_cb->length);
00079         if (res) {
00080             *key_type = fcc_bundle_key_type_lookup_table[key_type_index].key_type;
00081             return true;
00082         }
00083     }
00084     SA_PV_LOG_TRACE_FUNC_EXIT("key_type is %d", (int)(*key_type));
00085     return false;
00086 
00087 }
00088 
00089 /** Processes  keys list.
00090 * The function extracts data parameters for each key and stores its according to it type.
00091 *
00092 * @param keys_list_cb[in]   The cbor structure with keys list.
00093 *
00094 * @return
00095 *     true for success, false otherwise.
00096 */
00097 fcc_status_e fcc_bundle_process_keys(const cn_cbor *keys_list_cb)
00098 {
00099 
00100     bool status = false;
00101     fcc_status_e fcc_status = FCC_STATUS_SUCCESS;
00102     fcc_status_e output_info_fcc_status = FCC_STATUS_SUCCESS;
00103     kcm_status_e kcm_result = KCM_STATUS_SUCCESS;
00104     uint32_t key_index = 0;
00105     cn_cbor *key_cb;
00106     fcc_bundle_data_param_s key;
00107 
00108     SA_PV_LOG_TRACE_FUNC_ENTER_NO_ARGS();
00109     SA_PV_ERR_RECOVERABLE_RETURN_IF((keys_list_cb == NULL), fcc_status = FCC_STATUS_INVALID_PARAMETER, "Invalid keys_list_cb pointer");
00110 
00111     //Initialize data struct
00112     memset(&key,0,sizeof(fcc_bundle_data_param_s));
00113 
00114     for (key_index = 0; key_index < (uint32_t)keys_list_cb->length; key_index++) {
00115 
00116         FCC_SET_START_TIMER(fcc_key_timer);
00117 
00118         fcc_bundle_clean_and_free_data_param(&key);
00119 
00120         //Get key CBOR struct at index key_index
00121         key_cb = cn_cbor_index(keys_list_cb, key_index);
00122         SA_PV_ERR_RECOVERABLE_RETURN_IF((key_cb == NULL), fcc_status = FCC_STATUS_BUNDLE_ERROR, "Failed to get key at index (%" PRIu32 ") ", key_index);
00123         SA_PV_ERR_RECOVERABLE_RETURN_IF((key_cb->type != CN_CBOR_MAP), fcc_status = FCC_STATUS_BUNDLE_ERROR, "Wrong type of key CBOR struct at index (%" PRIu32 ")", key_index);
00124 
00125         status = fcc_bundle_get_data_param(key_cb, &key);
00126         SA_PV_ERR_RECOVERABLE_RETURN_IF((status != true), fcc_status = FCC_STATUS_BUNDLE_ERROR, "Failed to get key data at index (%" PRIu32 ") ", key_index);
00127 
00128         switch (key.type) {
00129             case FCC_ECC_PRIVATE_KEY_TYPE:
00130             case FCC_RSA_PRIVATE_KEY_TYPE:
00131                 kcm_result = kcm_item_store(key.name, key.name_len, KCM_PRIVATE_KEY_ITEM, true, key.data, key.data_size, key.acl);
00132                 SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_result != KCM_STATUS_SUCCESS), fcc_status = FCC_STATUS_KCM_ERROR, exit, "Failed to store key private at index (%" PRIu32 ") ", key_index);
00133                 break;
00134 
00135             case FCC_ECC_PUBLIC_KEY_TYPE:
00136             case FCC_RSA_PUBLIC_KEY_TYPE:
00137                 kcm_result = kcm_item_store(key.name, key.name_len, KCM_PUBLIC_KEY_ITEM, true, key.data, key.data_size, key.acl);
00138                 SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_result != KCM_STATUS_SUCCESS), fcc_status = FCC_STATUS_KCM_ERROR, exit, "Failed to store key public at index (%" PRIu32 ") ", key_index);
00139                 break;
00140 
00141             case (FCC_SYM_KEY_TYPE):
00142                 kcm_result = kcm_item_store(key.name, key.name_len, KCM_SYMMETRIC_KEY_ITEM, true, key.data, key.data_size, key.acl);
00143                 SA_PV_ERR_RECOVERABLE_GOTO_IF((kcm_result != KCM_STATUS_SUCCESS), fcc_status = FCC_STATUS_KCM_ERROR, exit, "Failed to store symmetric key at index (%" PRIu32 ") ", key_index);
00144                 break;
00145             default:
00146                 SA_PV_LOG_ERR("Invalid key type (%u)!", key.type);
00147                 goto exit;
00148         }
00149         FCC_END_TIMER((char*)key.name, key.name_len, fcc_key_timer);
00150     }
00151 
00152 exit:
00153     if (kcm_result != KCM_STATUS_SUCCESS) {
00154         output_info_fcc_status = fcc_bundle_store_error_info(key.name, key.name_len, kcm_result);
00155         SA_PV_ERR_RECOVERABLE_RETURN_IF((output_info_fcc_status != FCC_STATUS_SUCCESS),
00156                                         fcc_status = FCC_STATUS_OUTPUT_INFO_ERROR,
00157                                         "Failed to create output kcm_status error %d", kcm_result);
00158     }
00159     fcc_bundle_clean_and_free_data_param(&key);
00160     SA_PV_LOG_TRACE_FUNC_EXIT_NO_ARGS();
00161     return fcc_status;
00162 }