2 * Copyright (C) 2002-2014 Free Software Foundation, Inc.
4 * This file is part of LIBTASN1.
6 * The LIBTASN1 library is free software; you can redistribute it
7 * and/or modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 /*****************************************************/
24 /* File: structure.c */
25 /* Description: Functions to create and delete an */
27 /*****************************************************/
31 #include <structure.h>
32 #include "parser_aux.h"
36 extern char _asn1_identifierMissing[];
39 /******************************************************/
40 /* Function : _asn1_add_single_node */
41 /* Description: creates a new NODE_ASN element. */
43 /* type: type of the new element (see ASN1_ETYPE_ */
44 /* and CONST_ constants). */
45 /* Return: pointer to the new element. */
46 /******************************************************/
48 _asn1_add_single_node (unsigned int type)
52 punt = calloc (1, sizeof (struct asn1_node_st));
62 /******************************************************************/
63 /* Function : _asn1_find_left */
64 /* Description: returns the NODE_ASN element with RIGHT field that*/
65 /* points the element NODE. */
67 /* node: NODE_ASN element pointer. */
68 /* Return: NULL if not found. */
69 /******************************************************************/
71 _asn1_find_left (asn1_node node)
73 if ((node == NULL) || (node->left == NULL) || (node->left->down == node))
81 _asn1_create_static_structure (asn1_node pointer, char *output_file_name,
88 file = fopen (output_file_name, "w");
91 return ASN1_FILE_NOT_FOUND;
93 fprintf (file, "#if HAVE_CONFIG_H\n");
94 fprintf (file, "# include \"config.h\"\n");
95 fprintf (file, "#endif\n\n");
97 fprintf (file, "#include <libtasn1.h>\n\n");
99 fprintf (file, "const asn1_static_node %s[] = {\n", vector_name);
105 fprintf (file, " { ");
108 fprintf (file, "\"%s\", ", p->name);
110 fprintf (file, "NULL, ");
118 fprintf (file, "%lu, ", t);
121 fprintf (file, "\"%s\"},\n", p->value);
123 fprintf (file, "NULL },\n");
137 p = _asn1_get_up (p);
152 fprintf (file, " { NULL, 0, NULL }\n};\n");
162 * @array: specify the array that contains ASN.1 declarations
163 * @definitions: return the pointer to the structure created by
164 * *ARRAY ASN.1 declarations
165 * @errorDescription: return the error description.
167 * Creates the structures needed to manage the ASN.1 definitions.
168 * @array is a vector created by asn1_parser2array().
170 * Returns: %ASN1_SUCCESS if structure was created correctly,
171 * %ASN1_ELEMENT_NOT_EMPTY if *@definitions not NULL,
172 * %ASN1_IDENTIFIER_NOT_FOUND if in the file there is an identifier
173 * that is not defined (see @errorDescription for more information),
174 * %ASN1_ARRAY_ERROR if the array pointed by @array is wrong.
177 asn1_array2tree (const asn1_static_node * array, asn1_node * definitions,
178 char *errorDescription)
180 asn1_node p, p_last = NULL;
187 if (*definitions != NULL)
188 return ASN1_ELEMENT_NOT_EMPTY;
193 while (array[k].value || array[k].type || array[k].name)
195 type = convert_old_type (array[k].type);
197 p = _asn1_add_static_node (type & (~CONST_DOWN));
199 _asn1_set_name (p, array[k].name);
201 _asn1_set_value (p, array[k].value, strlen (array[k].value) + 1);
203 if (*definitions == NULL)
207 _asn1_set_down (p_last, p);
208 else if (move == RIGHT)
209 _asn1_set_right (p_last, p);
213 if (type & CONST_DOWN)
215 else if (type & CONST_RIGHT)
221 if (p_last == *definitions)
224 p_last = _asn1_get_up (p_last);
229 if (p_last->type & CONST_RIGHT)
231 p_last->type &= ~CONST_RIGHT;
240 if (p_last == *definitions)
242 result = _asn1_check_identifier (*definitions);
243 if (result == ASN1_SUCCESS)
245 _asn1_change_integer_value (*definitions);
246 _asn1_expand_object_id (*definitions);
251 result = ASN1_ARRAY_ERROR;
254 if (errorDescription != NULL)
256 if (result == ASN1_IDENTIFIER_NOT_FOUND)
258 Estrcpy (errorDescription, ":: identifier '");
259 Estrcat (errorDescription, _asn1_identifierMissing);
260 Estrcat (errorDescription, "' not found");
263 errorDescription[0] = 0;
266 if (result != ASN1_SUCCESS)
268 _asn1_delete_list_and_nodes ();
272 _asn1_delete_list ();
278 * asn1_delete_structure:
279 * @structure: pointer to the structure that you want to delete.
281 * Deletes the structure *@structure. At the end, *@structure is set
284 * Returns: %ASN1_SUCCESS if successful, %ASN1_ELEMENT_NOT_FOUND if
285 * *@structure was NULL.
288 asn1_delete_structure (asn1_node * structure)
290 return asn1_delete_structure2(structure, 0);
294 * asn1_delete_structure2:
295 * @structure: pointer to the structure that you want to delete.
296 * @flags: additional flags (see %ASN1_DELETE_FLAG)
298 * Deletes the structure *@structure. At the end, *@structure is set
301 * Returns: %ASN1_SUCCESS if successful, %ASN1_ELEMENT_NOT_FOUND if
302 * *@structure was NULL.
305 asn1_delete_structure2 (asn1_node * structure, unsigned int flags)
309 if (*structure == NULL)
310 return ASN1_ELEMENT_NOT_FOUND;
324 p3 = _asn1_get_up (p);
325 _asn1_set_down (p3, p2);
326 _asn1_remove_node (p, flags);
331 p3 = _asn1_find_left (p);
334 p3 = _asn1_get_up (p);
336 _asn1_set_down (p3, p2);
340 p->right->left = NULL;
344 _asn1_set_right (p3, p2);
345 _asn1_remove_node (p, flags);
358 * asn1_delete_element:
359 * @structure: pointer to the structure that contains the element you
361 * @element_name: element's name you want to delete.
363 * Deletes the element named *@element_name inside *@structure.
365 * Returns: %ASN1_SUCCESS if successful, %ASN1_ELEMENT_NOT_FOUND if
366 * the @element_name was not found.
369 asn1_delete_element (asn1_node structure, const char *element_name)
371 asn1_node p2, p3, source_node;
373 source_node = asn1_find_node (structure, element_name);
375 if (source_node == NULL)
376 return ASN1_ELEMENT_NOT_FOUND;
378 p2 = source_node->right;
379 p3 = _asn1_find_left (source_node);
382 p3 = _asn1_get_up (source_node);
384 _asn1_set_down (p3, p2);
385 else if (source_node->right)
386 source_node->right->left = NULL;
389 _asn1_set_right (p3, p2);
391 return asn1_delete_structure (&source_node);
395 _asn1_copy_structure3 (asn1_node source_node)
397 asn1_node dest_node, p_s, p_d, p_d_prev;
400 if (source_node == NULL)
403 dest_node = _asn1_add_single_node (source_node->type);
414 if (p_s->name[0] != 0)
415 _asn1_cpy_name (p_d, p_s);
417 _asn1_set_value (p_d, p_s->value, p_s->value_len);
422 p_d = _asn1_add_single_node (p_s->type);
423 _asn1_set_down (p_d_prev, p_d);
428 if (p_s == source_node)
436 p_d = _asn1_add_single_node (p_s->type);
437 _asn1_set_right (p_d_prev, p_d);
442 p_s = _asn1_get_up (p_s);
443 p_d = _asn1_get_up (p_d);
446 while (p_s != source_node);
453 _asn1_copy_structure2 (asn1_node root, const char *source_name)
455 asn1_node source_node;
457 source_node = asn1_find_node (root, source_name);
459 return _asn1_copy_structure3 (source_node);
465 _asn1_type_choice_config (asn1_node node)
467 asn1_node p, p2, p3, p4;
471 return ASN1_ELEMENT_NOT_FOUND;
476 while (!((p == node) && (move == UP)))
480 if ((type_field (p->type) == ASN1_ETYPE_CHOICE)
481 && (p->type & CONST_TAG))
486 if (type_field (p2->type) != ASN1_ETYPE_TAG)
488 p2->type |= CONST_TAG;
489 p3 = _asn1_find_left (p2);
492 if (type_field (p3->type) == ASN1_ETYPE_TAG)
494 p4 = _asn1_add_single_node (p3->type);
495 tlen = _asn1_strlen (p3->value);
497 _asn1_set_value (p4, p3->value, tlen + 1);
498 _asn1_set_right (p4, p2->down);
499 _asn1_set_down (p2, p4);
501 p3 = _asn1_find_left (p3);
506 p->type &= ~(CONST_TAG);
511 if (type_field (p2->type) == ASN1_ETYPE_TAG)
512 asn1_delete_structure (&p2);
543 p = _asn1_get_up (p);
551 _asn1_expand_identifier (asn1_node * node, asn1_node root)
554 char name2[ASN1_MAX_NAME_SIZE + 2];
558 return ASN1_ELEMENT_NOT_FOUND;
563 while (!((p == *node) && (move == UP)))
567 if (type_field (p->type) == ASN1_ETYPE_IDENTIFIER)
569 snprintf (name2, sizeof (name2), "%s.%s", root->name, p->value);
570 p2 = _asn1_copy_structure2 (root, name2);
573 return ASN1_IDENTIFIER_NOT_FOUND;
575 _asn1_cpy_name (p2, p);
576 p2->right = p->right;
585 _asn1_set_right (p3, p2->down);
586 _asn1_set_down (p2, p->down);
589 p3 = _asn1_find_left (p);
591 _asn1_set_right (p3, p2);
594 p3 = _asn1_get_up (p);
596 _asn1_set_down (p3, p2);
603 if (p->type & CONST_SIZE)
604 p2->type |= CONST_SIZE;
605 if (p->type & CONST_TAG)
606 p2->type |= CONST_TAG;
607 if (p->type & CONST_OPTION)
608 p2->type |= CONST_OPTION;
609 if (p->type & CONST_DEFAULT)
610 p2->type |= CONST_DEFAULT;
611 if (p->type & CONST_SET)
612 p2->type |= CONST_SET;
613 if (p->type & CONST_NOT_USED)
614 p2->type |= CONST_NOT_USED;
618 _asn1_remove_node (p, 0);
650 p = _asn1_get_up (p);
658 * asn1_create_element:
659 * @definitions: pointer to the structure returned by "parser_asn1" function
660 * @source_name: the name of the type of the new structure (must be
661 * inside p_structure).
662 * @element: pointer to the structure created.
664 * Creates a structure of type @source_name. Example using
667 * rc = asn1_create_element(cert_def, "PKIX1.Certificate", certptr);
669 * Returns: %ASN1_SUCCESS if creation OK, %ASN1_ELEMENT_NOT_FOUND if
670 * @source_name is not known.
673 asn1_create_element (asn1_node definitions, const char *source_name,
679 dest_node = _asn1_copy_structure2 (definitions, source_name);
681 if (dest_node == NULL)
682 return ASN1_ELEMENT_NOT_FOUND;
684 _asn1_set_name (dest_node, "");
686 res = _asn1_expand_identifier (&dest_node, definitions);
687 _asn1_type_choice_config (dest_node);
689 *element = dest_node;
696 * asn1_print_structure:
697 * @out: pointer to the output file (e.g. stdout).
698 * @structure: pointer to the structure that you want to visit.
699 * @name: an element of the structure
700 * @mode: specify how much of the structure to print, can be
701 * %ASN1_PRINT_NAME, %ASN1_PRINT_NAME_TYPE,
702 * %ASN1_PRINT_NAME_TYPE_VALUE, or %ASN1_PRINT_ALL.
704 * Prints on the @out file descriptor the structure's tree starting
705 * from the @name element inside the structure @structure.
708 asn1_print_structure (FILE * out, asn1_node structure, const char *name,
712 int k, indent = 0, len, len2, len3;
717 root = asn1_find_node (structure, name);
725 if (mode == ASN1_PRINT_ALL)
727 for (k = 0; k < indent; k++)
729 fprintf (out, "name:");
731 fprintf (out, "%s ", p->name);
733 fprintf (out, "NULL ");
737 switch (type_field (p->type))
739 case ASN1_ETYPE_CONSTANT:
741 case ASN1_ETYPE_SIZE:
744 for (k = 0; k < indent; k++)
746 fprintf (out, "name:");
748 fprintf (out, "%s ", p->name);
750 fprintf (out, "NULL ");
754 if (mode != ASN1_PRINT_NAME)
756 unsigned type = type_field (p->type);
759 case ASN1_ETYPE_CONSTANT:
760 if (mode == ASN1_PRINT_ALL)
761 fprintf (out, "type:CONST");
764 if (mode == ASN1_PRINT_ALL)
765 fprintf (out, "type:TAG");
767 case ASN1_ETYPE_SIZE:
768 if (mode == ASN1_PRINT_ALL)
769 fprintf (out, "type:SIZE");
771 case ASN1_ETYPE_DEFAULT:
772 fprintf (out, "type:DEFAULT");
774 case ASN1_ETYPE_IDENTIFIER:
775 fprintf (out, "type:IDENTIFIER");
778 fprintf (out, "type:ANY");
780 case ASN1_ETYPE_CHOICE:
781 fprintf (out, "type:CHOICE");
783 case ASN1_ETYPE_DEFINITIONS:
784 fprintf (out, "type:DEFINITIONS");
787 fprintf (out, "%s", _asn1_tags[type].desc);
794 if ((mode == ASN1_PRINT_NAME_TYPE_VALUE) || (mode == ASN1_PRINT_ALL))
796 switch (type_field (p->type))
798 case ASN1_ETYPE_CONSTANT:
799 if (mode == ASN1_PRINT_ALL)
801 fprintf (out, " value:%s", p->value);
804 if (mode == ASN1_PRINT_ALL)
806 fprintf (out, " value:%s", p->value);
808 case ASN1_ETYPE_SIZE:
809 if (mode == ASN1_PRINT_ALL)
811 fprintf (out, " value:%s", p->value);
813 case ASN1_ETYPE_DEFAULT:
815 fprintf (out, " value:%s", p->value);
816 else if (p->type & CONST_TRUE)
817 fprintf (out, " value:TRUE");
818 else if (p->type & CONST_FALSE)
819 fprintf (out, " value:FALSE");
821 case ASN1_ETYPE_IDENTIFIER:
823 fprintf (out, " value:%s", p->value);
825 case ASN1_ETYPE_INTEGER:
829 len = asn1_get_length_der (p->value, p->value_len, &len2);
830 fprintf (out, " value:0x");
832 for (k = 0; k < len; k++)
833 fprintf (out, "%02x", (p->value)[k + len2]);
836 case ASN1_ETYPE_ENUMERATED:
840 len = asn1_get_length_der (p->value, p->value_len, &len2);
841 fprintf (out, " value:0x");
843 for (k = 0; k < len; k++)
844 fprintf (out, "%02x", (p->value)[k + len2]);
847 case ASN1_ETYPE_BOOLEAN:
850 if (p->value[0] == 'T')
851 fprintf (out, " value:TRUE");
852 else if (p->value[0] == 'F')
853 fprintf (out, " value:FALSE");
856 case ASN1_ETYPE_BIT_STRING:
860 len = asn1_get_length_der (p->value, p->value_len, &len2);
863 fprintf (out, " value(%i):",
864 (len - 1) * 8 - (p->value[len2]));
865 for (k = 1; k < len; k++)
866 fprintf (out, "%02x", (p->value)[k + len2]);
870 case ASN1_ETYPE_GENERALIZED_TIME:
871 case ASN1_ETYPE_UTC_TIME:
874 fprintf (out, " value:");
875 for (k = 0; k < p->value_len; k++)
876 fprintf (out, "%c", (p->value)[k]);
879 case ASN1_ETYPE_GENERALSTRING:
880 case ASN1_ETYPE_NUMERIC_STRING:
881 case ASN1_ETYPE_IA5_STRING:
882 case ASN1_ETYPE_TELETEX_STRING:
883 case ASN1_ETYPE_PRINTABLE_STRING:
884 case ASN1_ETYPE_UNIVERSAL_STRING:
885 case ASN1_ETYPE_UTF8_STRING:
886 case ASN1_ETYPE_VISIBLE_STRING:
890 len = asn1_get_length_der (p->value, p->value_len, &len2);
891 fprintf (out, " value:");
893 for (k = 0; k < len; k++)
894 fprintf (out, "%c", (p->value)[k + len2]);
897 case ASN1_ETYPE_BMP_STRING:
898 case ASN1_ETYPE_OCTET_STRING:
902 len = asn1_get_length_der (p->value, p->value_len, &len2);
903 fprintf (out, " value:");
905 for (k = 0; k < len; k++)
906 fprintf (out, "%02x", (p->value)[k + len2]);
909 case ASN1_ETYPE_OBJECT_ID:
911 fprintf (out, " value:%s", p->value);
917 len2 = asn1_get_length_der (p->value, p->value_len, &len3);
918 fprintf (out, " value:");
920 for (k = 0; k < len2; k++)
921 fprintf (out, "%02x", (p->value)[k + len3]);
925 case ASN1_ETYPE_SET_OF:
926 case ASN1_ETYPE_CHOICE:
927 case ASN1_ETYPE_DEFINITIONS:
928 case ASN1_ETYPE_SEQUENCE_OF:
929 case ASN1_ETYPE_SEQUENCE:
930 case ASN1_ETYPE_NULL:
937 if (mode == ASN1_PRINT_ALL)
939 if (p->type & 0x1FFFFF00)
941 fprintf (out, " attr:");
942 if (p->type & CONST_UNIVERSAL)
943 fprintf (out, "UNIVERSAL,");
944 if (p->type & CONST_PRIVATE)
945 fprintf (out, "PRIVATE,");
946 if (p->type & CONST_APPLICATION)
947 fprintf (out, "APPLICATION,");
948 if (p->type & CONST_EXPLICIT)
949 fprintf (out, "EXPLICIT,");
950 if (p->type & CONST_IMPLICIT)
951 fprintf (out, "IMPLICIT,");
952 if (p->type & CONST_TAG)
953 fprintf (out, "TAG,");
954 if (p->type & CONST_DEFAULT)
955 fprintf (out, "DEFAULT,");
956 if (p->type & CONST_TRUE)
957 fprintf (out, "TRUE,");
958 if (p->type & CONST_FALSE)
959 fprintf (out, "FALSE,");
960 if (p->type & CONST_LIST)
961 fprintf (out, "LIST,");
962 if (p->type & CONST_MIN_MAX)
963 fprintf (out, "MIN_MAX,");
964 if (p->type & CONST_OPTION)
965 fprintf (out, "OPTION,");
966 if (p->type & CONST_1_PARAM)
967 fprintf (out, "1_PARAM,");
968 if (p->type & CONST_SIZE)
969 fprintf (out, "SIZE,");
970 if (p->type & CONST_DEFINED_BY)
971 fprintf (out, "DEF_BY,");
972 if (p->type & CONST_GENERALIZED)
973 fprintf (out, "GENERALIZED,");
974 if (p->type & CONST_UTC)
975 fprintf (out, "UTC,");
976 if (p->type & CONST_SET)
977 fprintf (out, "SET,");
978 if (p->type & CONST_NOT_USED)
979 fprintf (out, "NOT_USED,");
980 if (p->type & CONST_ASSIGN)
981 fprintf (out, "ASSIGNMENT,");
985 if (mode == ASN1_PRINT_ALL)
991 switch (type_field (p->type))
993 case ASN1_ETYPE_CONSTANT:
995 case ASN1_ETYPE_SIZE:
1018 p = _asn1_get_up (p);
1038 * asn1_number_of_elements:
1039 * @element: pointer to the root of an ASN1 structure.
1040 * @name: the name of a sub-structure of ROOT.
1041 * @num: pointer to an integer where the result will be stored
1043 * Counts the number of elements of a sub-structure called NAME with
1044 * names equal to "?1","?2", ...
1046 * Returns: %ASN1_SUCCESS if successful, %ASN1_ELEMENT_NOT_FOUND if
1047 * @name is not known, %ASN1_GENERIC_ERROR if pointer @num is %NULL.
1050 asn1_number_of_elements (asn1_node element, const char *name, int *num)
1055 return ASN1_GENERIC_ERROR;
1059 node = asn1_find_node (element, name);
1061 return ASN1_ELEMENT_NOT_FOUND;
1067 if (p->name[0] == '?')
1072 return ASN1_SUCCESS;
1077 * asn1_find_structure_from_oid:
1078 * @definitions: ASN1 definitions
1079 * @oidValue: value of the OID to search (e.g. "1.2.3.4").
1081 * Search the structure that is defined just after an OID definition.
1083 * Returns: %NULL when @oidValue not found, otherwise the pointer to a
1084 * constant string that contains the element name defined just after
1088 asn1_find_structure_from_oid (asn1_node definitions, const char *oidValue)
1090 char name[2 * ASN1_MAX_NAME_SIZE + 1];
1091 char value[ASN1_MAX_NAME_SIZE];
1095 const char *definitionsName;
1097 if ((definitions == NULL) || (oidValue == NULL))
1098 return NULL; /* ASN1_ELEMENT_NOT_FOUND; */
1100 definitionsName = definitions->name;
1102 /* search the OBJECT_ID into definitions */
1103 p = definitions->down;
1106 if ((type_field (p->type) == ASN1_ETYPE_OBJECT_ID) &&
1107 (p->type & CONST_ASSIGN))
1109 snprintf(name, sizeof(name), "%s.%s", definitionsName, p->name);
1111 len = ASN1_MAX_NAME_SIZE;
1112 result = asn1_read_value (definitions, name, value, &len);
1114 if ((result == ASN1_SUCCESS) && (!strcmp (oidValue, value)))
1117 if (p == NULL) /* reach the end of ASN1 definitions */
1118 return NULL; /* ASN1_ELEMENT_NOT_FOUND; */
1126 return NULL; /* ASN1_ELEMENT_NOT_FOUND; */
1131 * @dst: Destination asn1 node.
1132 * @dst_name: Field name in destination node.
1133 * @src: Source asn1 node.
1134 * @src_name: Field name in source node.
1136 * Create a deep copy of a asn1_node variable. That
1137 * function requires @dst to be expanded using asn1_create_element().
1139 * Returns: Return %ASN1_SUCCESS on success.
1142 asn1_copy_node (asn1_node dst, const char *dst_name,
1143 asn1_node src, const char *src_name)
1150 result = asn1_der_coding (src, src_name, NULL, &size, NULL);
1151 if (result != ASN1_MEM_ERROR)
1154 data = malloc (size);
1156 return ASN1_MEM_ERROR;
1158 result = asn1_der_coding (src, src_name, data, &size, NULL);
1159 if (result != ASN1_SUCCESS)
1165 dst_node = asn1_find_node (dst, dst_name);
1166 if (dst_node == NULL)
1169 return ASN1_ELEMENT_NOT_FOUND;
1172 result = asn1_der_decoding (&dst_node, data, size, NULL);
1181 * @src: Source asn1 node.
1182 * @src_name: Field name in source node.
1184 * Create a deep copy of a asn1_node variable. This function
1185 * will return an exact copy of the provided structure.
1187 * Returns: Return %NULL on failure.
1190 asn1_copy_node2 (asn1_node src, const char *src_name)
1192 return _asn1_copy_structure2(src, src_name);