From fb34bcd008cf4185f455230b980c7b01fd0a235d Mon Sep 17 00:00:00 2001 From: Sukru Senli Date: Sun, 26 Apr 2020 09:24:56 +0200 Subject: [PATCH] security: collect certificates by section type instead of specific section name --- dmtree/tr181/security.c | 88 ++++++++++++++++++++++----- libbbf_api/dmcommon.c | 130 +++++++++++++--------------------------- libbbf_api/dmcommon.h | 5 +- 3 files changed, 116 insertions(+), 107 deletions(-) diff --git a/dmtree/tr181/security.c b/dmtree/tr181/security.c index 11d39fb8..40d086b2 100644 --- a/dmtree/tr181/security.c +++ b/dmtree/tr181/security.c @@ -11,6 +11,9 @@ #include "security.h" #define DATE_LEN 128 +#define MAX_CERT 32 + +static char certifcates_paths[MAX_CERT][256]; struct certificate_profile { char *path; @@ -152,19 +155,72 @@ static char *get_certificate_pk(mbedtls_pk_type_t sig_pk) /************************************************************* * ENTRY METHOD **************************************************************/ + +static void get_certificate_paths(void) +{ + struct uci_section *s; + int cidx; + + for (cidx=0; cidx= MAX_CERT) + break; + if(!file_exists(cert) && is_regular_file(cert)) + continue; + strncpy(certifcates_paths[cidx], cert, 256); + cidx++; + } + + uci_foreach_sections("openvpn", "openvpn", s) { + char *cert; + dmuci_get_value_by_section_string(s, "cert", &cert); + if (*cert == '\0') + continue; + if (cidx >= MAX_CERT) + break; + if(!file_exists(cert) && is_regular_file(cert)) + continue; + strncpy(certifcates_paths[cidx], cert, 256); + cidx++; + } + + uci_foreach_sections("obuspa", "obuspa", s) { + char *cert; + dmuci_get_value_by_section_string(s, "cert", &cert); + if (*cert == '\0') + continue; + if (cidx >= MAX_CERT) + break; + if(!file_exists(cert) && is_regular_file(cert)) + continue; + strncpy(certifcates_paths[cidx], cert, 256); + cidx++; + } +} + static int browseSecurityCertificateInst(struct dmctx *dmctx, DMNODE *parent_node, void *prev_data, char *prev_instance) { #if defined(LOPENSSL) || defined(LMBEDTLS) - char **certifcates_paths; - int length, i; char *cert_inst= NULL, *cert_inst_last= NULL, *v = NULL; struct uci_section *dmmap_sect = NULL; struct certificate_profile certificateprofile = {}; - certifcates_paths = get_all_iop_certificates(&length); check_create_dmmap_package("dmmap_security"); - for (i=0; i 0) { - DIR *dir; - struct dirent *ent; - if ((dir = opendir(dirpath)) == NULL) - continue; - while ((ent = readdir (dir)) != NULL) { - if (ent->d_name[0] == '.' || is_string_exist_in_str_array(certificates_paths, j, dirpath, ent->d_name)) - continue; - dmasprintf(&certificates_paths[j],"%s%s", dirpath, ent->d_name); - j++; - } - closedir(dir); - dmfree(dirpath); - dirpath = NULL; - } - } - *length = j; - return certificates_paths; -} - char *stringToHex(char *text, int length) { - char *hex = NULL; - int i, j; - hex = (char *)dmcalloc(100, sizeof(char)); + char *hex = NULL; + int i, j; - for (i = 0, j = 0; i < length; ++i, j += 3) { - sprintf(hex + j, "%02x", text[i] & 0xff); - if (i < length-1) - sprintf(hex + j + 2, "%c", ':'); - } - return hex; + hex = (char *)dmcalloc(100, sizeof(char)); + + for (i = 0, j = 0; i < length; ++i, j += 3) { + sprintf(hex + j, "%02x", text[i] & 0xff); + if (i < length-1) + sprintf(hex + j + 2, "%c", ':'); + } + return hex; } char *replace_char(char *str, char find, char replace) { - char *current_pos = strchr(str, find); - while (current_pos) { - *current_pos = replace; - current_pos = strchr(current_pos, find); - } - return str; + char *current_pos = strchr(str, find); + while (current_pos) { + *current_pos = replace; + current_pos = strchr(current_pos, find); + } + return str; } int is_vlan_termination_section(char *name) diff --git a/libbbf_api/dmcommon.h b/libbbf_api/dmcommon.h index 8352e36e..db2a0f60 100644 --- a/libbbf_api/dmcommon.h +++ b/libbbf_api/dmcommon.h @@ -326,8 +326,9 @@ int dm_validate_dateTime(char *value); int dm_validate_hexBinary(char *value, struct range_args r_args[], int r_args_size); int dm_validate_string_list(char *value, int min_item, int max_item, int max_size, int min, int max, char *enumeration[], int enumeration_size, char *pattern[], int pattern_size); int dm_validate_unsignedInt_list(char *value, int min_item, int max_item, int max_size, struct range_args r_args[], int r_args_size); -char **get_all_iop_certificates(int *length); -char *decode64 (char *enc); +char *decode64(char *enc); +bool file_exists(const char *path); +int is_regular_file(const char *path); char *stringToHex(char *text, int length); char *replace_char(char *str, char find, char replace); int is_vlan_termination_section(char *name);