2 * Copyright (C) 2002-2013 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: decoding.c */
25 /* Description: Functions to manage DER decoding */
26 /*****************************************************/
29 #include <parser_aux.h>
31 #include <structure.h>
37 _asn1_get_indefinite_length_string (const unsigned char *der, int *len);
40 _asn1_error_description_tag_error (asn1_node node, char *ErrorDescription)
43 Estrcpy (ErrorDescription, ":: tag error near element '");
44 _asn1_hierarchical_name (node, ErrorDescription + strlen (ErrorDescription),
45 ASN1_MAX_ERROR_DESCRIPTION_SIZE - 40);
46 Estrcat (ErrorDescription, "'");
51 * asn1_get_length_der:
52 * @der: DER data to decode.
53 * @der_len: Length of DER data to decode.
54 * @len: Output variable containing the length of the DER length field.
56 * Extract a length field from DER data.
58 * Returns: Return the decoded length value, or -1 on indefinite
59 * length, or -2 when the value was too big to fit in a int, or -4
60 * when the decoded length value plus @len would exceed @der_len.
63 asn1_get_length_der (const unsigned char *der, int der_len, int *len)
84 { /* definite length method */
86 while (punt <= k && punt < der_len)
88 if (INT_MULTIPLY_OVERFLOW (ans, 256))
92 if (INT_ADD_OVERFLOW (ans, ((unsigned) der[punt])))
99 { /* indefinite length method */
108 if (ans >= INT_MAX || INT_ADD_OVERFLOW (sum, (*len)))
120 * @der: DER data to decode.
121 * @der_len: Length of DER data to decode.
122 * @cls: Output variable containing decoded class.
123 * @len: Output variable containing the length of the DER TAG data.
124 * @tag: Output variable containing the decoded tag.
126 * Decode the class and TAG from DER code.
128 * Returns: Returns %ASN1_SUCCESS on success, or an error.
131 asn1_get_tag_der (const unsigned char *der, int der_len,
132 unsigned char *cls, int *len, unsigned long *tag)
137 if (der == NULL || der_len < 2 || len == NULL)
138 return ASN1_DER_ERROR;
140 *cls = der[0] & 0xE0;
141 if ((der[0] & 0x1F) != 0x1F)
152 while (punt <= der_len && der[punt] & 128)
155 if (INT_MULTIPLY_OVERFLOW (ris, 128))
156 return ASN1_DER_ERROR;
159 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
160 return ASN1_DER_ERROR;
161 ris += (der[punt] & 0x7F);
166 return ASN1_DER_ERROR;
168 if (INT_MULTIPLY_OVERFLOW (ris, 128))
169 return ASN1_DER_ERROR;
172 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
173 return ASN1_DER_ERROR;
174 ris += (der[punt] & 0x7F);
186 * asn1_get_length_ber:
187 * @ber: BER data to decode.
188 * @ber_len: Length of BER data to decode.
189 * @len: Output variable containing the length of the BER length field.
191 * Extract a length field from BER data. The difference to
192 * asn1_get_length_der() is that this function will return a length
193 * even if the value has indefinite encoding.
195 * Returns: Return the decoded length value, or negative value when
196 * the value was too big.
201 asn1_get_length_ber (const unsigned char *ber, int ber_len, int *len)
206 ret = asn1_get_length_der (ber, ber_len, len);
208 { /* indefinite length method */
210 err = _asn1_get_indefinite_length_string (ber + 1, &ret);
211 if (err != ASN1_SUCCESS)
219 * asn1_get_octet_der:
220 * @der: DER data to decode containing the OCTET SEQUENCE.
221 * @der_len: Length of DER data to decode.
222 * @ret_len: Output variable containing the length of the DER data.
223 * @str: Pre-allocated output buffer to put decoded OCTET SEQUENCE in.
224 * @str_size: Length of pre-allocated output buffer.
225 * @str_len: Output variable containing the length of the OCTET SEQUENCE.
227 * Extract an OCTET SEQUENCE from DER data.
229 * Returns: Returns %ASN1_SUCCESS on success, or an error.
232 asn1_get_octet_der (const unsigned char *der, int der_len,
233 int *ret_len, unsigned char *str, int str_size,
239 return ASN1_GENERIC_ERROR;
241 /* if(str==NULL) return ASN1_SUCCESS; */
242 *str_len = asn1_get_length_der (der, der_len, &len_len);
245 return ASN1_DER_ERROR;
247 *ret_len = *str_len + len_len;
248 if (str_size >= *str_len)
249 memcpy (str, der + len_len, *str_len);
252 return ASN1_MEM_ERROR;
258 /* Returns ASN1_SUCCESS on success or an error code on error.
261 _asn1_get_time_der (const unsigned char *der, int der_len, int *ret_len,
262 char *str, int str_size)
264 int len_len, str_len;
266 if (der_len <= 0 || str == NULL)
267 return ASN1_DER_ERROR;
268 str_len = asn1_get_length_der (der, der_len, &len_len);
269 if (str_len < 0 || str_size < str_len)
270 return ASN1_DER_ERROR;
271 memcpy (str, der + len_len, str_len);
273 *ret_len = str_len + len_len;
279 _asn1_get_objectid_der (const unsigned char *der, int der_len, int *ret_len,
280 char *str, int str_size)
285 unsigned long val, val1;
288 if (str && str_size > 0)
289 str[0] = 0; /* no oid */
291 if (str == NULL || der_len <= 0)
292 return ASN1_GENERIC_ERROR;
294 len = asn1_get_length_der (der, der_len, &len_len);
296 if (len < 0 || len > der_len || len_len > der_len)
297 return ASN1_DER_ERROR;
299 val1 = der[len_len] / 40;
300 val = der[len_len] - val1 * 40;
302 _asn1_str_cpy (str, str_size, _asn1_ltostr (val1, temp));
303 _asn1_str_cat (str, str_size, ".");
304 _asn1_str_cat (str, str_size, _asn1_ltostr (val, temp));
308 for (k = 1; k < len; k++)
310 /* X.690 mandates that the leading byte must never be 0x80
312 if (leading != 0 && der[len_len + k] == 0x80)
313 return ASN1_DER_ERROR;
316 /* check for wrap around */
317 if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
318 return ASN1_DER_ERROR;
321 val |= der[len_len + k] & 0x7F;
323 if (!(der[len_len + k] & 0x80))
325 _asn1_str_cat (str, str_size, ".");
326 _asn1_str_cat (str, str_size, _asn1_ltostr (val, temp));
332 if (INT_ADD_OVERFLOW (len, len_len))
333 return ASN1_DER_ERROR;
335 *ret_len = len + len_len;
342 * @der: DER data to decode containing the BIT SEQUENCE.
343 * @der_len: Length of DER data to decode.
344 * @ret_len: Output variable containing the length of the DER data.
345 * @str: Pre-allocated output buffer to put decoded BIT SEQUENCE in.
346 * @str_size: Length of pre-allocated output buffer.
347 * @bit_len: Output variable containing the size of the BIT SEQUENCE.
349 * Extract a BIT SEQUENCE from DER data.
351 * Returns: Return %ASN1_SUCCESS on success, or an error.
354 asn1_get_bit_der (const unsigned char *der, int der_len,
355 int *ret_len, unsigned char *str, int str_size,
358 int len_len, len_byte;
361 return ASN1_GENERIC_ERROR;
362 len_byte = asn1_get_length_der (der, der_len, &len_len) - 1;
364 return ASN1_DER_ERROR;
366 *ret_len = len_byte + len_len + 1;
367 *bit_len = len_byte * 8 - der[len_len];
369 if (str_size >= len_byte)
370 memcpy (str, der + len_len + 1, len_byte);
373 return ASN1_MEM_ERROR;
381 _asn1_extract_tag_der (asn1_node node, const unsigned char *der, int der_len,
385 int counter, len2, len3, is_tag_implicit;
386 unsigned long tag, tag_implicit = 0;
387 unsigned char class, class2, class_implicit = 0;
390 return ASN1_GENERIC_ERROR;
392 counter = is_tag_implicit = 0;
394 if (node->type & CONST_TAG)
399 if (type_field (p->type) == ASN1_ETYPE_TAG)
401 if (p->type & CONST_APPLICATION)
402 class2 = ASN1_CLASS_APPLICATION;
403 else if (p->type & CONST_UNIVERSAL)
404 class2 = ASN1_CLASS_UNIVERSAL;
405 else if (p->type & CONST_PRIVATE)
406 class2 = ASN1_CLASS_PRIVATE;
408 class2 = ASN1_CLASS_CONTEXT_SPECIFIC;
410 if (p->type & CONST_EXPLICIT)
413 (der + counter, der_len - counter, &class, &len2,
414 &tag) != ASN1_SUCCESS)
415 return ASN1_DER_ERROR;
417 if (counter + len2 > der_len)
418 return ASN1_DER_ERROR;
422 asn1_get_length_ber (der + counter, der_len - counter,
425 return ASN1_DER_ERROR;
428 if (counter > der_len)
429 return ASN1_DER_ERROR;
431 if (!is_tag_implicit)
433 if ((class != (class2 | ASN1_CLASS_STRUCTURED)) ||
434 (tag != strtoul ((char *) p->value, NULL, 10)))
435 return ASN1_TAG_ERROR;
438 { /* ASN1_TAG_IMPLICIT */
439 if ((class != class_implicit) || (tag != tag_implicit))
440 return ASN1_TAG_ERROR;
445 { /* ASN1_TAG_IMPLICIT */
446 if (!is_tag_implicit)
448 if ((type_field (node->type) == ASN1_ETYPE_SEQUENCE) ||
449 (type_field (node->type) == ASN1_ETYPE_SEQUENCE_OF)
450 || (type_field (node->type) == ASN1_ETYPE_SET)
451 || (type_field (node->type) == ASN1_ETYPE_SET_OF))
452 class2 |= ASN1_CLASS_STRUCTURED;
453 class_implicit = class2;
454 tag_implicit = strtoul ((char *) p->value, NULL, 10);
466 (der + counter, der_len - counter, &class, &len2,
467 &tag) != ASN1_SUCCESS)
468 return ASN1_DER_ERROR;
469 if (counter + len2 > der_len)
470 return ASN1_DER_ERROR;
472 if ((class != class_implicit) || (tag != tag_implicit))
474 if (type_field (node->type) == ASN1_ETYPE_OCTET_STRING)
476 class_implicit |= ASN1_CLASS_STRUCTURED;
477 if ((class != class_implicit) || (tag != tag_implicit))
478 return ASN1_TAG_ERROR;
481 return ASN1_TAG_ERROR;
486 unsigned type = type_field (node->type);
487 if (type == ASN1_ETYPE_TAG)
495 (der + counter, der_len - counter, &class, &len2,
496 &tag) != ASN1_SUCCESS)
497 return ASN1_DER_ERROR;
499 if (counter + len2 > der_len)
500 return ASN1_DER_ERROR;
504 case ASN1_ETYPE_NULL:
505 case ASN1_ETYPE_BOOLEAN:
506 case ASN1_ETYPE_INTEGER:
507 case ASN1_ETYPE_ENUMERATED:
508 case ASN1_ETYPE_OBJECT_ID:
509 case ASN1_ETYPE_GENERALSTRING:
510 case ASN1_ETYPE_NUMERIC_STRING:
511 case ASN1_ETYPE_IA5_STRING:
512 case ASN1_ETYPE_TELETEX_STRING:
513 case ASN1_ETYPE_PRINTABLE_STRING:
514 case ASN1_ETYPE_UNIVERSAL_STRING:
515 case ASN1_ETYPE_BMP_STRING:
516 case ASN1_ETYPE_UTF8_STRING:
517 case ASN1_ETYPE_VISIBLE_STRING:
518 case ASN1_ETYPE_BIT_STRING:
519 case ASN1_ETYPE_SEQUENCE:
520 case ASN1_ETYPE_SEQUENCE_OF:
522 case ASN1_ETYPE_SET_OF:
523 case ASN1_ETYPE_GENERALIZED_TIME:
524 case ASN1_ETYPE_UTC_TIME:
525 if ((class != _asn1_tags[type].class)
526 || (tag != _asn1_tags[type].tag))
527 return ASN1_DER_ERROR;
530 case ASN1_ETYPE_OCTET_STRING:
531 /* OCTET STRING is handled differently to allow
532 * BER encodings (structured class). */
533 if (((class != ASN1_CLASS_UNIVERSAL)
534 && (class != (ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED)))
535 || (tag != ASN1_TAG_OCTET_STRING))
536 return ASN1_DER_ERROR;
542 return ASN1_DER_ERROR;
553 extract_tag_der_recursive(asn1_node node, const unsigned char *der, int der_len,
559 if (type_field (node->type) == ASN1_ETYPE_CHOICE)
564 ris = _asn1_extract_tag_der (p, der, der_len, ret_len);
565 if (ris == ASN1_SUCCESS)
572 return _asn1_extract_tag_der (node, der, der_len, ret_len);
576 _asn1_delete_not_used (asn1_node node)
581 return ASN1_ELEMENT_NOT_FOUND;
586 if (p->type & CONST_NOT_USED)
591 p2 = _asn1_find_left (p);
593 p2 = _asn1_find_up (p);
595 asn1_delete_structure (&p);
600 break; /* reach node */
616 p = _asn1_find_up (p);
635 _asn1_extract_der_octet (asn1_node node, const unsigned char *der,
639 int counter2, counter_end;
641 len2 = asn1_get_length_der (der, der_len, &len3);
643 return ASN1_DER_ERROR;
648 counter_end = der_len - 2;
650 counter_end = der_len;
652 while (counter2 < counter_end)
654 len2 = asn1_get_length_der (der + counter2, der_len - counter2, &len3);
657 return ASN1_DER_ERROR;
661 _asn1_append_value (node, der + counter2 + len3, len2);
667 _asn1_extract_der_octet (node, der + counter2 + len3,
668 der_len - counter2 - len3);
673 counter2 += len2 + len3 + 1;
680 _asn1_get_octet_string (const unsigned char *der, asn1_node node, int *len)
682 int len2, len3, counter, tot_len, indefinite;
686 if (*(der - 1) & ASN1_CLASS_STRUCTURED)
689 indefinite = asn1_get_length_der (der, *len, &len3);
691 return ASN1_DER_ERROR;
699 if (counter > (*len))
700 return ASN1_DER_ERROR;
702 if (indefinite == -1)
704 if ((der[counter] == 0) && (der[counter + 1] == 0))
710 else if (counter >= indefinite)
713 if (der[counter] != ASN1_TAG_OCTET_STRING)
714 return ASN1_DER_ERROR;
718 len2 = asn1_get_length_der (der + counter, *len - counter, &len3);
720 return ASN1_DER_ERROR;
722 counter += len3 + len2;
729 unsigned char temp[DER_LEN];
732 len2 = sizeof (temp);
734 asn1_length_der (tot_len, temp, &len2);
735 _asn1_set_value (node, temp, len2);
737 ret = _asn1_extract_der_octet (node, der, *len);
738 if (ret != ASN1_SUCCESS)
744 { /* NOT STRUCTURED */
745 len2 = asn1_get_length_der (der, *len, &len3);
747 return ASN1_DER_ERROR;
749 counter = len3 + len2;
751 _asn1_set_value (node, der, counter);
760 _asn1_get_indefinite_length_string (const unsigned char *der, int *len)
762 int len2, len3, counter, indefinite;
766 counter = indefinite = 0;
770 if ((*len) < counter)
771 return ASN1_DER_ERROR;
773 if ((der[counter] == 0) && (der[counter + 1] == 0))
784 (der + counter, *len - counter, &class, &len2,
785 &tag) != ASN1_SUCCESS)
786 return ASN1_DER_ERROR;
787 if (counter + len2 > *len)
788 return ASN1_DER_ERROR;
790 len2 = asn1_get_length_der (der + counter, *len - counter, &len3);
792 return ASN1_DER_ERROR;
800 counter += len2 + len3;
809 static void delete_unneeded_choice_fields(asn1_node p)
813 asn1_delete_structure (&p->right);
819 * @element: pointer to an ASN1 structure.
820 * @ider: vector that contains the DER encoding.
821 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1].
822 * @errorDescription: null-terminated string contains details when an
825 * Fill the structure *@ELEMENT with values of a DER encoding
826 * string. The structure must just be created with function
827 * asn1_create_element(). If an error occurs during the decoding
828 * procedure, the *@ELEMENT is deleted and set equal to
831 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
832 * if @ELEMENT is %NULL, and %ASN1_TAG_ERROR or
833 * %ASN1_DER_ERROR if the der encoding doesn't match the structure
834 * name (*@ELEMENT deleted).
837 asn1_der_decoding (asn1_node * element, const void *ider, int len,
838 char *errorDescription)
840 asn1_node node, p, p2, p3;
842 int counter, len2, len3, len4, move, ris, tlen;
845 int indefinite, result;
846 const unsigned char *der = ider;
850 if (errorDescription != NULL)
851 errorDescription[0] = 0;
854 return ASN1_ELEMENT_NOT_FOUND;
856 if (node->type & CONST_OPTION)
858 result = ASN1_GENERIC_ERROR;
870 if (p->type & CONST_SET)
872 p2 = _asn1_find_up (p);
873 len2 = _asn1_strtol (p2->value, NULL, 10);
876 if (!der[counter] && !der[counter + 1])
884 else if (counter == len2)
890 else if (counter > len2)
892 result = ASN1_DER_ERROR;
898 if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
901 extract_tag_der_recursive (p2, der + counter,
902 len - counter, &len2);
903 if (ris == ASN1_SUCCESS)
905 p2->type &= ~CONST_NOT_USED;
914 result = ASN1_DER_ERROR;
919 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
921 p2 = _asn1_find_up (p);
922 len2 = _asn1_strtol (p2->value, NULL, 10);
933 if (p->type & CONST_OPTION)
934 asn1_delete_structure (&p);
941 if (type_field (p->type) == ASN1_ETYPE_CHOICE)
947 extract_tag_der_recursive (p->down, der + counter,
948 len - counter, &len2);
950 ris = ASN1_DER_ERROR;
951 if (ris == ASN1_SUCCESS)
953 delete_unneeded_choice_fields(p->down);
956 else if (ris == ASN1_ERROR_TYPE_ANY)
958 result = ASN1_ERROR_TYPE_ANY;
964 asn1_delete_structure (&p2);
970 if (!(p->type & CONST_OPTION))
972 result = ASN1_DER_ERROR;
980 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
982 p2 = _asn1_find_up (p);
983 len2 = _asn1_strtol (p2->value, NULL, 10);
984 if ((len2 != -1) && (counter > len2))
985 ris = ASN1_TAG_ERROR;
988 if (ris == ASN1_SUCCESS)
990 _asn1_extract_tag_der (p, der + counter, len - counter, &len2);
991 if (ris != ASN1_SUCCESS)
993 if (p->type & CONST_OPTION)
995 p->type |= CONST_NOT_USED;
998 else if (p->type & CONST_DEFAULT)
1000 _asn1_set_value (p, NULL, 0);
1005 if (errorDescription != NULL)
1006 _asn1_error_description_tag_error (p, errorDescription);
1008 result = ASN1_TAG_ERROR;
1016 if (ris == ASN1_SUCCESS)
1018 switch (type_field (p->type))
1020 case ASN1_ETYPE_NULL:
1023 result = ASN1_DER_ERROR;
1029 case ASN1_ETYPE_BOOLEAN:
1030 if (der[counter++] != 1)
1032 result = ASN1_DER_ERROR;
1035 if (der[counter++] == 0)
1036 _asn1_set_value (p, "F", 1);
1038 _asn1_set_value (p, "T", 1);
1041 case ASN1_ETYPE_INTEGER:
1042 case ASN1_ETYPE_ENUMERATED:
1044 asn1_get_length_der (der + counter, len - counter, &len3);
1047 result = ASN1_DER_ERROR;
1051 _asn1_set_value (p, der + counter, len3 + len2);
1052 counter += len3 + len2;
1055 case ASN1_ETYPE_OBJECT_ID:
1057 _asn1_get_objectid_der (der + counter, len - counter, &len2,
1058 temp, sizeof (temp));
1059 if (result != ASN1_SUCCESS)
1062 tlen = strlen (temp);
1064 _asn1_set_value (p, temp, tlen + 1);
1068 case ASN1_ETYPE_GENERALIZED_TIME:
1069 case ASN1_ETYPE_UTC_TIME:
1071 _asn1_get_time_der (der + counter, len - counter, &len2, temp,
1073 if (result != ASN1_SUCCESS)
1076 tlen = strlen (temp);
1078 _asn1_set_value (p, temp, tlen);
1082 case ASN1_ETYPE_OCTET_STRING:
1083 len3 = len - counter;
1084 result = _asn1_get_octet_string (der + counter, p, &len3);
1085 if (result != ASN1_SUCCESS)
1091 case ASN1_ETYPE_GENERALSTRING:
1092 case ASN1_ETYPE_NUMERIC_STRING:
1093 case ASN1_ETYPE_IA5_STRING:
1094 case ASN1_ETYPE_TELETEX_STRING:
1095 case ASN1_ETYPE_PRINTABLE_STRING:
1096 case ASN1_ETYPE_UNIVERSAL_STRING:
1097 case ASN1_ETYPE_BMP_STRING:
1098 case ASN1_ETYPE_UTF8_STRING:
1099 case ASN1_ETYPE_VISIBLE_STRING:
1100 case ASN1_ETYPE_BIT_STRING:
1102 asn1_get_length_der (der + counter, len - counter, &len3);
1105 result = ASN1_DER_ERROR;
1109 _asn1_set_value (p, der + counter, len3 + len2);
1110 counter += len3 + len2;
1113 case ASN1_ETYPE_SEQUENCE:
1114 case ASN1_ETYPE_SET:
1117 len2 = _asn1_strtol (p->value, NULL, 10);
1118 _asn1_set_value (p, NULL, 0);
1120 { /* indefinite length method */
1121 if (len - counter + 1 > 0)
1123 if ((der[counter]) || der[counter + 1])
1125 result = ASN1_DER_ERROR;
1131 result = ASN1_DER_ERROR;
1137 { /* definite length method */
1138 if (len2 != counter)
1140 result = ASN1_DER_ERROR;
1147 { /* move==DOWN || move==RIGHT */
1149 asn1_get_length_der (der + counter, len - counter, &len2);
1152 result = ASN1_DER_ERROR;
1158 _asn1_ltostr (counter + len3, temp);
1159 tlen = strlen (temp);
1161 _asn1_set_value (p, temp, tlen + 1);
1169 if (type_field (p2->type) != ASN1_ETYPE_TAG)
1172 asn1_delete_structure (&p2);
1181 { /* indefinite length method */
1182 _asn1_set_value (p, "-1", 3);
1187 case ASN1_ETYPE_SEQUENCE_OF:
1188 case ASN1_ETYPE_SET_OF:
1191 len2 = _asn1_strtol (p->value, NULL, 10);
1193 { /* indefinite length method */
1194 if ((counter + 2) > len)
1196 result = ASN1_DER_ERROR;
1200 if ((der[counter]) || der[counter + 1])
1202 _asn1_append_sequence_set (p);
1209 _asn1_set_value (p, NULL, 0);
1213 { /* definite length method */
1216 _asn1_append_sequence_set (p);
1223 _asn1_set_value (p, NULL, 0);
1224 if (len2 != counter)
1226 result = ASN1_DER_ERROR;
1232 { /* move==DOWN || move==RIGHT */
1234 asn1_get_length_der (der + counter, len - counter, &len2);
1237 result = ASN1_DER_ERROR;
1244 { /* definite length method */
1245 _asn1_ltostr (counter + len3, temp);
1246 tlen = strlen (temp);
1249 _asn1_set_value (p, temp, tlen + 1);
1252 { /* indefinite length method */
1253 _asn1_set_value (p, "-1", 3);
1256 while ((type_field (p2->type) == ASN1_ETYPE_TAG)
1257 || (type_field (p2->type) == ASN1_ETYPE_SIZE))
1259 if (p2->right == NULL)
1260 _asn1_append_sequence_set (p);
1266 case ASN1_ETYPE_ANY:
1267 if (asn1_get_tag_der
1268 (der + counter, len - counter, &class, &len2,
1269 &tag) != ASN1_SUCCESS)
1271 result = ASN1_DER_ERROR;
1275 if (counter + len2 > len)
1277 result = ASN1_DER_ERROR;
1281 asn1_get_length_der (der + counter + len2,
1282 len - counter - len2, &len3);
1285 result = ASN1_DER_ERROR;
1291 _asn1_set_value_lv (p, der + counter, len2 + len3);
1292 counter += len2 + len3;
1295 { /* indefinite length */
1296 /* Check indefinite lenth method in an EXPLICIT TAG */
1297 if ((p->type & CONST_TAG) && (der[counter - 1] == 0x80))
1302 len2 = len - counter;
1304 _asn1_get_indefinite_length_string (der + counter, &len2);
1305 if (result != ASN1_SUCCESS)
1308 _asn1_set_value_lv (p, der + counter, len2);
1311 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1312 an indefinite length method. */
1315 if (!der[counter] && !der[counter + 1])
1321 result = ASN1_DER_ERROR;
1329 move = (move == UP) ? RIGHT : DOWN;
1334 if (p == node && move != DOWN)
1344 if ((move == RIGHT) && !(p->type & CONST_SET))
1352 p = _asn1_find_up (p);
1355 _asn1_delete_not_used (*element);
1359 result = ASN1_DER_ERROR;
1363 return ASN1_SUCCESS;
1366 asn1_delete_structure (element);
1371 #define SAME_BRANCH 2
1372 #define OTHER_BRANCH 3
1376 * asn1_der_decoding_element:
1377 * @structure: pointer to an ASN1 structure
1378 * @elementName: name of the element to fill
1379 * @ider: vector that contains the DER encoding of the whole structure.
1380 * @len: number of bytes of *der: der[0]..der[len-1]
1381 * @errorDescription: null-terminated string contains details when an
1384 * Fill the element named @ELEMENTNAME with values of a DER encoding
1385 * string. The structure must just be created with function
1386 * asn1_create_element(). The DER vector must contain the encoding
1387 * string of the whole @STRUCTURE. If an error occurs during the
1388 * decoding procedure, the *@STRUCTURE is deleted and set equal to
1391 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1392 * if ELEMENT is %NULL or @elementName == NULL, and
1393 * %ASN1_TAG_ERROR or %ASN1_DER_ERROR if the der encoding doesn't
1394 * match the structure @structure (*ELEMENT deleted).
1397 asn1_der_decoding_element (asn1_node * structure, const char *elementName,
1398 const void *ider, int len, char *errorDescription)
1400 asn1_node node, p, p2, p3, nodeFound = NULL;
1401 char temp[128], currentName[ASN1_MAX_NAME_SIZE * 10], *dot_p, *char_p;
1402 int nameLen = ASN1_MAX_NAME_SIZE * 10 - 1, state;
1403 int counter, len2, len3, len4, move, ris, tlen;
1404 unsigned char class;
1406 int indefinite, result;
1407 const unsigned char *der = ider;
1412 return ASN1_ELEMENT_NOT_FOUND;
1414 if (elementName == NULL)
1416 result = ASN1_ELEMENT_NOT_FOUND;
1420 if (node->type & CONST_OPTION)
1422 result = ASN1_GENERIC_ERROR;
1426 if ((*structure)->name[0] != 0)
1427 { /* Has *structure got a name? */
1428 nameLen -= strlen ((*structure)->name);
1430 strcpy (currentName, (*structure)->name);
1433 result = ASN1_MEM_ERROR;
1436 if (!(strcmp (currentName, elementName)))
1439 nodeFound = *structure;
1441 else if (!memcmp (currentName, elementName, strlen (currentName)))
1442 state = SAME_BRANCH;
1444 state = OTHER_BRANCH;
1447 { /* *structure doesn't have a name? */
1449 if (elementName[0] == 0)
1452 nodeFound = *structure;
1456 state = SAME_BRANCH;
1470 if (p->type & CONST_SET)
1472 p2 = _asn1_find_up (p);
1473 len2 = _asn1_strtol (p2->value, NULL, 10);
1474 if (counter == len2)
1480 else if (counter > len2)
1482 result = ASN1_DER_ERROR;
1488 if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
1491 extract_tag_der_recursive (p2, der + counter,
1492 len - counter, &len2);
1493 if (ris == ASN1_SUCCESS)
1495 p2->type &= ~CONST_NOT_USED;
1504 result = ASN1_DER_ERROR;
1509 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1511 p2 = _asn1_find_up (p);
1512 len2 = _asn1_strtol (p2->value, NULL, 10);
1513 if (counter == len2)
1523 if (p->type & CONST_OPTION)
1524 asn1_delete_structure (&p);
1531 if (type_field (p->type) == ASN1_ETYPE_CHOICE)
1537 _asn1_extract_tag_der (p->down, der + counter,
1538 len - counter, &len2);
1540 ris = ASN1_DER_ERROR;
1541 if (ris == ASN1_SUCCESS)
1543 delete_unneeded_choice_fields(p->down);
1546 else if (ris == ASN1_ERROR_TYPE_ANY)
1548 result = ASN1_ERROR_TYPE_ANY;
1554 asn1_delete_structure (&p2);
1558 if (p->down == NULL)
1560 if (!(p->type & CONST_OPTION))
1562 result = ASN1_DER_ERROR;
1570 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1572 p2 = _asn1_find_up (p);
1573 len2 = _asn1_strtol (p2->value, NULL, 10);
1575 ris = ASN1_TAG_ERROR;
1578 if (ris == ASN1_SUCCESS)
1580 _asn1_extract_tag_der (p, der + counter, len - counter, &len2);
1581 if (ris != ASN1_SUCCESS)
1583 if (p->type & CONST_OPTION)
1585 p->type |= CONST_NOT_USED;
1588 else if (p->type & CONST_DEFAULT)
1590 _asn1_set_value (p, NULL, 0);
1595 if (errorDescription != NULL)
1596 _asn1_error_description_tag_error (p, errorDescription);
1598 result = ASN1_TAG_ERROR;
1606 if (ris == ASN1_SUCCESS)
1608 switch (type_field (p->type))
1610 case ASN1_ETYPE_NULL:
1613 result = ASN1_DER_ERROR;
1623 case ASN1_ETYPE_BOOLEAN:
1624 if (der[counter++] != 1)
1626 result = ASN1_DER_ERROR;
1632 if (der[counter++] == 0)
1633 _asn1_set_value (p, "F", 1);
1635 _asn1_set_value (p, "T", 1);
1646 case ASN1_ETYPE_INTEGER:
1647 case ASN1_ETYPE_ENUMERATED:
1649 asn1_get_length_der (der + counter, len - counter, &len3);
1652 result = ASN1_DER_ERROR;
1658 if (len3 + len2 > len - counter)
1660 result = ASN1_DER_ERROR;
1663 _asn1_set_value (p, der + counter, len3 + len2);
1668 counter += len3 + len2;
1671 case ASN1_ETYPE_OBJECT_ID:
1675 _asn1_get_objectid_der (der + counter, len - counter,
1676 &len2, temp, sizeof (temp));
1677 if (result != ASN1_SUCCESS)
1680 tlen = strlen (temp);
1683 _asn1_set_value (p, temp, tlen + 1);
1691 asn1_get_length_der (der + counter, len - counter, &len3);
1694 result = ASN1_DER_ERROR;
1703 case ASN1_ETYPE_GENERALIZED_TIME:
1704 case ASN1_ETYPE_UTC_TIME:
1708 _asn1_get_time_der (der + counter, len - counter, &len2,
1709 temp, sizeof (temp) - 1);
1710 if (result != ASN1_SUCCESS)
1713 tlen = strlen (temp);
1715 _asn1_set_value (p, temp, tlen + 1);
1723 asn1_get_length_der (der + counter, len - counter, &len3);
1726 result = ASN1_DER_ERROR;
1735 case ASN1_ETYPE_OCTET_STRING:
1736 len3 = len - counter;
1739 result = _asn1_get_octet_string (der + counter, p, &len3);
1744 result = _asn1_get_octet_string (der + counter, NULL, &len3);
1746 if (result != ASN1_SUCCESS)
1752 case ASN1_ETYPE_GENERALSTRING:
1753 case ASN1_ETYPE_NUMERIC_STRING:
1754 case ASN1_ETYPE_IA5_STRING:
1755 case ASN1_ETYPE_TELETEX_STRING:
1756 case ASN1_ETYPE_PRINTABLE_STRING:
1757 case ASN1_ETYPE_UNIVERSAL_STRING:
1758 case ASN1_ETYPE_BMP_STRING:
1759 case ASN1_ETYPE_UTF8_STRING:
1760 case ASN1_ETYPE_VISIBLE_STRING:
1761 case ASN1_ETYPE_BIT_STRING:
1763 asn1_get_length_der (der + counter, len - counter, &len3);
1766 result = ASN1_DER_ERROR;
1772 if (len3 + len2 > len - counter)
1774 result = ASN1_DER_ERROR;
1777 _asn1_set_value (p, der + counter, len3 + len2);
1782 counter += len3 + len2;
1785 case ASN1_ETYPE_SEQUENCE:
1786 case ASN1_ETYPE_SET:
1789 len2 = _asn1_strtol (p->value, NULL, 10);
1790 _asn1_set_value (p, NULL, 0);
1792 { /* indefinite length method */
1793 if ((der[counter]) || der[counter + 1])
1795 result = ASN1_DER_ERROR;
1801 { /* definite length method */
1802 if (len2 != counter)
1804 result = ASN1_DER_ERROR;
1813 { /* move==DOWN || move==RIGHT */
1814 if (state == OTHER_BRANCH)
1817 asn1_get_length_der (der + counter, len - counter,
1821 result = ASN1_DER_ERROR;
1824 counter += len2 + len3;
1828 { /* state==SAME_BRANCH or state==FOUND */
1830 asn1_get_length_der (der + counter, len - counter,
1834 result = ASN1_DER_ERROR;
1840 _asn1_ltostr (counter + len3, temp);
1841 tlen = strlen (temp);
1844 _asn1_set_value (p, temp, tlen + 1);
1852 if (type_field (p2->type) != ASN1_ETYPE_TAG)
1855 asn1_delete_structure (&p2);
1864 { /* indefinite length method */
1865 _asn1_set_value (p, "-1", 3);
1871 case ASN1_ETYPE_SEQUENCE_OF:
1872 case ASN1_ETYPE_SET_OF:
1875 len2 = _asn1_strtol (p->value, NULL, 10);
1878 _asn1_append_sequence_set (p);
1885 _asn1_set_value (p, NULL, 0);
1886 if (len2 != counter)
1888 result = ASN1_DER_ERROR;
1896 { /* move==DOWN || move==RIGHT */
1897 if (state == OTHER_BRANCH)
1900 asn1_get_length_der (der + counter, len - counter,
1904 result = ASN1_DER_ERROR;
1907 counter += len2 + len3;
1911 { /* state==FOUND or state==SAME_BRANCH */
1913 asn1_get_length_der (der + counter, len - counter,
1917 result = ASN1_DER_ERROR;
1923 _asn1_ltostr (counter + len3, temp);
1924 tlen = strlen (temp);
1927 _asn1_set_value (p, temp, tlen + 1);
1929 while ((type_field (p2->type) == ASN1_ETYPE_TAG)
1930 || (type_field (p2->type) ==
1933 if (p2->right == NULL)
1934 _asn1_append_sequence_set (p);
1942 case ASN1_ETYPE_ANY:
1943 if (asn1_get_tag_der
1944 (der + counter, len - counter, &class, &len2,
1945 &tag) != ASN1_SUCCESS)
1947 result = ASN1_DER_ERROR;
1951 if (counter + len2 > len)
1953 result = ASN1_DER_ERROR;
1958 asn1_get_length_der (der + counter + len2,
1959 len - counter - len2, &len3);
1962 result = ASN1_DER_ERROR;
1971 _asn1_set_value_lv (p, der + counter, len2 + len3);
1976 counter += len2 + len3;
1979 { /* indefinite length */
1980 /* Check indefinite lenth method in an EXPLICIT TAG */
1981 if ((p->type & CONST_TAG) && (der[counter - 1] == 0x80))
1986 len2 = len - counter;
1988 _asn1_get_indefinite_length_string (der + counter, &len2);
1989 if (result != ASN1_SUCCESS)
1994 _asn1_set_value_lv (p, der + counter, len2);
2002 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
2003 an indefinite length method. */
2006 if (!der[counter] && !der[counter + 1])
2012 result = ASN1_DER_ERROR;
2021 move = (move == UP) ? RIGHT : DOWN;
2026 if ((p == node && move != DOWN) || (state == EXIT))
2037 nameLen -= strlen (p->name) + 1;
2041 strcat (currentName, ".");
2042 strcat (currentName, p->name);
2046 result = ASN1_MEM_ERROR;
2049 if (!(strcmp (currentName, elementName)))
2056 (currentName, elementName, strlen (currentName)))
2057 state = SAME_BRANCH;
2059 state = OTHER_BRANCH;
2066 if ((move == RIGHT) && !(p->type & CONST_SET))
2074 dot_p = char_p = currentName;
2075 while ((char_p = strchr (char_p, '.')))
2081 nameLen += strlen (currentName) - (dot_p - currentName);
2084 nameLen -= strlen (p->name);
2086 strcat (currentName, p->name);
2089 result = ASN1_MEM_ERROR;
2093 if (!(strcmp (currentName, elementName)))
2100 (currentName, elementName, strlen (currentName)))
2101 state = SAME_BRANCH;
2103 state = OTHER_BRANCH;
2112 p = _asn1_find_up (p);
2116 dot_p = char_p = currentName;
2117 while ((char_p = strchr (char_p, '.')))
2123 nameLen += strlen (currentName) - (dot_p - currentName);
2126 if (!(strcmp (currentName, elementName)))
2132 if (!memcmp (currentName, elementName, strlen (currentName)))
2133 state = SAME_BRANCH;
2135 state = OTHER_BRANCH;
2140 _asn1_delete_not_used (*structure);
2144 result = ASN1_DER_ERROR;
2148 return ASN1_SUCCESS;
2151 asn1_delete_structure (structure);
2156 * asn1_der_decoding_startEnd:
2157 * @element: pointer to an ASN1 element
2158 * @ider: vector that contains the DER encoding.
2159 * @len: number of bytes of *@ider: @ider[0]..@ider[len-1]
2160 * @name_element: an element of NAME structure.
2161 * @start: the position of the first byte of NAME_ELEMENT decoding
2163 * @end: the position of the last byte of NAME_ELEMENT decoding
2166 * Find the start and end point of an element in a DER encoding
2167 * string. I mean that if you have a der encoding and you have already
2168 * used the function asn1_der_decoding() to fill a structure, it may
2169 * happen that you want to find the piece of string concerning an
2170 * element of the structure.
2172 * One example is the sequence "tbsCertificate" inside an X509
2175 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
2176 * if ELEMENT is %asn1_node EMPTY or @name_element is not a valid
2177 * element, %ASN1_TAG_ERROR or %ASN1_DER_ERROR if the der encoding
2178 * doesn't match the structure ELEMENT.
2181 asn1_der_decoding_startEnd (asn1_node element, const void *ider, int len,
2182 const char *name_element, int *start, int *end)
2184 asn1_node node, node_to_find, p, p2, p3;
2185 int counter, len2, len3, len4, move, ris;
2186 unsigned char class;
2189 const unsigned char *der = ider;
2194 return ASN1_ELEMENT_NOT_FOUND;
2196 node_to_find = asn1_find_node (node, name_element);
2198 if (node_to_find == NULL)
2199 return ASN1_ELEMENT_NOT_FOUND;
2201 if (node_to_find == node)
2205 return ASN1_SUCCESS;
2208 if (node->type & CONST_OPTION)
2209 return ASN1_GENERIC_ERROR;
2217 return ASN1_DER_ERROR;
2223 if (p->type & CONST_SET)
2225 p2 = _asn1_find_up (p);
2227 return ASN1_DER_ERROR;
2229 len2 = _asn1_strtol (p2->value, NULL, 10);
2232 if (!der[counter] && !der[counter + 1])
2240 else if (counter == len2)
2246 else if (counter > len2)
2247 return ASN1_DER_ERROR;
2253 if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
2256 extract_tag_der_recursive (p2, der + counter,
2257 len - counter, &len2);
2258 if (ris == ASN1_SUCCESS)
2260 p2->type &= ~CONST_NOT_USED;
2268 return ASN1_DER_ERROR;
2271 if (p == node_to_find)
2274 if (type_field (p->type) == ASN1_ETYPE_CHOICE)
2278 return ASN1_DER_ERROR;
2281 _asn1_extract_tag_der (p, der + counter, len - counter,
2283 if (p == node_to_find)
2287 if (ris == ASN1_SUCCESS)
2289 _asn1_extract_tag_der (p, der + counter, len - counter, &len2);
2290 if (ris != ASN1_SUCCESS)
2292 if (p->type & CONST_OPTION)
2294 p->type |= CONST_NOT_USED;
2297 else if (p->type & CONST_DEFAULT)
2303 return ASN1_TAG_ERROR;
2310 if (ris == ASN1_SUCCESS)
2312 switch (type_field (p->type))
2314 case ASN1_ETYPE_NULL:
2316 return ASN1_DER_ERROR;
2320 case ASN1_ETYPE_BOOLEAN:
2321 if (der[counter++] != 1)
2322 return ASN1_DER_ERROR;
2326 case ASN1_ETYPE_OCTET_STRING:
2327 len3 = len - counter;
2328 ris = _asn1_get_octet_string (der + counter, NULL, &len3);
2329 if (ris != ASN1_SUCCESS)
2334 case ASN1_ETYPE_UTC_TIME:
2335 case ASN1_ETYPE_GENERALIZED_TIME:
2336 case ASN1_ETYPE_OBJECT_ID:
2337 case ASN1_ETYPE_INTEGER:
2338 case ASN1_ETYPE_ENUMERATED:
2339 case ASN1_ETYPE_GENERALSTRING:
2340 case ASN1_ETYPE_NUMERIC_STRING:
2341 case ASN1_ETYPE_IA5_STRING:
2342 case ASN1_ETYPE_TELETEX_STRING:
2343 case ASN1_ETYPE_PRINTABLE_STRING:
2344 case ASN1_ETYPE_UNIVERSAL_STRING:
2345 case ASN1_ETYPE_BMP_STRING:
2346 case ASN1_ETYPE_UTF8_STRING:
2347 case ASN1_ETYPE_VISIBLE_STRING:
2348 case ASN1_ETYPE_BIT_STRING:
2350 asn1_get_length_der (der + counter, len - counter, &len3);
2352 return ASN1_DER_ERROR;
2353 counter += len3 + len2;
2356 case ASN1_ETYPE_SEQUENCE:
2357 case ASN1_ETYPE_SET:
2361 asn1_get_length_der (der + counter, len - counter, &len2);
2363 return ASN1_DER_ERROR;
2372 if (!der[counter] && !der[counter + 1]) /* indefinite length method */
2377 case ASN1_ETYPE_SEQUENCE_OF:
2378 case ASN1_ETYPE_SET_OF:
2382 asn1_get_length_der (der + counter, len - counter, &len2);
2384 return ASN1_DER_ERROR;
2386 if ((len3 == -1) && !der[counter] && !der[counter + 1])
2391 while ((type_field (p2->type) == ASN1_ETYPE_TAG) ||
2392 (type_field (p2->type) == ASN1_ETYPE_SIZE))
2399 if (!der[counter] && !der[counter + 1]) /* indefinite length method */
2404 case ASN1_ETYPE_ANY:
2405 if (asn1_get_tag_der
2406 (der + counter, len - counter, &class, &len2,
2407 &tag) != ASN1_SUCCESS)
2408 return ASN1_DER_ERROR;
2409 if (counter + len2 > len)
2410 return ASN1_DER_ERROR;
2413 asn1_get_length_der (der + counter + len2,
2414 len - counter - len2, &len3);
2416 return ASN1_DER_ERROR;
2420 counter += len2 + len4 + len3;
2423 { /* indefinite length */
2424 /* Check indefinite lenth method in an EXPLICIT TAG */
2425 if ((p->type & CONST_TAG) && (der[counter - 1] == 0x80))
2430 len2 = len - counter;
2432 _asn1_get_indefinite_length_string (der + counter, &len2);
2433 if (ris != ASN1_SUCCESS)
2437 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
2438 an indefinite length method. */
2441 if (!der[counter] && !der[counter + 1])
2444 return ASN1_DER_ERROR;
2450 move = (move == UP) ? RIGHT : DOWN;
2455 if ((p == node_to_find) && (move == RIGHT))
2458 return ASN1_SUCCESS;
2461 if (p == node && move != DOWN)
2471 if ((move == RIGHT) && !(p->type & CONST_SET))
2479 p = _asn1_find_up (p);
2482 return ASN1_ELEMENT_NOT_FOUND;
2486 * asn1_expand_any_defined_by:
2487 * @definitions: ASN1 definitions
2488 * @element: pointer to an ASN1 structure
2490 * Expands every "ANY DEFINED BY" element of a structure created from
2491 * a DER decoding process (asn1_der_decoding function). The element
2492 * ANY must be defined by an OBJECT IDENTIFIER. The type used to
2493 * expand the element ANY is the first one following the definition of
2494 * the actual value of the OBJECT IDENTIFIER.
2496 * Returns: %ASN1_SUCCESS if Substitution OK, %ASN1_ERROR_TYPE_ANY if
2497 * some "ANY DEFINED BY" element couldn't be expanded due to a
2498 * problem in OBJECT_ID -> TYPE association, or other error codes
2499 * depending on DER decoding.
2502 asn1_expand_any_defined_by (asn1_node definitions, asn1_node * element)
2504 char definitionsName[ASN1_MAX_NAME_SIZE], name[2 * ASN1_MAX_NAME_SIZE + 1],
2505 value[ASN1_MAX_NAME_SIZE];
2506 int retCode = ASN1_SUCCESS, result;
2507 int len, len2, len3;
2508 asn1_node p, p2, p3, aux = NULL;
2509 char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
2511 if ((definitions == NULL) || (*element == NULL))
2512 return ASN1_ELEMENT_NOT_FOUND;
2514 strcpy (definitionsName, definitions->name);
2515 strcat (definitionsName, ".");
2521 switch (type_field (p->type))
2523 case ASN1_ETYPE_ANY:
2524 if ((p->type & CONST_DEFINED_BY) && (p->value))
2526 /* search the "DEF_BY" element */
2528 while ((p2) && (type_field (p2->type) != ASN1_ETYPE_CONSTANT))
2533 retCode = ASN1_ERROR_TYPE_ANY;
2537 p3 = _asn1_find_up (p);
2541 retCode = ASN1_ERROR_TYPE_ANY;
2548 if (!(strcmp (p3->name, p2->name)))
2553 if ((!p3) || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID) ||
2554 (p3->value == NULL))
2557 p3 = _asn1_find_up (p);
2558 p3 = _asn1_find_up (p3);
2562 retCode = ASN1_ERROR_TYPE_ANY;
2570 if (!(strcmp (p3->name, p2->name)))
2575 if ((!p3) || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID)
2576 || (p3->value == NULL))
2578 retCode = ASN1_ERROR_TYPE_ANY;
2583 /* search the OBJECT_ID into definitions */
2584 p2 = definitions->down;
2587 if ((type_field (p2->type) == ASN1_ETYPE_OBJECT_ID) &&
2588 (p2->type & CONST_ASSIGN))
2590 strcpy (name, definitionsName);
2591 strcat (name, p2->name);
2593 len = ASN1_MAX_NAME_SIZE;
2595 asn1_read_value (definitions, name, value, &len);
2597 if ((result == ASN1_SUCCESS)
2598 && (!_asn1_strcmp (p3->value, value)))
2600 p2 = p2->right; /* pointer to the structure to
2601 use for expansion */
2602 while ((p2) && (p2->type & CONST_ASSIGN))
2607 strcpy (name, definitionsName);
2608 strcat (name, p2->name);
2611 asn1_create_element (definitions, name, &aux);
2612 if (result == ASN1_SUCCESS)
2614 _asn1_cpy_name (aux, p);
2616 asn1_get_length_der (p->value,
2617 p->value_len, &len3);
2619 return ASN1_DER_ERROR;
2622 asn1_der_decoding (&aux, p->value + len3,
2625 if (result == ASN1_SUCCESS)
2628 _asn1_set_right (aux, p->right);
2629 _asn1_set_right (p, aux);
2631 result = asn1_delete_structure (&p);
2632 if (result == ASN1_SUCCESS)
2639 { /* error with asn1_delete_structure */
2640 asn1_delete_structure (&aux);
2646 { /* error with asn1_der_decoding */
2652 { /* error with asn1_create_element */
2658 { /* error with the pointer to the structure to exapand */
2659 retCode = ASN1_ERROR_TYPE_ANY;
2669 retCode = ASN1_ERROR_TYPE_ANY;
2684 else if (p == *element)
2695 p = _asn1_find_up (p);
2714 * asn1_expand_octet_string:
2715 * @definitions: ASN1 definitions
2716 * @element: pointer to an ASN1 structure
2717 * @octetName: name of the OCTECT STRING field to expand.
2718 * @objectName: name of the OBJECT IDENTIFIER field to use to define
2719 * the type for expansion.
2721 * Expands an "OCTET STRING" element of a structure created from a DER
2722 * decoding process (the asn1_der_decoding() function). The type used
2723 * for expansion is the first one following the definition of the
2724 * actual value of the OBJECT IDENTIFIER indicated by OBJECTNAME.
2726 * Returns: %ASN1_SUCCESS if substitution OK, %ASN1_ELEMENT_NOT_FOUND
2727 * if @objectName or @octetName are not correct,
2728 * %ASN1_VALUE_NOT_VALID if it wasn't possible to find the type to
2729 * use for expansion, or other errors depending on DER decoding.
2732 asn1_expand_octet_string (asn1_node definitions, asn1_node * element,
2733 const char *octetName, const char *objectName)
2735 char name[2 * ASN1_MAX_NAME_SIZE + 1], value[ASN1_MAX_NAME_SIZE];
2736 int retCode = ASN1_SUCCESS, result;
2737 int len, len2, len3;
2738 asn1_node p2, aux = NULL;
2739 asn1_node octetNode = NULL, objectNode = NULL;
2740 char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
2742 if ((definitions == NULL) || (*element == NULL))
2743 return ASN1_ELEMENT_NOT_FOUND;
2745 octetNode = asn1_find_node (*element, octetName);
2746 if (octetNode == NULL)
2747 return ASN1_ELEMENT_NOT_FOUND;
2748 if (type_field (octetNode->type) != ASN1_ETYPE_OCTET_STRING)
2749 return ASN1_ELEMENT_NOT_FOUND;
2750 if (octetNode->value == NULL)
2751 return ASN1_VALUE_NOT_FOUND;
2753 objectNode = asn1_find_node (*element, objectName);
2754 if (objectNode == NULL)
2755 return ASN1_ELEMENT_NOT_FOUND;
2757 if (type_field (objectNode->type) != ASN1_ETYPE_OBJECT_ID)
2758 return ASN1_ELEMENT_NOT_FOUND;
2760 if (objectNode->value == NULL)
2761 return ASN1_VALUE_NOT_FOUND;
2764 /* search the OBJECT_ID into definitions */
2765 p2 = definitions->down;
2768 if ((type_field (p2->type) == ASN1_ETYPE_OBJECT_ID) &&
2769 (p2->type & CONST_ASSIGN))
2771 strcpy (name, definitions->name);
2773 strcat (name, p2->name);
2775 len = sizeof (value);
2776 result = asn1_read_value (definitions, name, value, &len);
2778 if ((result == ASN1_SUCCESS)
2779 && (!_asn1_strcmp (objectNode->value, value)))
2782 p2 = p2->right; /* pointer to the structure to
2783 use for expansion */
2784 while ((p2) && (p2->type & CONST_ASSIGN))
2789 strcpy (name, definitions->name);
2791 strcat (name, p2->name);
2793 result = asn1_create_element (definitions, name, &aux);
2794 if (result == ASN1_SUCCESS)
2796 _asn1_cpy_name (aux, octetNode);
2798 asn1_get_length_der (octetNode->value,
2799 octetNode->value_len, &len3);
2801 return ASN1_DER_ERROR;
2804 asn1_der_decoding (&aux, octetNode->value + len3,
2805 len2, errorDescription);
2806 if (result == ASN1_SUCCESS)
2809 _asn1_set_right (aux, octetNode->right);
2810 _asn1_set_right (octetNode, aux);
2812 result = asn1_delete_structure (&octetNode);
2813 if (result == ASN1_SUCCESS)
2819 { /* error with asn1_delete_structure */
2820 asn1_delete_structure (&aux);
2826 { /* error with asn1_der_decoding */
2832 { /* error with asn1_create_element */
2838 { /* error with the pointer to the structure to exapand */
2839 retCode = ASN1_VALUE_NOT_VALID;
2850 retCode = ASN1_VALUE_NOT_VALID;
2856 * asn1_decode_simple_der:
2857 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2858 * @der: the encoded string
2859 * @der_len: the bytes of the encoded string
2860 * @str: a pointer to the data
2861 * @str_len: the length of the data
2863 * Decodes a simple DER encoded type (e.g. a string, which is not constructed).
2864 * The output is a pointer inside the @der.
2866 * Returns: %ASN1_SUCCESS if successful or an error value.
2869 asn1_decode_simple_der (unsigned int etype, const unsigned char *der,
2870 unsigned int der_len, const unsigned char **str,
2871 unsigned int *str_len)
2873 int tag_len, len_len;
2874 const unsigned char *p;
2875 unsigned char class;
2879 if (der == NULL || der_len == 0)
2880 return ASN1_VALUE_NOT_VALID;
2882 if (ETYPE_OK (etype) == 0)
2883 return ASN1_VALUE_NOT_VALID;
2885 /* doesn't handle constructed classes */
2886 if (ETYPE_CLASS (etype) != ASN1_CLASS_UNIVERSAL)
2887 return ASN1_VALUE_NOT_VALID;
2890 ret = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag);
2891 if (ret != ASN1_SUCCESS)
2894 if (class != ETYPE_CLASS (etype) || tag != ETYPE_TAG (etype))
2895 return ASN1_DER_ERROR;
2900 ret = asn1_get_length_der (p, der_len, &len_len);
2902 return ASN1_DER_ERROR;
2910 return ASN1_SUCCESS;