2 * Copyright (C) 2002-2016 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 # define warn() fprintf(stderr, "%s: %d\n", __func__, __LINE__)
42 #define IS_ERR(len, flags) (len < -1 || ((flags & ASN1_DECODE_FLAG_STRICT_DER) && len < 0))
44 #define HAVE_TWO(x) (x>=2?1:0)
46 #define DECODE_FLAG_HAVE_TAG 1
47 #define DECODE_FLAG_INDEFINITE (1<<1)
49 #define DECR_LEN(l, s) do { \
53 result = ASN1_DER_ERROR; \
59 _asn1_get_indefinite_length_string (const unsigned char *der, int der_len, int *len);
62 _asn1_decode_simple_ber (unsigned int etype, const unsigned char *der,
63 unsigned int _der_len, unsigned char **str,
64 unsigned int *str_len, unsigned int *ber_len,
68 _asn1_decode_simple_der (unsigned int etype, const unsigned char *der,
69 unsigned int _der_len, const unsigned char **str,
70 unsigned int *str_len, unsigned dflags);
73 _asn1_error_description_tag_error (asn1_node node, char *ErrorDescription)
76 Estrcpy (ErrorDescription, ":: tag error near element '");
77 _asn1_hierarchical_name (node, ErrorDescription + strlen (ErrorDescription),
78 ASN1_MAX_ERROR_DESCRIPTION_SIZE - 40);
79 Estrcat (ErrorDescription, "'");
84 * asn1_get_length_der:
85 * @der: DER data to decode.
86 * @der_len: Length of DER data to decode.
87 * @len: Output variable containing the length of the DER length field.
89 * Extract a length field from DER data.
91 * Returns: Return the decoded length value, or -1 on indefinite
92 * length, or -2 when the value was too big to fit in a int, or -4
93 * when the decoded length value plus @len would exceed @der_len.
96 asn1_get_length_der (const unsigned char *der, int der_len, int *len)
117 { /* definite length method */
119 while (punt <= k && punt < der_len)
121 if (INT_MULTIPLY_OVERFLOW (ans, 256))
125 if (INT_ADD_OVERFLOW (ans, ((unsigned) der[punt])))
132 { /* indefinite length method */
141 if (ans >= INT_MAX || INT_ADD_OVERFLOW (sum, (*len)))
153 * @der: DER data to decode.
154 * @der_len: Length of DER data to decode.
155 * @cls: Output variable containing decoded class.
156 * @len: Output variable containing the length of the DER TAG data.
157 * @tag: Output variable containing the decoded tag.
159 * Decode the class and TAG from DER code.
161 * Returns: Returns %ASN1_SUCCESS on success, or an error.
164 asn1_get_tag_der (const unsigned char *der, int der_len,
165 unsigned char *cls, int *len, unsigned long *tag)
170 if (der == NULL || der_len < 2 || len == NULL)
171 return ASN1_DER_ERROR;
173 *cls = der[0] & 0xE0;
174 if ((der[0] & 0x1F) != 0x1F)
185 while (punt < der_len && der[punt] & 128)
188 if (INT_MULTIPLY_OVERFLOW (ris, 128))
189 return ASN1_DER_ERROR;
192 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
193 return ASN1_DER_ERROR;
194 ris += (der[punt] & 0x7F);
199 return ASN1_DER_ERROR;
201 if (INT_MULTIPLY_OVERFLOW (ris, 128))
202 return ASN1_DER_ERROR;
205 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
206 return ASN1_DER_ERROR;
207 ris += (der[punt] & 0x7F);
219 * asn1_get_length_ber:
220 * @ber: BER data to decode.
221 * @ber_len: Length of BER data to decode.
222 * @len: Output variable containing the length of the BER length field.
224 * Extract a length field from BER data. The difference to
225 * asn1_get_length_der() is that this function will return a length
226 * even if the value has indefinite encoding.
228 * Returns: Return the decoded length value, or negative value when
229 * the value was too big.
234 asn1_get_length_ber (const unsigned char *ber, int ber_len, int *len)
239 ret = asn1_get_length_der (ber, ber_len, len);
241 { /* indefinite length method */
242 err = _asn1_get_indefinite_length_string (ber + 1, ber_len, &ret);
243 if (err != ASN1_SUCCESS)
251 * asn1_get_octet_der:
252 * @der: DER data to decode containing the OCTET SEQUENCE.
253 * @der_len: The length of the @der data to decode.
254 * @ret_len: Output variable containing the encoded length of the DER data.
255 * @str: Pre-allocated output buffer to put decoded OCTET SEQUENCE in.
256 * @str_size: Length of pre-allocated output buffer.
257 * @str_len: Output variable containing the length of the contents of the OCTET SEQUENCE.
259 * Extract an OCTET SEQUENCE from DER data. Note that this function
260 * expects the DER data past the tag field, i.e., the length and
263 * Returns: Returns %ASN1_SUCCESS on success, or an error.
266 asn1_get_octet_der (const unsigned char *der, int der_len,
267 int *ret_len, unsigned char *str, int str_size,
273 return ASN1_GENERIC_ERROR;
275 *str_len = asn1_get_length_der (der, der_len, &len_len);
278 return ASN1_DER_ERROR;
280 *ret_len = *str_len + len_len;
281 if (str_size >= *str_len)
283 if (*str_len > 0 && str != NULL)
284 memcpy (str, der + len_len, *str_len);
288 return ASN1_MEM_ERROR;
296 * _asn1_get_time_der:
297 * @type: %ASN1_ETYPE_GENERALIZED_TIME or %ASN1_ETYPE_UTC_TIME
298 * @der: DER data to decode containing the time
299 * @der_len: Length of DER data to decode.
300 * @ret_len: Output variable containing the length of the DER data.
301 * @str: Pre-allocated output buffer to put the textual time in.
302 * @str_size: Length of pre-allocated output buffer.
303 * @flags: Zero or %ASN1_DECODE_FLAG_STRICT_DER
305 * Performs basic checks in the DER encoded time object and returns its textual form.
306 * The textual form will be in the YYYYMMDD000000Z format for GeneralizedTime
307 * and YYMMDD000000Z for UTCTime.
309 * Returns: %ASN1_SUCCESS on success, or an error.
312 _asn1_get_time_der (unsigned type, const unsigned char *der, int der_len, int *ret_len,
313 char *str, int str_size, unsigned flags)
315 int len_len, str_len;
317 unsigned sign_count = 0;
318 unsigned dot_count = 0;
319 const unsigned char *p;
321 if (der_len <= 0 || str == NULL)
322 return ASN1_DER_ERROR;
324 str_len = asn1_get_length_der (der, der_len, &len_len);
325 if (str_len <= 0 || str_size < str_len)
326 return ASN1_DER_ERROR;
328 /* perform some sanity checks on the data */
332 return ASN1_DER_ERROR;
335 if (flags & ASN1_DECODE_FLAG_STRICT_DER)
338 for (i=0;i<(unsigned)(str_len-1);i++)
340 if (isdigit(p[i]) == 0)
342 if (type == ASN1_ETYPE_GENERALIZED_TIME)
344 /* tolerate lax encodings */
345 if (p[i] == '.' && dot_count == 0)
351 /* This is not really valid DER, but there are
352 * structures using that */
353 if (!(flags & ASN1_DECODE_FLAG_STRICT_DER) &&
354 (p[i] == '+' || p[i] == '-') && sign_count == 0)
362 return ASN1_DER_ERROR;
366 if (sign_count == 0 && p[str_len-1] != 'Z')
369 return ASN1_DER_ERROR;
372 memcpy (str, der + len_len, str_len);
374 *ret_len = str_len + len_len;
380 * asn1_get_objectid_der:
381 * @der: DER data to decode containing the OBJECT IDENTIFIER
382 * @der_len: Length of DER data to decode.
383 * @ret_len: Output variable containing the length of the DER data.
384 * @str: Pre-allocated output buffer to put the textual object id in.
385 * @str_size: Length of pre-allocated output buffer.
387 * Converts a DER encoded object identifier to its textual form. This
388 * function expects the DER object identifier without the tag.
390 * Returns: %ASN1_SUCCESS on success, or an error.
393 asn1_get_object_id_der (const unsigned char *der, int der_len, int *ret_len,
394 char *str, int str_size)
398 char temp[LTOSTR_MAX_SIZE];
402 if (str && str_size > 0)
403 str[0] = 0; /* no oid */
405 if (str == NULL || der_len <= 0)
406 return ASN1_GENERIC_ERROR;
408 len = asn1_get_length_der (der, der_len, &len_len);
410 if (len <= 0 || len + len_len > der_len)
411 return ASN1_DER_ERROR;
413 val1 = der[len_len] / 40;
414 val = der[len_len] - val1 * 40;
416 _asn1_str_cpy (str, str_size, _asn1_ltostr (val1, temp));
417 _asn1_str_cat (str, str_size, ".");
418 _asn1_str_cat (str, str_size, _asn1_ltostr (val, temp));
422 for (k = 1; k < len; k++)
424 /* X.690 mandates that the leading byte must never be 0x80
426 if (leading != 0 && der[len_len + k] == 0x80)
427 return ASN1_DER_ERROR;
430 /* check for wrap around */
431 if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
432 return ASN1_DER_ERROR;
435 val |= der[len_len + k] & 0x7F;
437 if (!(der[len_len + k] & 0x80))
439 _asn1_str_cat (str, str_size, ".");
440 _asn1_str_cat (str, str_size, _asn1_ltostr (val, temp));
446 if (INT_ADD_OVERFLOW (len, len_len))
447 return ASN1_DER_ERROR;
449 *ret_len = len + len_len;
456 * @der: DER data to decode containing the BIT SEQUENCE.
457 * @der_len: Length of DER data to decode.
458 * @ret_len: Output variable containing the length of the DER data.
459 * @str: Pre-allocated output buffer to put decoded BIT SEQUENCE in.
460 * @str_size: Length of pre-allocated output buffer.
461 * @bit_len: Output variable containing the size of the BIT SEQUENCE.
463 * Extract a BIT SEQUENCE from DER data.
465 * Returns: %ASN1_SUCCESS on success, or an error.
468 asn1_get_bit_der (const unsigned char *der, int der_len,
469 int *ret_len, unsigned char *str, int str_size,
472 int len_len = 0, len_byte;
475 return ASN1_GENERIC_ERROR;
477 len_byte = asn1_get_length_der (der, der_len, &len_len) - 1;
479 return ASN1_DER_ERROR;
481 *ret_len = len_byte + len_len + 1;
482 *bit_len = len_byte * 8 - der[len_len];
485 return ASN1_DER_ERROR;
487 if (str_size >= len_byte)
489 if (len_byte > 0 && str)
490 memcpy (str, der + len_len + 1, len_byte);
494 return ASN1_MEM_ERROR;
500 /* tag_len: the total tag length (explicit+inner)
501 * inner_tag_len: the inner_tag length
504 _asn1_extract_tag_der (asn1_node node, const unsigned char *der, int der_len,
505 int *tag_len, int *inner_tag_len, unsigned flags)
508 int counter, len2, len3, is_tag_implicit;
510 unsigned long tag, tag_implicit = 0;
511 unsigned char class, class2, class_implicit = 0;
514 return ASN1_GENERIC_ERROR;
516 counter = is_tag_implicit = 0;
518 if (node->type & CONST_TAG)
523 if (type_field (p->type) == ASN1_ETYPE_TAG)
525 if (p->type & CONST_APPLICATION)
526 class2 = ASN1_CLASS_APPLICATION;
527 else if (p->type & CONST_UNIVERSAL)
528 class2 = ASN1_CLASS_UNIVERSAL;
529 else if (p->type & CONST_PRIVATE)
530 class2 = ASN1_CLASS_PRIVATE;
532 class2 = ASN1_CLASS_CONTEXT_SPECIFIC;
534 if (p->type & CONST_EXPLICIT)
537 (der + counter, der_len, &class, &len2,
538 &tag) != ASN1_SUCCESS)
539 return ASN1_DER_ERROR;
541 DECR_LEN(der_len, len2);
544 if (flags & ASN1_DECODE_FLAG_STRICT_DER)
546 asn1_get_length_der (der + counter, der_len,
550 asn1_get_length_ber (der + counter, der_len,
553 return ASN1_DER_ERROR;
555 DECR_LEN(der_len, len2);
558 if (!is_tag_implicit)
560 if ((class != (class2 | ASN1_CLASS_STRUCTURED)) ||
561 (tag != strtoul ((char *) p->value, NULL, 10)))
562 return ASN1_TAG_ERROR;
565 { /* ASN1_TAG_IMPLICIT */
566 if ((class != class_implicit) || (tag != tag_implicit))
567 return ASN1_TAG_ERROR;
572 { /* ASN1_TAG_IMPLICIT */
573 if (!is_tag_implicit)
575 if ((type_field (node->type) == ASN1_ETYPE_SEQUENCE) ||
576 (type_field (node->type) == ASN1_ETYPE_SEQUENCE_OF)
577 || (type_field (node->type) == ASN1_ETYPE_SET)
578 || (type_field (node->type) == ASN1_ETYPE_SET_OF))
579 class2 |= ASN1_CLASS_STRUCTURED;
580 class_implicit = class2;
581 tag_implicit = strtoul ((char *) p->value, NULL, 10);
593 (der + counter, der_len, &class, &len2,
594 &tag) != ASN1_SUCCESS)
595 return ASN1_DER_ERROR;
597 DECR_LEN(der_len, len2);
599 if ((class != class_implicit) || (tag != tag_implicit))
601 if (type_field (node->type) == ASN1_ETYPE_OCTET_STRING)
603 class_implicit |= ASN1_CLASS_STRUCTURED;
604 if ((class != class_implicit) || (tag != tag_implicit))
605 return ASN1_TAG_ERROR;
608 return ASN1_TAG_ERROR;
613 unsigned type = type_field (node->type);
614 if (type == ASN1_ETYPE_TAG)
623 (der + counter, der_len, &class, &len2,
624 &tag) != ASN1_SUCCESS)
625 return ASN1_DER_ERROR;
627 DECR_LEN(der_len, len2);
631 case ASN1_ETYPE_NULL:
632 case ASN1_ETYPE_BOOLEAN:
633 case ASN1_ETYPE_INTEGER:
634 case ASN1_ETYPE_ENUMERATED:
635 case ASN1_ETYPE_OBJECT_ID:
636 case ASN1_ETYPE_GENERALSTRING:
637 case ASN1_ETYPE_NUMERIC_STRING:
638 case ASN1_ETYPE_IA5_STRING:
639 case ASN1_ETYPE_TELETEX_STRING:
640 case ASN1_ETYPE_PRINTABLE_STRING:
641 case ASN1_ETYPE_UNIVERSAL_STRING:
642 case ASN1_ETYPE_BMP_STRING:
643 case ASN1_ETYPE_UTF8_STRING:
644 case ASN1_ETYPE_VISIBLE_STRING:
645 case ASN1_ETYPE_BIT_STRING:
646 case ASN1_ETYPE_SEQUENCE:
647 case ASN1_ETYPE_SEQUENCE_OF:
649 case ASN1_ETYPE_SET_OF:
650 case ASN1_ETYPE_GENERALIZED_TIME:
651 case ASN1_ETYPE_UTC_TIME:
652 if ((class != _asn1_tags[type].class)
653 || (tag != _asn1_tags[type].tag))
654 return ASN1_DER_ERROR;
657 case ASN1_ETYPE_OCTET_STRING:
658 /* OCTET STRING is handled differently to allow
659 * BER encodings (structured class). */
660 if (((class != ASN1_CLASS_UNIVERSAL)
661 && (class != (ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED)))
662 || (tag != ASN1_TAG_OCTET_STRING))
663 return ASN1_DER_ERROR;
668 case ASN1_ETYPE_CHOICE:
672 return ASN1_DER_ERROR;
680 *inner_tag_len = len2;
688 extract_tag_der_recursive(asn1_node node, const unsigned char *der, int der_len,
689 int *ret_len, int *inner_len, unsigned flags)
692 int ris = ASN1_DER_ERROR;
694 if (type_field (node->type) == ASN1_ETYPE_CHOICE)
699 ris = _asn1_extract_tag_der (p, der, der_len, ret_len, inner_len, flags);
700 if (ris == ASN1_SUCCESS)
709 return _asn1_extract_tag_der (node, der, der_len, ret_len, inner_len, flags);
713 _asn1_delete_not_used (asn1_node node)
718 return ASN1_ELEMENT_NOT_FOUND;
723 if (p->type & CONST_NOT_USED)
728 p2 = _asn1_find_left (p);
730 p2 = _asn1_find_up (p);
732 asn1_delete_structure (&p);
737 break; /* reach node */
753 p = _asn1_find_up (p);
772 _asn1_get_indefinite_length_string (const unsigned char *der,
773 int der_len, int *len)
775 int len2, len3, counter, indefinite;
780 counter = indefinite = 0;
784 if (HAVE_TWO(der_len) && (der[counter] == 0) && (der[counter + 1] == 0))
787 DECR_LEN(der_len, 2);
797 (der + counter, der_len, &class, &len2,
798 &tag) != ASN1_SUCCESS)
799 return ASN1_DER_ERROR;
801 DECR_LEN(der_len, len2);
804 len2 = asn1_get_length_der (der + counter, der_len, &len3);
806 return ASN1_DER_ERROR;
812 DECR_LEN(der_len, 1);
816 counter += len2 + len3;
817 DECR_LEN(der_len, len2+len3);
828 static void delete_unneeded_choice_fields(asn1_node p)
835 asn1_delete_structure (&p2);
842 * @element: pointer to an ASN1 structure.
843 * @ider: vector that contains the DER encoding.
844 * @max_ider_len: pointer to an integer giving the information about the
845 * maximal number of bytes occupied by *@ider. The real size of the DER
846 * encoding is returned through this pointer.
847 * @flags: flags controlling the behaviour of the function.
848 * @errorDescription: null-terminated string contains details when an
851 * Fill the structure *@element with values of a DER encoding string. The
852 * structure must just be created with function asn1_create_element().
854 * If %ASN1_DECODE_FLAG_ALLOW_PADDING flag is set then the function will ignore
855 * padding after the decoded DER data. Upon a successful return the value of
856 * *@max_ider_len will be set to the number of bytes decoded.
858 * If %ASN1_DECODE_FLAG_STRICT_DER flag is set then the function will
859 * not decode any BER-encoded elements.
861 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
862 * if @ELEMENT is %NULL, and %ASN1_TAG_ERROR or
863 * %ASN1_DER_ERROR if the der encoding doesn't match the structure
864 * name (*@ELEMENT deleted).
867 asn1_der_decoding2 (asn1_node *element, const void *ider, int *max_ider_len,
868 unsigned int flags, char *errorDescription)
870 asn1_node node, p, p2, p3;
872 int counter, len2, len3, len4, move, ris, tlen;
873 struct node_tail_cache_st tcache = {NULL, NULL};
877 int indefinite, result, total_len = *max_ider_len, ider_len = *max_ider_len;
880 const unsigned char *ptag;
881 const unsigned char *der = ider;
885 if (errorDescription != NULL)
886 errorDescription[0] = 0;
889 return ASN1_ELEMENT_NOT_FOUND;
891 if (node->type & CONST_OPTION)
893 result = ASN1_GENERIC_ERROR;
908 if (p->type & CONST_SET)
910 p2 = _asn1_find_up (p);
914 if (HAVE_TWO(ider_len) && !der[counter] && !der[counter + 1])
919 DECR_LEN(ider_len, 2);
923 else if (counter == len2)
929 else if (counter > len2)
931 result = ASN1_DER_ERROR;
938 if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
941 extract_tag_der_recursive (p2, der + counter,
942 ider_len, &len2, NULL, flags);
943 if (ris == ASN1_SUCCESS)
945 p2->type &= ~CONST_NOT_USED;
954 result = ASN1_DER_ERROR;
960 /* the position in the DER structure this starts */
962 p->end = total_len - 1;
964 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
966 p2 = _asn1_find_up (p);
978 if (p->type & CONST_OPTION)
979 asn1_delete_structure (&p);
986 if (type_field (p->type) == ASN1_ETYPE_CHOICE)
991 extract_tag_der_recursive (p->down, der + counter,
992 ider_len, &len2, NULL, flags);
994 if (ris == ASN1_SUCCESS)
996 delete_unneeded_choice_fields(p->down);
999 else if (ris == ASN1_ERROR_TYPE_ANY)
1001 result = ASN1_ERROR_TYPE_ANY;
1008 asn1_delete_structure (&p2);
1012 if (p->down == NULL)
1014 if (!(p->type & CONST_OPTION))
1016 result = ASN1_DER_ERROR;
1021 else if (type_field (p->type) != ASN1_ETYPE_CHOICE)
1027 if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1029 p2 = _asn1_find_up (p);
1030 len2 = p2->tmp_ival;
1032 if ((len2 != -1) && (counter > len2))
1033 ris = ASN1_TAG_ERROR;
1036 if (ris == ASN1_SUCCESS)
1038 extract_tag_der_recursive (p, der + counter, ider_len,
1039 &tag_len, &inner_tag_len, flags);
1041 if (ris != ASN1_SUCCESS)
1043 if (p->type & CONST_OPTION)
1045 p->type |= CONST_NOT_USED;
1048 else if (p->type & CONST_DEFAULT)
1050 _asn1_set_value (p, NULL, 0);
1055 if (errorDescription != NULL)
1056 _asn1_error_description_tag_error (p, errorDescription);
1058 result = ASN1_TAG_ERROR;
1065 DECR_LEN(ider_len, tag_len);
1070 if (ris == ASN1_SUCCESS)
1072 switch (type_field (p->type))
1074 case ASN1_ETYPE_NULL:
1075 DECR_LEN(ider_len, 1);
1078 result = ASN1_DER_ERROR;
1085 case ASN1_ETYPE_BOOLEAN:
1086 DECR_LEN(ider_len, 2);
1088 if (der[counter++] != 1)
1090 result = ASN1_DER_ERROR;
1094 if (der[counter++] == 0)
1095 _asn1_set_value (p, "F", 1);
1097 _asn1_set_value (p, "T", 1);
1100 case ASN1_ETYPE_INTEGER:
1101 case ASN1_ETYPE_ENUMERATED:
1103 asn1_get_length_der (der + counter, ider_len, &len3);
1106 result = ASN1_DER_ERROR;
1111 DECR_LEN(ider_len, len3+len2);
1113 _asn1_set_value (p, der + counter, len3 + len2);
1114 counter += len3 + len2;
1117 case ASN1_ETYPE_OBJECT_ID:
1119 asn1_get_object_id_der (der + counter, ider_len, &len2,
1120 temp, sizeof (temp));
1121 if (result != ASN1_SUCCESS)
1127 DECR_LEN(ider_len, len2);
1129 tlen = strlen (temp);
1131 _asn1_set_value (p, temp, tlen + 1);
1136 case ASN1_ETYPE_GENERALIZED_TIME:
1137 case ASN1_ETYPE_UTC_TIME:
1139 _asn1_get_time_der (type_field (p->type), der + counter, ider_len, &len2, temp,
1140 sizeof (temp) - 1, flags);
1141 if (result != ASN1_SUCCESS)
1147 DECR_LEN(ider_len, len2);
1149 tlen = strlen (temp);
1151 _asn1_set_value (p, temp, tlen);
1156 case ASN1_ETYPE_OCTET_STRING:
1157 if (counter < inner_tag_len)
1159 result = ASN1_DER_ERROR;
1164 ptag = der + counter - inner_tag_len;
1165 if (flags & ASN1_DECODE_FLAG_STRICT_DER || !(ptag[0] & ASN1_CLASS_STRUCTURED))
1168 asn1_get_length_der (der + counter, ider_len, &len3);
1171 result = ASN1_DER_ERROR;
1176 DECR_LEN(ider_len, len3+len2);
1178 _asn1_set_value (p, der + counter, len3 + len2);
1179 counter += len3 + len2;
1183 unsigned dflags = 0, vlen, ber_len;
1185 if (ptag[0] & ASN1_CLASS_STRUCTURED)
1186 dflags |= DECODE_FLAG_INDEFINITE;
1188 result = _asn1_decode_simple_ber(type_field (p->type), der+counter, ider_len, &ptmp, &vlen, &ber_len, dflags);
1189 if (result != ASN1_SUCCESS)
1195 DECR_LEN(ider_len, ber_len);
1197 _asn1_set_value_lv (p, ptmp, vlen);
1204 case ASN1_ETYPE_GENERALSTRING:
1205 case ASN1_ETYPE_NUMERIC_STRING:
1206 case ASN1_ETYPE_IA5_STRING:
1207 case ASN1_ETYPE_TELETEX_STRING:
1208 case ASN1_ETYPE_PRINTABLE_STRING:
1209 case ASN1_ETYPE_UNIVERSAL_STRING:
1210 case ASN1_ETYPE_BMP_STRING:
1211 case ASN1_ETYPE_UTF8_STRING:
1212 case ASN1_ETYPE_VISIBLE_STRING:
1213 case ASN1_ETYPE_BIT_STRING:
1215 asn1_get_length_der (der + counter, ider_len, &len3);
1218 result = ASN1_DER_ERROR;
1223 DECR_LEN(ider_len, len3+len2);
1225 _asn1_set_value (p, der + counter, len3 + len2);
1226 counter += len3 + len2;
1229 case ASN1_ETYPE_SEQUENCE:
1230 case ASN1_ETYPE_SET:
1236 { /* indefinite length method */
1237 DECR_LEN(ider_len, 2);
1238 if ((der[counter]) || der[counter + 1])
1240 result = ASN1_DER_ERROR;
1247 { /* definite length method */
1248 if (len2 != counter)
1250 result = ASN1_DER_ERROR;
1258 { /* move==DOWN || move==RIGHT */
1260 asn1_get_length_der (der + counter, ider_len, &len2);
1261 if (IS_ERR(len3, flags))
1263 result = ASN1_DER_ERROR;
1268 DECR_LEN(ider_len, len2);
1273 p->tmp_ival = counter + len3;
1281 if (type_field (p2->type) != ASN1_ETYPE_TAG)
1284 asn1_delete_structure (&p2);
1293 { /* indefinite length method */
1299 case ASN1_ETYPE_SEQUENCE_OF:
1300 case ASN1_ETYPE_SET_OF:
1305 { /* indefinite length method */
1306 if (!HAVE_TWO(ider_len) || ((der[counter]) || der[counter + 1]))
1308 _asn1_append_sequence_set (p, &tcache);
1315 tcache.tail = NULL; /* finished decoding this structure */
1317 DECR_LEN(ider_len, 2);
1321 { /* definite length method */
1324 _asn1_append_sequence_set (p, &tcache);
1331 tcache.tail = NULL; /* finished decoding this structure */
1334 if (len2 != counter)
1336 result = ASN1_DER_ERROR;
1343 { /* move==DOWN || move==RIGHT */
1345 asn1_get_length_der (der + counter, ider_len, &len2);
1346 if (IS_ERR(len3, flags))
1348 result = ASN1_DER_ERROR;
1353 DECR_LEN(ider_len, len2);
1358 { /* definite length method */
1359 p->tmp_ival = counter + len3;
1362 { /* indefinite length method */
1369 result = ASN1_DER_ERROR;
1374 while ((type_field (p2->type) == ASN1_ETYPE_TAG)
1375 || (type_field (p2->type) == ASN1_ETYPE_SIZE))
1377 if (p2->right == NULL)
1378 _asn1_append_sequence_set (p, &tcache);
1384 case ASN1_ETYPE_ANY:
1385 /* Check indefinite lenth method in an EXPLICIT TAG */
1387 if (!(flags & ASN1_DECODE_FLAG_STRICT_DER) && (p->type & CONST_TAG) &&
1388 tag_len == 2 && (der[counter - 1] == 0x80))
1393 if (asn1_get_tag_der
1394 (der + counter, ider_len, &class, &len2,
1395 &tag) != ASN1_SUCCESS)
1397 result = ASN1_DER_ERROR;
1402 DECR_LEN(ider_len, len2);
1405 asn1_get_length_der (der + counter + len2,
1407 if (IS_ERR(len4, flags))
1409 result = ASN1_DER_ERROR;
1413 if (len4 != -1) /* definite */
1417 DECR_LEN(ider_len, len4+len3);
1418 _asn1_set_value_lv (p, der + counter, len2 + len3);
1419 counter += len2 + len3;
1422 { /* indefinite length */
1423 ider_len += len2; /* undo DECR_LEN */
1427 result = ASN1_DER_ERROR;
1433 _asn1_get_indefinite_length_string (der + counter, ider_len, &len2);
1434 if (result != ASN1_SUCCESS)
1440 DECR_LEN(ider_len, len2);
1441 _asn1_set_value_lv (p, der + counter, len2);
1446 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1447 an indefinite length method. */
1450 DECR_LEN(ider_len, 2);
1451 if (!der[counter] && !der[counter + 1])
1457 result = ASN1_DER_ERROR;
1466 move = (move == UP) ? RIGHT : DOWN;
1473 p->end = counter - 1;
1476 if (p == node && move != DOWN)
1486 if ((move == RIGHT) && !(p->type & CONST_SET))
1494 p = _asn1_find_up (p);
1497 _asn1_delete_not_used (*element);
1499 if ((ider_len < 0) ||
1500 (!(flags & ASN1_DECODE_FLAG_ALLOW_PADDING) && (ider_len != 0)))
1503 result = ASN1_DER_ERROR;
1507 *max_ider_len = total_len - ider_len;
1509 return ASN1_SUCCESS;
1512 asn1_delete_structure (element);
1518 * asn1_der_decoding:
1519 * @element: pointer to an ASN1 structure.
1520 * @ider: vector that contains the DER encoding.
1521 * @ider_len: number of bytes of *@ider: @ider[0]..@ider[len-1].
1522 * @errorDescription: null-terminated string contains details when an
1525 * Fill the structure *@element with values of a DER encoding
1526 * string. The structure must just be created with function
1527 * asn1_create_element().
1529 * Note that the *@element variable is provided as a pointer for
1530 * historical reasons.
1532 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1533 * if @ELEMENT is %NULL, and %ASN1_TAG_ERROR or
1534 * %ASN1_DER_ERROR if the der encoding doesn't match the structure
1535 * name (*@ELEMENT deleted).
1538 asn1_der_decoding (asn1_node * element, const void *ider, int ider_len,
1539 char *errorDescription)
1541 return asn1_der_decoding2 (element, ider, &ider_len, 0, errorDescription);
1545 * asn1_der_decoding_element:
1546 * @structure: pointer to an ASN1 structure
1547 * @elementName: name of the element to fill
1548 * @ider: vector that contains the DER encoding of the whole structure.
1549 * @len: number of bytes of *der: der[0]..der[len-1]
1550 * @errorDescription: null-terminated string contains details when an
1553 * Fill the element named @ELEMENTNAME with values of a DER encoding
1554 * string. The structure must just be created with function
1555 * asn1_create_element(). The DER vector must contain the encoding
1556 * string of the whole @STRUCTURE. If an error occurs during the
1557 * decoding procedure, the *@STRUCTURE is deleted and set equal to
1560 * This function is deprecated and may just be an alias to asn1_der_decoding
1561 * in future versions. Use asn1_der_decoding() instead.
1563 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1564 * if ELEMENT is %NULL or @elementName == NULL, and
1565 * %ASN1_TAG_ERROR or %ASN1_DER_ERROR if the der encoding doesn't
1566 * match the structure @structure (*ELEMENT deleted).
1569 asn1_der_decoding_element (asn1_node * structure, const char *elementName,
1570 const void *ider, int len, char *errorDescription)
1572 return asn1_der_decoding(structure, ider, len, errorDescription);
1576 * asn1_der_decoding_startEnd:
1577 * @element: pointer to an ASN1 element
1578 * @ider: vector that contains the DER encoding.
1579 * @ider_len: number of bytes of *@ider: @ider[0]..@ider[len-1]
1580 * @name_element: an element of NAME structure.
1581 * @start: the position of the first byte of NAME_ELEMENT decoding
1583 * @end: the position of the last byte of NAME_ELEMENT decoding
1586 * Find the start and end point of an element in a DER encoding
1587 * string. I mean that if you have a der encoding and you have already
1588 * used the function asn1_der_decoding() to fill a structure, it may
1589 * happen that you want to find the piece of string concerning an
1590 * element of the structure.
1592 * One example is the sequence "tbsCertificate" inside an X509
1595 * Note that since libtasn1 3.7 the @ider and @ider_len parameters
1596 * can be omitted, if the element is already decoded using asn1_der_decoding().
1598 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1599 * if ELEMENT is %asn1_node EMPTY or @name_element is not a valid
1600 * element, %ASN1_TAG_ERROR or %ASN1_DER_ERROR if the der encoding
1601 * doesn't match the structure ELEMENT.
1604 asn1_der_decoding_startEnd (asn1_node element, const void *ider, int ider_len,
1605 const char *name_element, int *start, int *end)
1607 asn1_node node, node_to_find;
1608 int result = ASN1_DER_ERROR;
1613 return ASN1_ELEMENT_NOT_FOUND;
1615 node_to_find = asn1_find_node (node, name_element);
1617 if (node_to_find == NULL)
1618 return ASN1_ELEMENT_NOT_FOUND;
1620 *start = node_to_find->start;
1621 *end = node_to_find->end;
1623 if (*start == 0 && *end == 0)
1625 if (ider == NULL || ider_len == 0)
1626 return ASN1_GENERIC_ERROR;
1628 /* it seems asn1_der_decoding() wasn't called before. Do it now */
1629 result = asn1_der_decoding (&node, ider, ider_len, NULL);
1630 if (result != ASN1_SUCCESS)
1636 node_to_find = asn1_find_node (node, name_element);
1637 if (node_to_find == NULL)
1638 return ASN1_ELEMENT_NOT_FOUND;
1640 *start = node_to_find->start;
1641 *end = node_to_find->end;
1645 return ASN1_GENERIC_ERROR;
1647 return ASN1_SUCCESS;
1651 * asn1_expand_any_defined_by:
1652 * @definitions: ASN1 definitions
1653 * @element: pointer to an ASN1 structure
1655 * Expands every "ANY DEFINED BY" element of a structure created from
1656 * a DER decoding process (asn1_der_decoding function). The element
1657 * ANY must be defined by an OBJECT IDENTIFIER. The type used to
1658 * expand the element ANY is the first one following the definition of
1659 * the actual value of the OBJECT IDENTIFIER.
1661 * Returns: %ASN1_SUCCESS if Substitution OK, %ASN1_ERROR_TYPE_ANY if
1662 * some "ANY DEFINED BY" element couldn't be expanded due to a
1663 * problem in OBJECT_ID -> TYPE association, or other error codes
1664 * depending on DER decoding.
1667 asn1_expand_any_defined_by (asn1_node definitions, asn1_node * element)
1669 char name[2 * ASN1_MAX_NAME_SIZE + 1],
1670 value[ASN1_MAX_NAME_SIZE];
1671 int retCode = ASN1_SUCCESS, result;
1672 int len, len2, len3;
1673 asn1_node p, p2, p3, aux = NULL;
1674 char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
1675 const char *definitionsName;
1677 if ((definitions == NULL) || (*element == NULL))
1678 return ASN1_ELEMENT_NOT_FOUND;
1680 definitionsName = definitions->name;
1686 switch (type_field (p->type))
1688 case ASN1_ETYPE_ANY:
1689 if ((p->type & CONST_DEFINED_BY) && (p->value))
1691 /* search the "DEF_BY" element */
1693 while ((p2) && (type_field (p2->type) != ASN1_ETYPE_CONSTANT))
1698 retCode = ASN1_ERROR_TYPE_ANY;
1702 p3 = _asn1_find_up (p);
1706 retCode = ASN1_ERROR_TYPE_ANY;
1713 if (!(strcmp (p3->name, p2->name)))
1718 if ((!p3) || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID) ||
1719 (p3->value == NULL))
1722 p3 = _asn1_find_up (p);
1723 p3 = _asn1_find_up (p3);
1727 retCode = ASN1_ERROR_TYPE_ANY;
1735 if (!(strcmp (p3->name, p2->name)))
1740 if ((!p3) || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID)
1741 || (p3->value == NULL))
1743 retCode = ASN1_ERROR_TYPE_ANY;
1748 /* search the OBJECT_ID into definitions */
1749 p2 = definitions->down;
1752 if ((type_field (p2->type) == ASN1_ETYPE_OBJECT_ID) &&
1753 (p2->type & CONST_ASSIGN))
1755 snprintf(name, sizeof(name), "%s.%s", definitionsName, p2->name);
1757 len = ASN1_MAX_NAME_SIZE;
1759 asn1_read_value (definitions, name, value, &len);
1761 if ((result == ASN1_SUCCESS)
1762 && (!_asn1_strcmp (p3->value, value)))
1764 p2 = p2->right; /* pointer to the structure to
1765 use for expansion */
1766 while ((p2) && (p2->type & CONST_ASSIGN))
1771 snprintf(name, sizeof(name), "%s.%s", definitionsName, p2->name);
1774 asn1_create_element (definitions, name, &aux);
1775 if (result == ASN1_SUCCESS)
1777 _asn1_cpy_name (aux, p);
1779 asn1_get_length_der (p->value,
1780 p->value_len, &len3);
1782 return ASN1_DER_ERROR;
1785 asn1_der_decoding (&aux, p->value + len3,
1788 if (result == ASN1_SUCCESS)
1791 _asn1_set_right (aux, p->right);
1792 _asn1_set_right (p, aux);
1794 result = asn1_delete_structure (&p);
1795 if (result == ASN1_SUCCESS)
1802 { /* error with asn1_delete_structure */
1803 asn1_delete_structure (&aux);
1809 { /* error with asn1_der_decoding */
1815 { /* error with asn1_create_element */
1821 { /* error with the pointer to the structure to exapand */
1822 retCode = ASN1_ERROR_TYPE_ANY;
1832 retCode = ASN1_ERROR_TYPE_ANY;
1847 else if (p == *element)
1858 p = _asn1_find_up (p);
1877 * asn1_expand_octet_string:
1878 * @definitions: ASN1 definitions
1879 * @element: pointer to an ASN1 structure
1880 * @octetName: name of the OCTECT STRING field to expand.
1881 * @objectName: name of the OBJECT IDENTIFIER field to use to define
1882 * the type for expansion.
1884 * Expands an "OCTET STRING" element of a structure created from a DER
1885 * decoding process (the asn1_der_decoding() function). The type used
1886 * for expansion is the first one following the definition of the
1887 * actual value of the OBJECT IDENTIFIER indicated by OBJECTNAME.
1889 * Returns: %ASN1_SUCCESS if substitution OK, %ASN1_ELEMENT_NOT_FOUND
1890 * if @objectName or @octetName are not correct,
1891 * %ASN1_VALUE_NOT_VALID if it wasn't possible to find the type to
1892 * use for expansion, or other errors depending on DER decoding.
1895 asn1_expand_octet_string (asn1_node definitions, asn1_node * element,
1896 const char *octetName, const char *objectName)
1898 char name[2 * ASN1_MAX_NAME_SIZE + 1], value[ASN1_MAX_NAME_SIZE];
1899 int retCode = ASN1_SUCCESS, result;
1900 int len, len2, len3;
1901 asn1_node p2, aux = NULL;
1902 asn1_node octetNode = NULL, objectNode = NULL;
1903 char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
1905 if ((definitions == NULL) || (*element == NULL))
1906 return ASN1_ELEMENT_NOT_FOUND;
1908 octetNode = asn1_find_node (*element, octetName);
1909 if (octetNode == NULL)
1910 return ASN1_ELEMENT_NOT_FOUND;
1911 if (type_field (octetNode->type) != ASN1_ETYPE_OCTET_STRING)
1912 return ASN1_ELEMENT_NOT_FOUND;
1913 if (octetNode->value == NULL)
1914 return ASN1_VALUE_NOT_FOUND;
1916 objectNode = asn1_find_node (*element, objectName);
1917 if (objectNode == NULL)
1918 return ASN1_ELEMENT_NOT_FOUND;
1920 if (type_field (objectNode->type) != ASN1_ETYPE_OBJECT_ID)
1921 return ASN1_ELEMENT_NOT_FOUND;
1923 if (objectNode->value == NULL)
1924 return ASN1_VALUE_NOT_FOUND;
1927 /* search the OBJECT_ID into definitions */
1928 p2 = definitions->down;
1931 if ((type_field (p2->type) == ASN1_ETYPE_OBJECT_ID) &&
1932 (p2->type & CONST_ASSIGN))
1934 strcpy (name, definitions->name);
1936 strcat (name, p2->name);
1938 len = sizeof (value);
1939 result = asn1_read_value (definitions, name, value, &len);
1941 if ((result == ASN1_SUCCESS)
1942 && (!_asn1_strcmp (objectNode->value, value)))
1945 p2 = p2->right; /* pointer to the structure to
1946 use for expansion */
1947 while ((p2) && (p2->type & CONST_ASSIGN))
1952 strcpy (name, definitions->name);
1954 strcat (name, p2->name);
1956 result = asn1_create_element (definitions, name, &aux);
1957 if (result == ASN1_SUCCESS)
1959 _asn1_cpy_name (aux, octetNode);
1961 asn1_get_length_der (octetNode->value,
1962 octetNode->value_len, &len3);
1964 return ASN1_DER_ERROR;
1967 asn1_der_decoding (&aux, octetNode->value + len3,
1968 len2, errorDescription);
1969 if (result == ASN1_SUCCESS)
1972 _asn1_set_right (aux, octetNode->right);
1973 _asn1_set_right (octetNode, aux);
1975 result = asn1_delete_structure (&octetNode);
1976 if (result == ASN1_SUCCESS)
1982 { /* error with asn1_delete_structure */
1983 asn1_delete_structure (&aux);
1989 { /* error with asn1_der_decoding */
1995 { /* error with asn1_create_element */
2001 { /* error with the pointer to the structure to exapand */
2002 retCode = ASN1_VALUE_NOT_VALID;
2013 retCode = ASN1_VALUE_NOT_VALID;
2019 * _asn1_decode_simple_der:
2020 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2021 * @der: the encoded string
2022 * @_der_len: the bytes of the encoded string
2023 * @str: a pointer to the data
2024 * @str_len: the length of the data
2025 * @dflags: DECODE_FLAG_*
2027 * Decodes a simple DER encoded type (e.g. a string, which is not constructed).
2028 * The output is a pointer inside the @der.
2030 * Returns: %ASN1_SUCCESS if successful or an error value.
2033 _asn1_decode_simple_der (unsigned int etype, const unsigned char *der,
2034 unsigned int _der_len, const unsigned char **str,
2035 unsigned int *str_len, unsigned dflags)
2037 int tag_len, len_len;
2038 const unsigned char *p;
2039 int der_len = _der_len;
2040 unsigned char class;
2044 if (der == NULL || der_len == 0)
2045 return ASN1_VALUE_NOT_VALID;
2047 if (ETYPE_OK (etype) == 0 || ETYPE_IS_STRING(etype) == 0)
2048 return ASN1_VALUE_NOT_VALID;
2050 /* doesn't handle constructed classes */
2051 class = ETYPE_CLASS(etype);
2052 if (class != ASN1_CLASS_UNIVERSAL)
2053 return ASN1_VALUE_NOT_VALID;
2057 if (dflags & DECODE_FLAG_HAVE_TAG)
2059 ret = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag);
2060 if (ret != ASN1_SUCCESS)
2063 if (class != ETYPE_CLASS (etype) || tag != ETYPE_TAG (etype))
2066 return ASN1_DER_ERROR;
2072 return ASN1_DER_ERROR;
2075 ret = asn1_get_length_der (p, der_len, &len_len);
2077 return ASN1_DER_ERROR;
2082 return ASN1_DER_ERROR;
2087 return ASN1_SUCCESS;
2091 * asn1_decode_simple_der:
2092 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2093 * @der: the encoded string
2094 * @_der_len: the bytes of the encoded string
2095 * @str: a pointer to the data
2096 * @str_len: the length of the data
2098 * Decodes a simple DER encoded type (e.g. a string, which is not constructed).
2099 * The output is a pointer inside the @der.
2101 * Returns: %ASN1_SUCCESS if successful or an error value.
2104 asn1_decode_simple_der (unsigned int etype, const unsigned char *der,
2105 unsigned int _der_len, const unsigned char **str,
2106 unsigned int *str_len)
2108 return _asn1_decode_simple_der(etype, der, _der_len, str, str_len, DECODE_FLAG_HAVE_TAG);
2111 static int append(uint8_t **dst, unsigned *dst_size, const unsigned char *src, unsigned src_size)
2113 *dst = _asn1_realloc(*dst, *dst_size+src_size);
2115 return ASN1_MEM_ERROR;
2116 memcpy(*dst + *dst_size, src, src_size);
2117 *dst_size += src_size;
2118 return ASN1_SUCCESS;
2122 * _asn1_decode_simple_ber:
2123 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2124 * @der: the encoded string
2125 * @_der_len: the bytes of the encoded string
2126 * @str: a pointer to the data
2127 * @str_len: the length of the data
2128 * @ber_len: the total length occupied by BER (may be %NULL)
2129 * @have_tag: whether a DER tag is included
2131 * Decodes a BER encoded type. The output is an allocated value
2132 * of the data. This decodes BER STRINGS only. Other types are
2135 * Returns: %ASN1_SUCCESS if successful or an error value.
2138 _asn1_decode_simple_ber (unsigned int etype, const unsigned char *der,
2139 unsigned int _der_len, unsigned char **str,
2140 unsigned int *str_len, unsigned int *ber_len,
2143 int tag_len, len_len;
2144 const unsigned char *p;
2145 int der_len = _der_len;
2146 uint8_t *total = NULL;
2147 unsigned total_size = 0;
2148 unsigned char class;
2150 unsigned char *out = NULL;
2151 const unsigned char *cout = NULL;
2155 if (ber_len) *ber_len = 0;
2157 if (der == NULL || der_len == 0)
2160 return ASN1_VALUE_NOT_VALID;
2163 if (ETYPE_OK (etype) == 0)
2166 return ASN1_VALUE_NOT_VALID;
2169 /* doesn't handle constructed + definite classes */
2170 class = ETYPE_CLASS (etype);
2171 if (class != ASN1_CLASS_UNIVERSAL)
2174 return ASN1_VALUE_NOT_VALID;
2179 if (dflags & DECODE_FLAG_HAVE_TAG)
2181 result = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag);
2182 if (result != ASN1_SUCCESS)
2188 if (tag != ETYPE_TAG (etype))
2191 return ASN1_DER_ERROR;
2196 DECR_LEN(der_len, tag_len);
2198 if (ber_len) *ber_len += tag_len;
2201 /* indefinite constructed */
2202 if (((dflags & DECODE_FLAG_INDEFINITE) || class == ASN1_CLASS_STRUCTURED) && ETYPE_IS_STRING(etype))
2206 DECR_LEN(der_len, len_len);
2210 result = ASN1_DER_ERROR;
2216 if (ber_len) *ber_len += len_len;
2218 /* decode the available octet strings */
2223 result = asn1_decode_simple_ber(etype, p, der_len, &out, &out_len, &tmp_len);
2224 if (result != ASN1_SUCCESS)
2231 DECR_LEN(der_len, tmp_len);
2233 if (ber_len) *ber_len += tmp_len;
2235 DECR_LEN(der_len, 2); /* we need the EOC */
2239 result = append(&total, &total_size, out, out_len);
2240 if (result != ASN1_SUCCESS)
2250 if (p[0] == 0 && p[1] == 0) /* EOC */
2252 if (ber_len) *ber_len += 2;
2262 result = ASN1_DER_ERROR;
2268 else if (class == ETYPE_CLASS(etype))
2272 result = asn1_get_length_der (p, der_len, &len_len);
2276 result = ASN1_DER_ERROR;
2279 *ber_len += result + len_len;
2282 /* non-string values are decoded as DER */
2283 result = _asn1_decode_simple_der(etype, der, _der_len, &cout, &out_len, dflags);
2284 if (result != ASN1_SUCCESS)
2290 result = append(&total, &total_size, cout, out_len);
2291 if (result != ASN1_SUCCESS)
2300 result = ASN1_DER_ERROR;
2305 *str_len = total_size;
2307 return ASN1_SUCCESS;
2315 * asn1_decode_simple_ber:
2316 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2317 * @der: the encoded string
2318 * @_der_len: the bytes of the encoded string
2319 * @str: a pointer to the data
2320 * @str_len: the length of the data
2321 * @ber_len: the total length occupied by BER (may be %NULL)
2323 * Decodes a BER encoded type. The output is an allocated value
2324 * of the data. This decodes BER STRINGS only. Other types are
2327 * Returns: %ASN1_SUCCESS if successful or an error value.
2330 asn1_decode_simple_ber (unsigned int etype, const unsigned char *der,
2331 unsigned int _der_len, unsigned char **str,
2332 unsigned int *str_len, unsigned int *ber_len)
2334 return _asn1_decode_simple_ber(etype, der, _der_len, str, str_len, ber_len, DECODE_FLAG_HAVE_TAG);