diff options
Diffstat (limited to 'lib/minitasn1/structure.c')
-rw-r--r-- | lib/minitasn1/structure.c | 1736 |
1 files changed, 858 insertions, 878 deletions
diff --git a/lib/minitasn1/structure.c b/lib/minitasn1/structure.c index 31a5f654bb..567c5cec67 100644 --- a/lib/minitasn1/structure.c +++ b/lib/minitasn1/structure.c @@ -44,18 +44,17 @@ extern char _asn1_identifierMissing[]; /* and CONST_ constants). */ /* Return: pointer to the new element. */ /******************************************************/ -asn1_node -_asn1_add_single_node (unsigned int type) +asn1_node _asn1_add_single_node(unsigned int type) { - asn1_node punt; + asn1_node punt; - punt = calloc (1, sizeof (struct asn1_node_st)); - if (punt == NULL) - return NULL; + punt = calloc(1, sizeof(struct asn1_node_st)); + if (punt == NULL) + return NULL; - punt->type = type; + punt->type = type; - return punt; + return punt; } @@ -67,93 +66,84 @@ _asn1_add_single_node (unsigned int type) /* node: NODE_ASN element pointer. */ /* Return: NULL if not found. */ /******************************************************************/ -asn1_node -_asn1_find_left (asn1_node node) +asn1_node _asn1_find_left(asn1_node node) { - if ((node == NULL) || (node->left == NULL) || (node->left->down == node)) - return NULL; + if ((node == NULL) || (node->left == NULL) + || (node->left->down == node)) + return NULL; - return node->left; + return node->left; } int -_asn1_create_static_structure (asn1_node pointer, char *output_file_name, - char *vector_name) +_asn1_create_static_structure(asn1_node pointer, char *output_file_name, + char *vector_name) { - FILE *file; - asn1_node p; - unsigned long t; + FILE *file; + asn1_node p; + unsigned long t; - file = fopen (output_file_name, "w"); + file = fopen(output_file_name, "w"); - if (file == NULL) - return ASN1_FILE_NOT_FOUND; + if (file == NULL) + return ASN1_FILE_NOT_FOUND; - fprintf (file, "#if HAVE_CONFIG_H\n"); - fprintf (file, "# include \"config.h\"\n"); - fprintf (file, "#endif\n\n"); + fprintf(file, "#if HAVE_CONFIG_H\n"); + fprintf(file, "# include \"config.h\"\n"); + fprintf(file, "#endif\n\n"); - fprintf (file, "#include <libtasn1.h>\n\n"); + fprintf(file, "#include <libtasn1.h>\n\n"); - fprintf (file, "const asn1_static_node %s[] = {\n", vector_name); + fprintf(file, "const asn1_static_node %s[] = {\n", vector_name); - p = pointer; + p = pointer; - while (p) - { - fprintf (file, " { "); + while (p) { + fprintf(file, " { "); - if (p->name[0] != 0) - fprintf (file, "\"%s\", ", p->name); - else - fprintf (file, "NULL, "); + if (p->name[0] != 0) + fprintf(file, "\"%s\", ", p->name); + else + fprintf(file, "NULL, "); - t = p->type; - if (p->down) - t |= CONST_DOWN; - if (p->right) - t |= CONST_RIGHT; + t = p->type; + if (p->down) + t |= CONST_DOWN; + if (p->right) + t |= CONST_RIGHT; - fprintf (file, "%lu, ", t); + fprintf(file, "%lu, ", t); - if (p->value) - fprintf (file, "\"%s\"},\n", p->value); - else - fprintf (file, "NULL },\n"); - - if (p->down) - { - p = p->down; - } - else if (p->right) - { - p = p->right; - } - else - { - while (1) - { - p = _asn1_find_up (p); - if (p == pointer) - { - p = NULL; - break; - } - if (p->right) - { - p = p->right; - break; + if (p->value) + fprintf(file, "\"%s\"},\n", p->value); + else + fprintf(file, "NULL },\n"); + + if (p->down) { + p = p->down; + } else if (p->right) { + p = p->right; + } else { + while (1) { + p = _asn1_find_up(p); + if (p == pointer) { + p = NULL; + break; + } + if (p->right) { + p = p->right; + break; + } + } } - } } - } - fprintf (file, " { NULL, 0, NULL }\n};\n"); + fprintf(file, " { NULL, 0, NULL }\n};\n"); - fclose (file); + fclose(file); - return ASN1_SUCCESS; + return ASN1_SUCCESS; } @@ -174,104 +164,92 @@ _asn1_create_static_structure (asn1_node pointer, char *output_file_name, * %ASN1_ARRAY_ERROR if the array pointed by @array is wrong. **/ int -asn1_array2tree (const asn1_static_node * array, asn1_node * definitions, - char *errorDescription) +asn1_array2tree(const asn1_static_node * array, asn1_node * definitions, + char *errorDescription) { - asn1_node p, p_last = NULL; - unsigned long k; - int move; - int result; - unsigned int type; - - - if (*definitions != NULL) - return ASN1_ELEMENT_NOT_EMPTY; - - move = UP; - - k = 0; - while (array[k].value || array[k].type || array[k].name) - { - type = convert_old_type(array[k].type); - - p = _asn1_add_static_node (type & (~CONST_DOWN)); - if (array[k].name) - _asn1_set_name (p, array[k].name); - if (array[k].value) - _asn1_set_value (p, array[k].value, strlen (array[k].value) + 1); - - if (*definitions == NULL) - *definitions = p; - - if (move == DOWN) - _asn1_set_down (p_last, p); - else if (move == RIGHT) - _asn1_set_right (p_last, p); - - p_last = p; - - if (type & CONST_DOWN) - move = DOWN; - else if (type & CONST_RIGHT) - move = RIGHT; - else - { - while (1) - { - if (p_last == *definitions) - break; - - p_last = _asn1_find_up (p_last); - - if (p_last == NULL) - break; - - if (p_last->type & CONST_RIGHT) - { - p_last->type &= ~CONST_RIGHT; - move = RIGHT; - break; + asn1_node p, p_last = NULL; + unsigned long k; + int move; + int result; + unsigned int type; + + + if (*definitions != NULL) + return ASN1_ELEMENT_NOT_EMPTY; + + move = UP; + + k = 0; + while (array[k].value || array[k].type || array[k].name) { + type = convert_old_type(array[k].type); + + p = _asn1_add_static_node(type & (~CONST_DOWN)); + if (array[k].name) + _asn1_set_name(p, array[k].name); + if (array[k].value) + _asn1_set_value(p, array[k].value, + strlen(array[k].value) + 1); + + if (*definitions == NULL) + *definitions = p; + + if (move == DOWN) + _asn1_set_down(p_last, p); + else if (move == RIGHT) + _asn1_set_right(p_last, p); + + p_last = p; + + if (type & CONST_DOWN) + move = DOWN; + else if (type & CONST_RIGHT) + move = RIGHT; + else { + while (1) { + if (p_last == *definitions) + break; + + p_last = _asn1_find_up(p_last); + + if (p_last == NULL) + break; + + if (p_last->type & CONST_RIGHT) { + p_last->type &= ~CONST_RIGHT; + move = RIGHT; + break; + } + } /* while */ } - } /* while */ - } - k++; - } /* while */ - - if (p_last == *definitions) - { - result = _asn1_check_identifier (*definitions); - if (result == ASN1_SUCCESS) - { - _asn1_change_integer_value (*definitions); - _asn1_expand_object_id (*definitions); + k++; + } /* while */ + + if (p_last == *definitions) { + result = _asn1_check_identifier(*definitions); + if (result == ASN1_SUCCESS) { + _asn1_change_integer_value(*definitions); + _asn1_expand_object_id(*definitions); + } + } else { + result = ASN1_ARRAY_ERROR; } - } - else - { - result = ASN1_ARRAY_ERROR; - } - - if (errorDescription != NULL) - { - if (result == ASN1_IDENTIFIER_NOT_FOUND) - { - Estrcpy (errorDescription, ":: identifier '"); - Estrcat (errorDescription, _asn1_identifierMissing); - Estrcat (errorDescription, "' not found"); + + if (errorDescription != NULL) { + if (result == ASN1_IDENTIFIER_NOT_FOUND) { + Estrcpy(errorDescription, ":: identifier '"); + Estrcat(errorDescription, _asn1_identifierMissing); + Estrcat(errorDescription, "' not found"); + } else + errorDescription[0] = 0; } - else - errorDescription[0] = 0; - } - - if (result != ASN1_SUCCESS) - { - _asn1_delete_list_and_nodes (); - *definitions = NULL; - } - else - _asn1_delete_list (); - - return result; + + if (result != ASN1_SUCCESS) { + _asn1_delete_list_and_nodes(); + *definitions = NULL; + } else + _asn1_delete_list(); + + return result; } /** @@ -284,55 +262,45 @@ asn1_array2tree (const asn1_static_node * array, asn1_node * definitions, * Returns: %ASN1_SUCCESS if successful, %ASN1_ELEMENT_NOT_FOUND if * *@structure was NULL. **/ -int -asn1_delete_structure (asn1_node * structure) +int asn1_delete_structure(asn1_node * structure) { - asn1_node p, p2, p3; - - if (*structure == NULL) - return ASN1_ELEMENT_NOT_FOUND; - - p = *structure; - while (p) - { - if (p->down) - { - p = p->down; - } - else - { /* no down */ - p2 = p->right; - if (p != *structure) - { - p3 = _asn1_find_up (p); - _asn1_set_down (p3, p2); - _asn1_remove_node (p); - p = p3; - } - else - { /* p==root */ - p3 = _asn1_find_left (p); - if (!p3) - { - p3 = _asn1_find_up (p); - if (p3) - _asn1_set_down (p3, p2); - else - { - if (p->right) - p->right->left = NULL; - } + asn1_node p, p2, p3; + + if (*structure == NULL) + return ASN1_ELEMENT_NOT_FOUND; + + p = *structure; + while (p) { + if (p->down) { + p = p->down; + } else { /* no down */ + p2 = p->right; + if (p != *structure) { + p3 = _asn1_find_up(p); + _asn1_set_down(p3, p2); + _asn1_remove_node(p); + p = p3; + } else { /* p==root */ + p3 = _asn1_find_left(p); + if (!p3) { + p3 = _asn1_find_up(p); + if (p3) + _asn1_set_down(p3, p2); + else { + if (p->right) + p->right->left = + NULL; + } + } else + _asn1_set_right(p3, p2); + _asn1_remove_node(p); + p = NULL; + } } - else - _asn1_set_right (p3, p2); - _asn1_remove_node (p); - p = NULL; - } } - } - *structure = NULL; - return ASN1_SUCCESS; + *structure = NULL; + return ASN1_SUCCESS; } @@ -348,291 +316,279 @@ asn1_delete_structure (asn1_node * structure) * Returns: %ASN1_SUCCESS if successful, %ASN1_ELEMENT_NOT_FOUND if * the @element_name was not found. **/ -int -asn1_delete_element (asn1_node structure, const char *element_name) +int asn1_delete_element(asn1_node structure, const char *element_name) { - asn1_node p2, p3, source_node; - - source_node = asn1_find_node (structure, element_name); - - if (source_node == NULL) - return ASN1_ELEMENT_NOT_FOUND; - - p2 = source_node->right; - p3 = _asn1_find_left (source_node); - if (!p3) - { - p3 = _asn1_find_up (source_node); - if (p3) - _asn1_set_down (p3, p2); - else if (source_node->right) - source_node->right->left = NULL; - } - else - _asn1_set_right (p3, p2); - - return asn1_delete_structure (&source_node); + asn1_node p2, p3, source_node; + + source_node = asn1_find_node(structure, element_name); + + if (source_node == NULL) + return ASN1_ELEMENT_NOT_FOUND; + + p2 = source_node->right; + p3 = _asn1_find_left(source_node); + if (!p3) { + p3 = _asn1_find_up(source_node); + if (p3) + _asn1_set_down(p3, p2); + else if (source_node->right) + source_node->right->left = NULL; + } else + _asn1_set_right(p3, p2); + + return asn1_delete_structure(&source_node); } -asn1_node -_asn1_copy_structure3 (asn1_node source_node) +asn1_node _asn1_copy_structure3(asn1_node source_node) { - asn1_node dest_node, p_s, p_d, p_d_prev; - int move; - - if (source_node == NULL) - return NULL; - - dest_node = _asn1_add_single_node (source_node->type); - - p_s = source_node; - p_d = dest_node; - - move = DOWN; - - do - { - if (move != UP) - { - if (p_s->name[0] != 0) - _asn1_cpy_name (p_d, p_s); - if (p_s->value) - _asn1_set_value (p_d, p_s->value, p_s->value_len); - if (p_s->down) - { - p_s = p_s->down; - p_d_prev = p_d; - p_d = _asn1_add_single_node (p_s->type); - _asn1_set_down (p_d_prev, p_d); - continue; - } + asn1_node dest_node, p_s, p_d, p_d_prev; + int move; + + if (source_node == NULL) + return NULL; + + dest_node = _asn1_add_single_node(source_node->type); + + p_s = source_node; + p_d = dest_node; + + move = DOWN; + + do { + if (move != UP) { + if (p_s->name[0] != 0) + _asn1_cpy_name(p_d, p_s); + if (p_s->value) + _asn1_set_value(p_d, p_s->value, + p_s->value_len); + if (p_s->down) { + p_s = p_s->down; + p_d_prev = p_d; + p_d = _asn1_add_single_node(p_s->type); + _asn1_set_down(p_d_prev, p_d); + continue; + } + } + + if (p_s == source_node) + break; + + if (p_s->right) { + move = RIGHT; + p_s = p_s->right; + p_d_prev = p_d; + p_d = _asn1_add_single_node(p_s->type); + _asn1_set_right(p_d_prev, p_d); + } else { + move = UP; + p_s = _asn1_find_up(p_s); + p_d = _asn1_find_up(p_d); + } } + while (p_s != source_node); - if (p_s == source_node) - break; - - if (p_s->right) - { - move = RIGHT; - p_s = p_s->right; - p_d_prev = p_d; - p_d = _asn1_add_single_node (p_s->type); - _asn1_set_right (p_d_prev, p_d); - } - else - { - move = UP; - p_s = _asn1_find_up (p_s); - p_d = _asn1_find_up (p_d); - } - } - while (p_s != source_node); - - return dest_node; + return dest_node; } static asn1_node -_asn1_copy_structure2 (asn1_node root, const char *source_name) +_asn1_copy_structure2(asn1_node root, const char *source_name) { - asn1_node source_node; + asn1_node source_node; - source_node = asn1_find_node (root, source_name); + source_node = asn1_find_node(root, source_name); - return _asn1_copy_structure3 (source_node); + return _asn1_copy_structure3(source_node); } -static int -_asn1_type_choice_config (asn1_node node) +static int _asn1_type_choice_config(asn1_node node) { - asn1_node p, p2, p3, p4; - int move, tlen; - - if (node == NULL) - return ASN1_ELEMENT_NOT_FOUND; - - p = node; - move = DOWN; - - while (!((p == node) && (move == UP))) - { - if (move != UP) - { - if ((type_field (p->type) == ASN1_ETYPE_CHOICE) && (p->type & CONST_TAG)) - { - p2 = p->down; - while (p2) - { - if (type_field (p2->type) != ASN1_ETYPE_TAG) - { - p2->type |= CONST_TAG; - p3 = _asn1_find_left (p2); - while (p3) - { - if (type_field (p3->type) == ASN1_ETYPE_TAG) - { - p4 = _asn1_add_single_node (p3->type); - tlen = _asn1_strlen (p3->value); - if (tlen > 0) - _asn1_set_value (p4, p3->value, tlen + 1); - _asn1_set_right (p4, p2->down); - _asn1_set_down (p2, p4); - } - p3 = _asn1_find_left (p3); + asn1_node p, p2, p3, p4; + int move, tlen; + + if (node == NULL) + return ASN1_ELEMENT_NOT_FOUND; + + p = node; + move = DOWN; + + while (!((p == node) && (move == UP))) { + if (move != UP) { + if ((type_field(p->type) == ASN1_ETYPE_CHOICE) + && (p->type & CONST_TAG)) { + p2 = p->down; + while (p2) { + if (type_field(p2->type) != + ASN1_ETYPE_TAG) { + p2->type |= CONST_TAG; + p3 = _asn1_find_left(p2); + while (p3) { + if (type_field + (p3->type) == + ASN1_ETYPE_TAG) + { + p4 = _asn1_add_single_node(p3->type); + tlen = + _asn1_strlen + (p3-> + value); + if (tlen > + 0) + _asn1_set_value + (p4, + p3-> + value, + tlen + + + 1); + _asn1_set_right + (p4, + p2-> + down); + _asn1_set_down + (p2, + p4); + } + p3 = _asn1_find_left(p3); + } + } + p2 = p2->right; + } + p->type &= ~(CONST_TAG); + p2 = p->down; + while (p2) { + p3 = p2->right; + if (type_field(p2->type) == + ASN1_ETYPE_TAG) + asn1_delete_structure(&p2); + p2 = p3; + } } - } - p2 = p2->right; + move = DOWN; + } else + move = RIGHT; + + if (move == DOWN) { + if (p->down) + p = p->down; + else + move = RIGHT; } - p->type &= ~(CONST_TAG); - p2 = p->down; - while (p2) - { - p3 = p2->right; - if (type_field (p2->type) == ASN1_ETYPE_TAG) - asn1_delete_structure (&p2); - p2 = p3; - } - } - move = DOWN; - } - else - move = RIGHT; - - if (move == DOWN) - { - if (p->down) - p = p->down; - else - move = RIGHT; - } - if (p == node) - { - move = UP; - continue; - } + if (p == node) { + move = UP; + continue; + } - if (move == RIGHT) - { - if (p->right) - p = p->right; - else - move = UP; + if (move == RIGHT) { + if (p->right) + p = p->right; + else + move = UP; + } + if (move == UP) + p = _asn1_find_up(p); } - if (move == UP) - p = _asn1_find_up (p); - } - return ASN1_SUCCESS; + return ASN1_SUCCESS; } -static int -_asn1_expand_identifier (asn1_node * node, asn1_node root) +static int _asn1_expand_identifier(asn1_node * node, asn1_node root) { - asn1_node p, p2, p3; - char name2[ASN1_MAX_NAME_SIZE + 2]; - int move; - - if (node == NULL) - return ASN1_ELEMENT_NOT_FOUND; - - p = *node; - move = DOWN; - - while (!((p == *node) && (move == UP))) - { - if (move != UP) - { - if (type_field (p->type) == ASN1_ETYPE_IDENTIFIER) - { - snprintf(name2, sizeof (name2), "%s.%s", root->name, p->value); - p2 = _asn1_copy_structure2 (root, name2); - if (p2 == NULL) - { - return ASN1_IDENTIFIER_NOT_FOUND; - } - _asn1_cpy_name (p2, p); - p2->right = p->right; - p2->left = p->left; - if (p->right) - p->right->left = p2; - p3 = p->down; - if (p3) - { - while (p3->right) - p3 = p3->right; - _asn1_set_right (p3, p2->down); - _asn1_set_down (p2, p->down); - } + asn1_node p, p2, p3; + char name2[ASN1_MAX_NAME_SIZE + 2]; + int move; - p3 = _asn1_find_left (p); - if (p3) - _asn1_set_right (p3, p2); - else - { - p3 = _asn1_find_up (p); - if (p3) - _asn1_set_down (p3, p2); - else - { - p2->left = NULL; - } - } + if (node == NULL) + return ASN1_ELEMENT_NOT_FOUND; - if (p->type & CONST_SIZE) - p2->type |= CONST_SIZE; - if (p->type & CONST_TAG) - p2->type |= CONST_TAG; - if (p->type & CONST_OPTION) - p2->type |= CONST_OPTION; - if (p->type & CONST_DEFAULT) - p2->type |= CONST_DEFAULT; - if (p->type & CONST_SET) - p2->type |= CONST_SET; - if (p->type & CONST_NOT_USED) - p2->type |= CONST_NOT_USED; - - if (p == *node) - *node = p2; - _asn1_remove_node (p); - p = p2; - move = DOWN; - continue; - } - move = DOWN; - } - else - move = RIGHT; - - if (move == DOWN) - { - if (p->down) - p = p->down; - else - move = RIGHT; - } + p = *node; + move = DOWN; - if (p == *node) - { - move = UP; - continue; - } + while (!((p == *node) && (move == UP))) { + if (move != UP) { + if (type_field(p->type) == ASN1_ETYPE_IDENTIFIER) { + snprintf(name2, sizeof(name2), "%s.%s", + root->name, p->value); + p2 = _asn1_copy_structure2(root, name2); + if (p2 == NULL) { + return ASN1_IDENTIFIER_NOT_FOUND; + } + _asn1_cpy_name(p2, p); + p2->right = p->right; + p2->left = p->left; + if (p->right) + p->right->left = p2; + p3 = p->down; + if (p3) { + while (p3->right) + p3 = p3->right; + _asn1_set_right(p3, p2->down); + _asn1_set_down(p2, p->down); + } + + p3 = _asn1_find_left(p); + if (p3) + _asn1_set_right(p3, p2); + else { + p3 = _asn1_find_up(p); + if (p3) + _asn1_set_down(p3, p2); + else { + p2->left = NULL; + } + } + + if (p->type & CONST_SIZE) + p2->type |= CONST_SIZE; + if (p->type & CONST_TAG) + p2->type |= CONST_TAG; + if (p->type & CONST_OPTION) + p2->type |= CONST_OPTION; + if (p->type & CONST_DEFAULT) + p2->type |= CONST_DEFAULT; + if (p->type & CONST_SET) + p2->type |= CONST_SET; + if (p->type & CONST_NOT_USED) + p2->type |= CONST_NOT_USED; + + if (p == *node) + *node = p2; + _asn1_remove_node(p); + p = p2; + move = DOWN; + continue; + } + move = DOWN; + } else + move = RIGHT; + + if (move == DOWN) { + if (p->down) + p = p->down; + else + move = RIGHT; + } + + if (p == *node) { + move = UP; + continue; + } - if (move == RIGHT) - { - if (p->right) - p = p->right; - else - move = UP; + if (move == RIGHT) { + if (p->right) + p = p->right; + else + move = UP; + } + if (move == UP) + p = _asn1_find_up(p); } - if (move == UP) - p = _asn1_find_up (p); - } - return ASN1_SUCCESS; + return ASN1_SUCCESS; } @@ -652,25 +608,25 @@ _asn1_expand_identifier (asn1_node * node, asn1_node root) * @source_name is not known. **/ int -asn1_create_element (asn1_node definitions, const char *source_name, - asn1_node * element) +asn1_create_element(asn1_node definitions, const char *source_name, + asn1_node * element) { - asn1_node dest_node; - int res; + asn1_node dest_node; + int res; - dest_node = _asn1_copy_structure2 (definitions, source_name); + dest_node = _asn1_copy_structure2(definitions, source_name); - if (dest_node == NULL) - return ASN1_ELEMENT_NOT_FOUND; + if (dest_node == NULL) + return ASN1_ELEMENT_NOT_FOUND; - _asn1_set_name (dest_node, ""); + _asn1_set_name(dest_node, ""); - res = _asn1_expand_identifier (&dest_node, definitions); - _asn1_type_choice_config (dest_node); + res = _asn1_expand_identifier(&dest_node, definitions); + _asn1_type_choice_config(dest_node); - *element = dest_node; + *element = dest_node; - return res; + return res; } @@ -687,331 +643,358 @@ asn1_create_element (asn1_node definitions, const char *source_name, * from the @name element inside the structure @structure. **/ void -asn1_print_structure (FILE * out, asn1_node structure, const char *name, - int mode) +asn1_print_structure(FILE * out, asn1_node structure, const char *name, + int mode) { - asn1_node p, root; - int k, indent = 0, len, len2, len3; - - if (out == NULL) - return; - - root = asn1_find_node (structure, name); - - if (root == NULL) - return; - - p = root; - while (p) - { - if (mode == ASN1_PRINT_ALL) - { - for (k = 0; k < indent; k++) - fprintf (out, " "); - fprintf (out, "name:"); - if (p->name[0] != 0) - fprintf (out, "%s ", p->name); - else - fprintf (out, "NULL "); - } - else - { - switch (type_field (p->type)) - { - case ASN1_ETYPE_CONSTANT: - case ASN1_ETYPE_TAG: - case ASN1_ETYPE_SIZE: - break; - default: - for (k = 0; k < indent; k++) - fprintf (out, " "); - fprintf (out, "name:"); - if (p->name[0] != 0) - fprintf (out, "%s ", p->name); - else - fprintf (out, "NULL "); - } - } - - if (mode != ASN1_PRINT_NAME) - { - unsigned type = type_field (p->type); - switch (type) - { - case ASN1_ETYPE_CONSTANT: - if (mode == ASN1_PRINT_ALL) - fprintf (out, "type:CONST"); - break; - case ASN1_ETYPE_TAG: - if (mode == ASN1_PRINT_ALL) - fprintf (out, "type:TAG"); - break; - case ASN1_ETYPE_SIZE: - if (mode == ASN1_PRINT_ALL) - fprintf (out, "type:SIZE"); - break; - case ASN1_ETYPE_DEFAULT: - fprintf (out, "type:DEFAULT"); - break; - case ASN1_ETYPE_IDENTIFIER: - fprintf (out, "type:IDENTIFIER"); - break; - case ASN1_ETYPE_ANY: - fprintf (out, "type:ANY"); - break; - case ASN1_ETYPE_CHOICE: - fprintf (out, "type:CHOICE"); - break; - case ASN1_ETYPE_DEFINITIONS: - fprintf (out, "type:DEFINITIONS"); - break; - CASE_HANDLED_ETYPES: - fprintf (out, "%s", _asn1_tags[type].desc); - break; - default: - break; - } - } - - if ((mode == ASN1_PRINT_NAME_TYPE_VALUE) || (mode == ASN1_PRINT_ALL)) - { - switch (type_field (p->type)) - { - case ASN1_ETYPE_CONSTANT: - if (mode == ASN1_PRINT_ALL) - if (p->value) - fprintf (out, " value:%s", p->value); - break; - case ASN1_ETYPE_TAG: - if (mode == ASN1_PRINT_ALL) - if (p->value) - fprintf (out, " value:%s", p->value); - break; - case ASN1_ETYPE_SIZE: - if (mode == ASN1_PRINT_ALL) - if (p->value) - fprintf (out, " value:%s", p->value); - break; - case ASN1_ETYPE_DEFAULT: - if (p->value) - fprintf (out, " value:%s", p->value); - else if (p->type & CONST_TRUE) - fprintf (out, " value:TRUE"); - else if (p->type & CONST_FALSE) - fprintf (out, " value:FALSE"); - break; - case ASN1_ETYPE_IDENTIFIER: - if (p->value) - fprintf (out, " value:%s", p->value); - break; - case ASN1_ETYPE_INTEGER: - if (p->value) - { - len2 = -1; - len = asn1_get_length_der (p->value, p->value_len, &len2); - fprintf (out, " value:0x"); - if (len > 0) - for (k = 0; k < len; k++) - fprintf (out, "%02x", (p->value)[k + len2]); - } - break; - case ASN1_ETYPE_ENUMERATED: - if (p->value) - { - len2 = -1; - len = asn1_get_length_der (p->value, p->value_len, &len2); - fprintf (out, " value:0x"); - if (len > 0) - for (k = 0; k < len; k++) - fprintf (out, "%02x", (p->value)[k + len2]); - } - break; - case ASN1_ETYPE_BOOLEAN: - if (p->value) - { - if (p->value[0] == 'T') - fprintf (out, " value:TRUE"); - else if (p->value[0] == 'F') - fprintf (out, " value:FALSE"); - } - break; - case ASN1_ETYPE_BIT_STRING: - if (p->value) - { - len2 = -1; - len = asn1_get_length_der (p->value, p->value_len, &len2); - if (len > 0) - { - fprintf (out, " value(%i):", - (len - 1) * 8 - (p->value[len2])); - for (k = 1; k < len; k++) - fprintf (out, "%02x", (p->value)[k + len2]); - } - } - break; - case ASN1_ETYPE_GENERALIZED_TIME: - case ASN1_ETYPE_UTC_TIME: - if (p->value) - { - fprintf (out, " value:"); - for (k = 0; k < p->value_len; k++) - fprintf (out, "%c", (p->value)[k]); - } - break; - case ASN1_ETYPE_GENERALSTRING: - case ASN1_ETYPE_NUMERIC_STRING: - case ASN1_ETYPE_IA5_STRING: - case ASN1_ETYPE_TELETEX_STRING: - case ASN1_ETYPE_PRINTABLE_STRING: - case ASN1_ETYPE_UNIVERSAL_STRING: - case ASN1_ETYPE_UTF8_STRING: - case ASN1_ETYPE_VISIBLE_STRING: - if (p->value) - { - len2 = -1; - len = asn1_get_length_der (p->value, p->value_len, &len2); - fprintf (out, " value:"); - if (len > 0) - for (k = 0; k < len; k++) - fprintf (out, "%c", (p->value)[k + len2]); - } - break; - case ASN1_ETYPE_BMP_STRING: - case ASN1_ETYPE_OCTET_STRING: - if (p->value) - { - len2 = -1; - len = asn1_get_length_der (p->value, p->value_len, &len2); - fprintf (out, " value:"); - if (len > 0) - for (k = 0; k < len; k++) - fprintf (out, "%02x", (p->value)[k + len2]); + asn1_node p, root; + int k, indent = 0, len, len2, len3; + + if (out == NULL) + return; + + root = asn1_find_node(structure, name); + + if (root == NULL) + return; + + p = root; + while (p) { + if (mode == ASN1_PRINT_ALL) { + for (k = 0; k < indent; k++) + fprintf(out, " "); + fprintf(out, "name:"); + if (p->name[0] != 0) + fprintf(out, "%s ", p->name); + else + fprintf(out, "NULL "); + } else { + switch (type_field(p->type)) { + case ASN1_ETYPE_CONSTANT: + case ASN1_ETYPE_TAG: + case ASN1_ETYPE_SIZE: + break; + default: + for (k = 0; k < indent; k++) + fprintf(out, " "); + fprintf(out, "name:"); + if (p->name[0] != 0) + fprintf(out, "%s ", p->name); + else + fprintf(out, "NULL "); + } } - break; - case ASN1_ETYPE_OBJECT_ID: - if (p->value) - fprintf (out, " value:%s", p->value); - break; - case ASN1_ETYPE_ANY: - if (p->value) - { - len3 = -1; - len2 = asn1_get_length_der (p->value, p->value_len, &len3); - fprintf (out, " value:"); - if (len2 > 0) - for (k = 0; k < len2; k++) - fprintf (out, "%02x", (p->value)[k + len3]); + + if (mode != ASN1_PRINT_NAME) { + unsigned type = type_field(p->type); + switch (type) { + case ASN1_ETYPE_CONSTANT: + if (mode == ASN1_PRINT_ALL) + fprintf(out, "type:CONST"); + break; + case ASN1_ETYPE_TAG: + if (mode == ASN1_PRINT_ALL) + fprintf(out, "type:TAG"); + break; + case ASN1_ETYPE_SIZE: + if (mode == ASN1_PRINT_ALL) + fprintf(out, "type:SIZE"); + break; + case ASN1_ETYPE_DEFAULT: + fprintf(out, "type:DEFAULT"); + break; + case ASN1_ETYPE_IDENTIFIER: + fprintf(out, "type:IDENTIFIER"); + break; + case ASN1_ETYPE_ANY: + fprintf(out, "type:ANY"); + break; + case ASN1_ETYPE_CHOICE: + fprintf(out, "type:CHOICE"); + break; + case ASN1_ETYPE_DEFINITIONS: + fprintf(out, "type:DEFINITIONS"); + break; + CASE_HANDLED_ETYPES: + fprintf(out, "%s", _asn1_tags[type].desc); + break; + default: + break; + } } - break; - case ASN1_ETYPE_SET: - case ASN1_ETYPE_SET_OF: - case ASN1_ETYPE_CHOICE: - case ASN1_ETYPE_DEFINITIONS: - case ASN1_ETYPE_SEQUENCE_OF: - case ASN1_ETYPE_SEQUENCE: - case ASN1_ETYPE_NULL: - break; - default: - break; - } - } - if (mode == ASN1_PRINT_ALL) - { - if (p->type & 0x1FFFFF00) - { - fprintf (out, " attr:"); - if (p->type & CONST_UNIVERSAL) - fprintf (out, "UNIVERSAL,"); - if (p->type & CONST_PRIVATE) - fprintf (out, "PRIVATE,"); - if (p->type & CONST_APPLICATION) - fprintf (out, "APPLICATION,"); - if (p->type & CONST_EXPLICIT) - fprintf (out, "EXPLICIT,"); - if (p->type & CONST_IMPLICIT) - fprintf (out, "IMPLICIT,"); - if (p->type & CONST_TAG) - fprintf (out, "TAG,"); - if (p->type & CONST_DEFAULT) - fprintf (out, "DEFAULT,"); - if (p->type & CONST_TRUE) - fprintf (out, "TRUE,"); - if (p->type & CONST_FALSE) - fprintf (out, "FALSE,"); - if (p->type & CONST_LIST) - fprintf (out, "LIST,"); - if (p->type & CONST_MIN_MAX) - fprintf (out, "MIN_MAX,"); - if (p->type & CONST_OPTION) - fprintf (out, "OPTION,"); - if (p->type & CONST_1_PARAM) - fprintf (out, "1_PARAM,"); - if (p->type & CONST_SIZE) - fprintf (out, "SIZE,"); - if (p->type & CONST_DEFINED_BY) - fprintf (out, "DEF_BY,"); - if (p->type & CONST_GENERALIZED) - fprintf (out, "GENERALIZED,"); - if (p->type & CONST_UTC) - fprintf (out, "UTC,"); - if (p->type & CONST_SET) - fprintf (out, "SET,"); - if (p->type & CONST_NOT_USED) - fprintf (out, "NOT_USED,"); - if (p->type & CONST_ASSIGN) - fprintf (out, "ASSIGNMENT,"); - } - } + if ((mode == ASN1_PRINT_NAME_TYPE_VALUE) + || (mode == ASN1_PRINT_ALL)) { + switch (type_field(p->type)) { + case ASN1_ETYPE_CONSTANT: + if (mode == ASN1_PRINT_ALL) + if (p->value) + fprintf(out, " value:%s", + p->value); + break; + case ASN1_ETYPE_TAG: + if (mode == ASN1_PRINT_ALL) + if (p->value) + fprintf(out, " value:%s", + p->value); + break; + case ASN1_ETYPE_SIZE: + if (mode == ASN1_PRINT_ALL) + if (p->value) + fprintf(out, " value:%s", + p->value); + break; + case ASN1_ETYPE_DEFAULT: + if (p->value) + fprintf(out, " value:%s", + p->value); + else if (p->type & CONST_TRUE) + fprintf(out, " value:TRUE"); + else if (p->type & CONST_FALSE) + fprintf(out, " value:FALSE"); + break; + case ASN1_ETYPE_IDENTIFIER: + if (p->value) + fprintf(out, " value:%s", + p->value); + break; + case ASN1_ETYPE_INTEGER: + if (p->value) { + len2 = -1; + len = + asn1_get_length_der(p->value, + p-> + value_len, + &len2); + fprintf(out, " value:0x"); + if (len > 0) + for (k = 0; k < len; k++) + fprintf(out, + "%02x", + (p-> + value)[k + + len2]); + } + break; + case ASN1_ETYPE_ENUMERATED: + if (p->value) { + len2 = -1; + len = + asn1_get_length_der(p->value, + p-> + value_len, + &len2); + fprintf(out, " value:0x"); + if (len > 0) + for (k = 0; k < len; k++) + fprintf(out, + "%02x", + (p-> + value)[k + + len2]); + } + break; + case ASN1_ETYPE_BOOLEAN: + if (p->value) { + if (p->value[0] == 'T') + fprintf(out, + " value:TRUE"); + else if (p->value[0] == 'F') + fprintf(out, + " value:FALSE"); + } + break; + case ASN1_ETYPE_BIT_STRING: + if (p->value) { + len2 = -1; + len = + asn1_get_length_der(p->value, + p-> + value_len, + &len2); + if (len > 0) { + fprintf(out, + " value(%i):", + (len - 1) * 8 - + (p->value[len2])); + for (k = 1; k < len; k++) + fprintf(out, + "%02x", + (p-> + value)[k + + len2]); + } + } + break; + case ASN1_ETYPE_GENERALIZED_TIME: + case ASN1_ETYPE_UTC_TIME: + if (p->value) { + fprintf(out, " value:"); + for (k = 0; k < p->value_len; k++) + fprintf(out, "%c", + (p->value)[k]); + } + break; + case ASN1_ETYPE_GENERALSTRING: + case ASN1_ETYPE_NUMERIC_STRING: + case ASN1_ETYPE_IA5_STRING: + case ASN1_ETYPE_TELETEX_STRING: + case ASN1_ETYPE_PRINTABLE_STRING: + case ASN1_ETYPE_UNIVERSAL_STRING: + case ASN1_ETYPE_UTF8_STRING: + case ASN1_ETYPE_VISIBLE_STRING: + if (p->value) { + len2 = -1; + len = + asn1_get_length_der(p->value, + p-> + value_len, + &len2); + fprintf(out, " value:"); + if (len > 0) + for (k = 0; k < len; k++) + fprintf(out, "%c", + (p-> + value)[k + + len2]); + } + break; + case ASN1_ETYPE_BMP_STRING: + case ASN1_ETYPE_OCTET_STRING: + if (p->value) { + len2 = -1; + len = + asn1_get_length_der(p->value, + p-> + value_len, + &len2); + fprintf(out, " value:"); + if (len > 0) + for (k = 0; k < len; k++) + fprintf(out, + "%02x", + (p-> + value)[k + + len2]); + } + break; + case ASN1_ETYPE_OBJECT_ID: + if (p->value) + fprintf(out, " value:%s", + p->value); + break; + case ASN1_ETYPE_ANY: + if (p->value) { + len3 = -1; + len2 = + asn1_get_length_der(p->value, + p-> + value_len, + &len3); + fprintf(out, " value:"); + if (len2 > 0) + for (k = 0; k < len2; k++) + fprintf(out, + "%02x", + (p-> + value)[k + + len3]); + } + break; + case ASN1_ETYPE_SET: + case ASN1_ETYPE_SET_OF: + case ASN1_ETYPE_CHOICE: + case ASN1_ETYPE_DEFINITIONS: + case ASN1_ETYPE_SEQUENCE_OF: + case ASN1_ETYPE_SEQUENCE: + case ASN1_ETYPE_NULL: + break; + default: + break; + } + } - if (mode == ASN1_PRINT_ALL) - { - fprintf (out, "\n"); - } - else - { - switch (type_field (p->type)) - { - case ASN1_ETYPE_CONSTANT: - case ASN1_ETYPE_TAG: - case ASN1_ETYPE_SIZE: - break; - default: - fprintf (out, "\n"); - } - } + if (mode == ASN1_PRINT_ALL) { + if (p->type & 0x1FFFFF00) { + fprintf(out, " attr:"); + if (p->type & CONST_UNIVERSAL) + fprintf(out, "UNIVERSAL,"); + if (p->type & CONST_PRIVATE) + fprintf(out, "PRIVATE,"); + if (p->type & CONST_APPLICATION) + fprintf(out, "APPLICATION,"); + if (p->type & CONST_EXPLICIT) + fprintf(out, "EXPLICIT,"); + if (p->type & CONST_IMPLICIT) + fprintf(out, "IMPLICIT,"); + if (p->type & CONST_TAG) + fprintf(out, "TAG,"); + if (p->type & CONST_DEFAULT) + fprintf(out, "DEFAULT,"); + if (p->type & CONST_TRUE) + fprintf(out, "TRUE,"); + if (p->type & CONST_FALSE) + fprintf(out, "FALSE,"); + if (p->type & CONST_LIST) + fprintf(out, "LIST,"); + if (p->type & CONST_MIN_MAX) + fprintf(out, "MIN_MAX,"); + if (p->type & CONST_OPTION) + fprintf(out, "OPTION,"); + if (p->type & CONST_1_PARAM) + fprintf(out, "1_PARAM,"); + if (p->type & CONST_SIZE) + fprintf(out, "SIZE,"); + if (p->type & CONST_DEFINED_BY) + fprintf(out, "DEF_BY,"); + if (p->type & CONST_GENERALIZED) + fprintf(out, "GENERALIZED,"); + if (p->type & CONST_UTC) + fprintf(out, "UTC,"); + if (p->type & CONST_SET) + fprintf(out, "SET,"); + if (p->type & CONST_NOT_USED) + fprintf(out, "NOT_USED,"); + if (p->type & CONST_ASSIGN) + fprintf(out, "ASSIGNMENT,"); + } + } - if (p->down) - { - p = p->down; - indent += 2; - } - else if (p == root) - { - p = NULL; - break; - } - else if (p->right) - p = p->right; - else - { - while (1) - { - p = _asn1_find_up (p); - if (p == root) - { - p = NULL; - break; + if (mode == ASN1_PRINT_ALL) { + fprintf(out, "\n"); + } else { + switch (type_field(p->type)) { + case ASN1_ETYPE_CONSTANT: + case ASN1_ETYPE_TAG: + case ASN1_ETYPE_SIZE: + break; + default: + fprintf(out, "\n"); + } } - indent -= 2; - if (p->right) - { - p = p->right; - break; + + if (p->down) { + p = p->down; + indent += 2; + } else if (p == root) { + p = NULL; + break; + } else if (p->right) + p = p->right; + else { + while (1) { + p = _asn1_find_up(p); + if (p == root) { + p = NULL; + break; + } + indent -= 2; + if (p->right) { + p = p->right; + break; + } + } } - } } - } } @@ -1028,30 +1011,28 @@ asn1_print_structure (FILE * out, asn1_node structure, const char *name, * Returns: %ASN1_SUCCESS if successful, %ASN1_ELEMENT_NOT_FOUND if * @name is not known, %ASN1_GENERIC_ERROR if pointer @num is %NULL. **/ -int -asn1_number_of_elements (asn1_node element, const char *name, int *num) +int asn1_number_of_elements(asn1_node element, const char *name, int *num) { - asn1_node node, p; + asn1_node node, p; - if (num == NULL) - return ASN1_GENERIC_ERROR; + if (num == NULL) + return ASN1_GENERIC_ERROR; - *num = 0; + *num = 0; - node = asn1_find_node (element, name); - if (node == NULL) - return ASN1_ELEMENT_NOT_FOUND; + node = asn1_find_node(element, name); + if (node == NULL) + return ASN1_ELEMENT_NOT_FOUND; - p = node->down; + p = node->down; - while (p) - { - if (p->name[0] == '?') - (*num)++; - p = p->right; - } + while (p) { + if (p->name[0] == '?') + (*num)++; + p = p->right; + } - return ASN1_SUCCESS; + return ASN1_SUCCESS; } @@ -1066,48 +1047,49 @@ asn1_number_of_elements (asn1_node element, const char *name, int *num) * constant string that contains the element name defined just after * the OID. **/ -const char * -asn1_find_structure_from_oid (asn1_node definitions, const char *oidValue) +const char *asn1_find_structure_from_oid(asn1_node definitions, + const char *oidValue) { - char definitionsName[ASN1_MAX_NAME_SIZE], name[2 * ASN1_MAX_NAME_SIZE + 1]; - char value[ASN1_MAX_NAME_SIZE]; - asn1_node p; - int len; - int result; - - if ((definitions == NULL) || (oidValue == NULL)) - return NULL; /* ASN1_ELEMENT_NOT_FOUND; */ - - - strcpy (definitionsName, definitions->name); - strcat (definitionsName, "."); - - /* search the OBJECT_ID into definitions */ - p = definitions->down; - while (p) - { - if ((type_field (p->type) == ASN1_ETYPE_OBJECT_ID) && - (p->type & CONST_ASSIGN)) - { - strcpy (name, definitionsName); - strcat (name, p->name); - - len = ASN1_MAX_NAME_SIZE; - result = asn1_read_value (definitions, name, value, &len); - - if ((result == ASN1_SUCCESS) && (!strcmp (oidValue, value))) - { - p = p->right; - if (p == NULL) /* reach the end of ASN1 definitions */ + char definitionsName[ASN1_MAX_NAME_SIZE], + name[2 * ASN1_MAX_NAME_SIZE + 1]; + char value[ASN1_MAX_NAME_SIZE]; + asn1_node p; + int len; + int result; + + if ((definitions == NULL) || (oidValue == NULL)) return NULL; /* ASN1_ELEMENT_NOT_FOUND; */ - return p->name; - } + + strcpy(definitionsName, definitions->name); + strcat(definitionsName, "."); + + /* search the OBJECT_ID into definitions */ + p = definitions->down; + while (p) { + if ((type_field(p->type) == ASN1_ETYPE_OBJECT_ID) && + (p->type & CONST_ASSIGN)) { + strcpy(name, definitionsName); + strcat(name, p->name); + + len = ASN1_MAX_NAME_SIZE; + result = + asn1_read_value(definitions, name, value, + &len); + + if ((result == ASN1_SUCCESS) + && (!strcmp(oidValue, value))) { + p = p->right; + if (p == NULL) /* reach the end of ASN1 definitions */ + return NULL; /* ASN1_ELEMENT_NOT_FOUND; */ + + return p->name; + } + } + p = p->right; } - p = p->right; - } - return NULL; /* ASN1_ELEMENT_NOT_FOUND; */ + return NULL; /* ASN1_ELEMENT_NOT_FOUND; */ } /** @@ -1122,42 +1104,40 @@ asn1_find_structure_from_oid (asn1_node definitions, const char *oidValue) * Returns: Return %ASN1_SUCCESS on success. **/ int -asn1_copy_node (asn1_node dst, const char *dst_name, - asn1_node src, const char *src_name) +asn1_copy_node(asn1_node dst, const char *dst_name, + asn1_node src, const char *src_name) { /* FIXME: rewrite using copy_structure(). * It seems quite hard to do. */ - int result; - asn1_node dst_node; - void *data = NULL; - int size = 0; - - result = asn1_der_coding (src, src_name, NULL, &size, NULL); - if (result != ASN1_MEM_ERROR) - return result; - - data = malloc (size); - if (data == NULL) - return ASN1_MEM_ERROR; - - result = asn1_der_coding (src, src_name, data, &size, NULL); - if (result != ASN1_SUCCESS) - { - free (data); - return result; - } - - dst_node = asn1_find_node (dst, dst_name); - if (dst_node == NULL) - { - free (data); - return ASN1_ELEMENT_NOT_FOUND; - } - - result = asn1_der_decoding (&dst_node, data, size, NULL); - - free (data); - - return result; + int result; + asn1_node dst_node; + void *data = NULL; + int size = 0; + + result = asn1_der_coding(src, src_name, NULL, &size, NULL); + if (result != ASN1_MEM_ERROR) + return result; + + data = malloc(size); + if (data == NULL) + return ASN1_MEM_ERROR; + + result = asn1_der_coding(src, src_name, data, &size, NULL); + if (result != ASN1_SUCCESS) { + free(data); + return result; + } + + dst_node = asn1_find_node(dst, dst_name); + if (dst_node == NULL) { + free(data); + return ASN1_ELEMENT_NOT_FOUND; + } + + result = asn1_der_decoding(&dst_node, data, size, NULL); + + free(data); + + return result; } |