Imported Upstream version 4.9
[platform/upstream/libtasn1.git] / lib / decoding.c
1 /*
2  * Copyright (C) 2002-2016 Free Software Foundation, Inc.
3  *
4  * This file is part of LIBTASN1.
5  *
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.
10  *
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.
15  *
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
19  * 02110-1301, USA
20  */
21
22
23 /*****************************************************/
24 /* File: decoding.c                                  */
25 /* Description: Functions to manage DER decoding     */
26 /*****************************************************/
27
28 #include <int.h>
29 #include <parser_aux.h>
30 #include <gstr.h>
31 #include <structure.h>
32 #include <element.h>
33 #include <limits.h>
34 #include <intprops.h>
35
36 #ifdef DEBUG
37 # define warn() fprintf(stderr, "%s: %d\n", __func__, __LINE__)
38 #else
39 # define warn()
40 #endif
41
42 #define IS_ERR(len, flags) (len < -1 || ((flags & ASN1_DECODE_FLAG_STRICT_DER) && len < 0))
43
44 #define HAVE_TWO(x) (x>=2?1:0)
45
46 #define DECODE_FLAG_HAVE_TAG 1
47 #define DECODE_FLAG_INDEFINITE (1<<1)
48
49 #define DECR_LEN(l, s) do { \
50           l -= s; \
51           if (l < 0) { \
52             warn(); \
53             result = ASN1_DER_ERROR; \
54             goto cleanup; \
55           } \
56         } while (0)
57
58 static int
59 _asn1_get_indefinite_length_string (const unsigned char *der, int der_len, int *len);
60
61 static int
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,
65                         unsigned dflags);
66
67 static int
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);
71
72 static void
73 _asn1_error_description_tag_error (asn1_node node, char *ErrorDescription)
74 {
75
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, "'");
80
81 }
82
83 /**
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.
88  *
89  * Extract a length field from DER data.
90  *
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.
94  **/
95 long
96 asn1_get_length_der (const unsigned char *der, int der_len, int *len)
97 {
98   unsigned int ans;
99   int k, punt, sum;
100
101   *len = 0;
102   if (der_len <= 0)
103     return 0;
104
105   if (!(der[0] & 128))
106     {
107       /* short form */
108       *len = 1;
109       ans = der[0];
110     }
111   else
112     {
113       /* Long form */
114       k = der[0] & 0x7F;
115       punt = 1;
116       if (k)
117         {                       /* definite length method */
118           ans = 0;
119           while (punt <= k && punt < der_len)
120             {
121               if (INT_MULTIPLY_OVERFLOW (ans, 256))
122                 return -2;
123               ans *= 256;
124
125               if (INT_ADD_OVERFLOW (ans, ((unsigned) der[punt])))
126                 return -2;
127               ans += der[punt];
128               punt++;
129             }
130         }
131       else
132         {                       /* indefinite length method */
133           *len = punt;
134           return -1;
135         }
136
137       *len = punt;
138     }
139
140   sum = ans;
141   if (ans >= INT_MAX || INT_ADD_OVERFLOW (sum, (*len)))
142     return -2;
143   sum += *len;
144
145   if (sum > der_len)
146     return -4;
147
148   return ans;
149 }
150
151 /**
152  * asn1_get_tag_der:
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.
158  *
159  * Decode the class and TAG from DER code.
160  *
161  * Returns: Returns %ASN1_SUCCESS on success, or an error.
162  **/
163 int
164 asn1_get_tag_der (const unsigned char *der, int der_len,
165                   unsigned char *cls, int *len, unsigned long *tag)
166 {
167   unsigned int ris;
168   int punt;
169
170   if (der == NULL || der_len < 2 || len == NULL)
171     return ASN1_DER_ERROR;
172
173   *cls = der[0] & 0xE0;
174   if ((der[0] & 0x1F) != 0x1F)
175     {
176       /* short form */
177       *len = 1;
178       ris = der[0] & 0x1F;
179     }
180   else
181     {
182       /* Long form */
183       punt = 1;
184       ris = 0;
185       while (punt < der_len && der[punt] & 128)
186         {
187
188           if (INT_MULTIPLY_OVERFLOW (ris, 128))
189             return ASN1_DER_ERROR;
190           ris *= 128;
191
192           if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
193             return ASN1_DER_ERROR;
194           ris += (der[punt] & 0x7F);
195           punt++;
196         }
197
198       if (punt >= der_len)
199         return ASN1_DER_ERROR;
200
201       if (INT_MULTIPLY_OVERFLOW (ris, 128))
202         return ASN1_DER_ERROR;
203       ris *= 128;
204
205       if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
206         return ASN1_DER_ERROR;
207       ris += (der[punt] & 0x7F);
208       punt++;
209
210       *len = punt;
211     }
212
213   if (tag)
214     *tag = ris;
215   return ASN1_SUCCESS;
216 }
217
218 /**
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.
223  *
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.
227  *
228  * Returns: Return the decoded length value, or negative value when
229  *   the value was too big.
230  *
231  * Since: 2.0
232  **/
233 long
234 asn1_get_length_ber (const unsigned char *ber, int ber_len, int *len)
235 {
236   int ret;
237   long err;
238
239   ret = asn1_get_length_der (ber, ber_len, len);
240   if (ret == -1)
241     {                           /* indefinite length method */
242       err = _asn1_get_indefinite_length_string (ber + 1, ber_len, &ret);
243       if (err != ASN1_SUCCESS)
244         return -3;
245     }
246
247   return ret;
248 }
249
250 /**
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.
258  *
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
261  * content octets.
262  *
263  * Returns: Returns %ASN1_SUCCESS on success, or an error.
264  **/
265 int
266 asn1_get_octet_der (const unsigned char *der, int der_len,
267                     int *ret_len, unsigned char *str, int str_size,
268                     int *str_len)
269 {
270   int len_len = 0;
271
272   if (der_len <= 0)
273     return ASN1_GENERIC_ERROR;
274
275   *str_len = asn1_get_length_der (der, der_len, &len_len);
276
277   if (*str_len < 0)
278     return ASN1_DER_ERROR;
279
280   *ret_len = *str_len + len_len;
281   if (str_size >= *str_len)
282     {
283       if (*str_len > 0 && str != NULL)
284         memcpy (str, der + len_len, *str_len);
285     }
286   else
287     {
288       return ASN1_MEM_ERROR;
289     }
290
291   return ASN1_SUCCESS;
292 }
293
294
295 /*- 
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
304  *
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.
308  *
309  * Returns: %ASN1_SUCCESS on success, or an error.
310  -*/
311 static int
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)
314 {
315   int len_len, str_len;
316   unsigned i;
317   unsigned sign_count = 0;
318   unsigned dot_count = 0;
319   const unsigned char *p;
320
321   if (der_len <= 0 || str == NULL)
322     return ASN1_DER_ERROR;
323
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;
327
328   /* perform some sanity checks on the data */
329   if (str_len < 8)
330     {
331       warn();
332       return ASN1_DER_ERROR;
333     }
334
335   if (flags & ASN1_DECODE_FLAG_STRICT_DER)
336     {
337       p = &der[len_len];
338       for (i=0;i<(unsigned)(str_len-1);i++)
339          {
340            if (isdigit(p[i]) == 0)
341              {
342                if (type == ASN1_ETYPE_GENERALIZED_TIME)
343                  {
344                    /* tolerate lax encodings */
345                    if (p[i] == '.' && dot_count == 0)
346                      {
347                        dot_count++;
348                        continue;
349                      }
350
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)
355                      {
356                        sign_count++;
357                        continue;
358                      }
359                  }
360
361                warn();
362                return ASN1_DER_ERROR;
363              }
364          }
365
366       if (sign_count == 0 && p[str_len-1] != 'Z')
367         {
368           warn();
369           return ASN1_DER_ERROR;
370         }
371     }
372   memcpy (str, der + len_len, str_len);
373   str[str_len] = 0;
374   *ret_len = str_len + len_len;
375
376   return ASN1_SUCCESS;
377 }
378
379 /**
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.
386  *
387  * Converts a DER encoded object identifier to its textual form. This
388  * function expects the DER object identifier without the tag.
389  *
390  * Returns: %ASN1_SUCCESS on success, or an error.
391  **/
392 int
393 asn1_get_object_id_der (const unsigned char *der, int der_len, int *ret_len,
394                         char *str, int str_size)
395 {
396   int len_len, len, k;
397   int leading;
398   char temp[LTOSTR_MAX_SIZE];
399   uint64_t val, val1;
400
401   *ret_len = 0;
402   if (str && str_size > 0)
403     str[0] = 0;                 /* no oid */
404
405   if (str == NULL || der_len <= 0)
406     return ASN1_GENERIC_ERROR;
407
408   len = asn1_get_length_der (der, der_len, &len_len);
409
410   if (len <= 0 || len + len_len > der_len)
411     return ASN1_DER_ERROR;
412
413   val1 = der[len_len] / 40;
414   val = der[len_len] - val1 * 40;
415
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));
419
420   val = 0;
421   leading = 1;
422   for (k = 1; k < len; k++)
423     {
424       /* X.690 mandates that the leading byte must never be 0x80
425        */
426       if (leading != 0 && der[len_len + k] == 0x80)
427         return ASN1_DER_ERROR;
428       leading = 0;
429
430       /* check for wrap around */
431       if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
432         return ASN1_DER_ERROR;
433
434       val = val << 7;
435       val |= der[len_len + k] & 0x7F;
436
437       if (!(der[len_len + k] & 0x80))
438         {
439           _asn1_str_cat (str, str_size, ".");
440           _asn1_str_cat (str, str_size, _asn1_ltostr (val, temp));
441           val = 0;
442           leading = 1;
443         }
444     }
445
446   if (INT_ADD_OVERFLOW (len, len_len))
447     return ASN1_DER_ERROR;
448
449   *ret_len = len + len_len;
450
451   return ASN1_SUCCESS;
452 }
453
454 /**
455  * asn1_get_bit_der:
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.
462  *
463  * Extract a BIT SEQUENCE from DER data.
464  *
465  * Returns: %ASN1_SUCCESS on success, or an error.
466  **/
467 int
468 asn1_get_bit_der (const unsigned char *der, int der_len,
469                   int *ret_len, unsigned char *str, int str_size,
470                   int *bit_len)
471 {
472   int len_len = 0, len_byte;
473
474   if (der_len <= 0)
475     return ASN1_GENERIC_ERROR;
476
477   len_byte = asn1_get_length_der (der, der_len, &len_len) - 1;
478   if (len_byte < 0)
479     return ASN1_DER_ERROR;
480
481   *ret_len = len_byte + len_len + 1;
482   *bit_len = len_byte * 8 - der[len_len];
483
484   if (*bit_len < 0)
485     return ASN1_DER_ERROR;
486
487   if (str_size >= len_byte)
488     {
489       if (len_byte > 0 && str)
490         memcpy (str, der + len_len + 1, len_byte);
491     }
492   else
493     {
494       return ASN1_MEM_ERROR;
495     }
496
497   return ASN1_SUCCESS;
498 }
499
500 /* tag_len: the total tag length (explicit+inner)
501  * inner_tag_len: the inner_tag length
502  */
503 static int
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)
506 {
507   asn1_node p;
508   int counter, len2, len3, is_tag_implicit;
509   int result;
510   unsigned long tag, tag_implicit = 0;
511   unsigned char class, class2, class_implicit = 0;
512
513   if (der_len <= 0)
514     return ASN1_GENERIC_ERROR;
515
516   counter = is_tag_implicit = 0;
517
518   if (node->type & CONST_TAG)
519     {
520       p = node->down;
521       while (p)
522         {
523           if (type_field (p->type) == ASN1_ETYPE_TAG)
524             {
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;
531               else
532                 class2 = ASN1_CLASS_CONTEXT_SPECIFIC;
533
534               if (p->type & CONST_EXPLICIT)
535                 {
536                   if (asn1_get_tag_der
537                       (der + counter, der_len, &class, &len2,
538                        &tag) != ASN1_SUCCESS)
539                     return ASN1_DER_ERROR;
540
541                   DECR_LEN(der_len, len2);
542                   counter += len2;
543
544                   if (flags & ASN1_DECODE_FLAG_STRICT_DER)
545                     len3 =
546                       asn1_get_length_der (der + counter, der_len,
547                                          &len2);
548                   else
549                     len3 =
550                       asn1_get_length_ber (der + counter, der_len,
551                                          &len2);
552                   if (len3 < 0)
553                     return ASN1_DER_ERROR;
554
555                   DECR_LEN(der_len, len2);
556                   counter += len2;
557
558                   if (!is_tag_implicit)
559                     {
560                       if ((class != (class2 | ASN1_CLASS_STRUCTURED)) ||
561                           (tag != strtoul ((char *) p->value, NULL, 10)))
562                         return ASN1_TAG_ERROR;
563                     }
564                   else
565                     {           /* ASN1_TAG_IMPLICIT */
566                       if ((class != class_implicit) || (tag != tag_implicit))
567                         return ASN1_TAG_ERROR;
568                     }
569                   is_tag_implicit = 0;
570                 }
571               else
572                 {               /* ASN1_TAG_IMPLICIT */
573                   if (!is_tag_implicit)
574                     {
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);
582                       is_tag_implicit = 1;
583                     }
584                 }
585             }
586           p = p->right;
587         }
588     }
589
590   if (is_tag_implicit)
591     {
592       if (asn1_get_tag_der
593           (der + counter, der_len, &class, &len2,
594            &tag) != ASN1_SUCCESS)
595         return ASN1_DER_ERROR;
596
597       DECR_LEN(der_len, len2);
598
599       if ((class != class_implicit) || (tag != tag_implicit))
600         {
601           if (type_field (node->type) == ASN1_ETYPE_OCTET_STRING)
602             {
603               class_implicit |= ASN1_CLASS_STRUCTURED;
604               if ((class != class_implicit) || (tag != tag_implicit))
605                 return ASN1_TAG_ERROR;
606             }
607           else
608             return ASN1_TAG_ERROR;
609         }
610     }
611   else
612     {
613       unsigned type = type_field (node->type);
614       if (type == ASN1_ETYPE_TAG)
615         {
616           *tag_len = 0;
617           if (inner_tag_len)
618             *inner_tag_len = 0;
619           return ASN1_SUCCESS;
620         }
621
622       if (asn1_get_tag_der
623           (der + counter, der_len, &class, &len2,
624            &tag) != ASN1_SUCCESS)
625         return ASN1_DER_ERROR;
626
627       DECR_LEN(der_len, len2);
628
629       switch (type)
630         {
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:
648         case ASN1_ETYPE_SET:
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;
655           break;
656
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;
664           break;
665         case ASN1_ETYPE_ANY:
666           counter -= len2;
667           break;
668         case ASN1_ETYPE_CHOICE:
669           counter -= len2;
670           break;
671         default:
672           return ASN1_DER_ERROR;
673           break;
674         }
675     }
676
677   counter += len2;
678   *tag_len = counter;
679   if (inner_tag_len)
680     *inner_tag_len = len2;
681   return ASN1_SUCCESS;
682
683 cleanup:
684   return result;
685 }
686
687 static int
688 extract_tag_der_recursive(asn1_node node, const unsigned char *der, int der_len,
689                        int *ret_len, int *inner_len, unsigned flags)
690 {
691 asn1_node p;
692 int ris = ASN1_DER_ERROR;
693
694   if (type_field (node->type) == ASN1_ETYPE_CHOICE)
695     {
696       p = node->down;
697       while (p)
698         {
699           ris = _asn1_extract_tag_der (p, der, der_len, ret_len, inner_len, flags);
700           if (ris == ASN1_SUCCESS)
701             break;
702           p = p->right;
703         }
704
705       *ret_len = 0;
706       return ris;
707     }
708   else
709     return _asn1_extract_tag_der (node, der, der_len, ret_len, inner_len, flags);
710 }
711
712 static int
713 _asn1_delete_not_used (asn1_node node)
714 {
715   asn1_node p, p2;
716
717   if (node == NULL)
718     return ASN1_ELEMENT_NOT_FOUND;
719
720   p = node;
721   while (p)
722     {
723       if (p->type & CONST_NOT_USED)
724         {
725           p2 = NULL;
726           if (p != node)
727             {
728               p2 = _asn1_find_left (p);
729               if (!p2)
730                 p2 = _asn1_find_up (p);
731             }
732           asn1_delete_structure (&p);
733           p = p2;
734         }
735
736       if (!p)
737         break;                  /* reach node */
738
739       if (p->down)
740         {
741           p = p->down;
742         }
743       else
744         {
745           if (p == node)
746             p = NULL;
747           else if (p->right)
748             p = p->right;
749           else
750             {
751               while (1)
752                 {
753                   p = _asn1_find_up (p);
754                   if (p == node)
755                     {
756                       p = NULL;
757                       break;
758                     }
759                   if (p->right)
760                     {
761                       p = p->right;
762                       break;
763                     }
764                 }
765             }
766         }
767     }
768   return ASN1_SUCCESS;
769 }
770
771 static int
772 _asn1_get_indefinite_length_string (const unsigned char *der,
773                                     int der_len, int *len)
774 {
775   int len2, len3, counter, indefinite;
776   int result;
777   unsigned long tag;
778   unsigned char class;
779
780   counter = indefinite = 0;
781
782   while (1)
783     {
784       if (HAVE_TWO(der_len) && (der[counter] == 0) && (der[counter + 1] == 0))
785         {
786           counter += 2;
787           DECR_LEN(der_len, 2);
788
789           indefinite--;
790           if (indefinite <= 0)
791             break;
792           else
793             continue;
794         }
795
796       if (asn1_get_tag_der
797           (der + counter, der_len, &class, &len2,
798            &tag) != ASN1_SUCCESS)
799         return ASN1_DER_ERROR;
800
801       DECR_LEN(der_len, len2);
802       counter += len2;
803
804       len2 = asn1_get_length_der (der + counter, der_len, &len3);
805       if (len2 < -1)
806         return ASN1_DER_ERROR;
807
808       if (len2 == -1)
809         {
810           indefinite++;
811           counter += 1;
812           DECR_LEN(der_len, 1);
813         }
814       else
815         {
816           counter += len2 + len3;
817           DECR_LEN(der_len, len2+len3);
818         }
819     }
820
821   *len = counter;
822   return ASN1_SUCCESS;
823
824 cleanup:
825   return result;
826 }
827
828 static void delete_unneeded_choice_fields(asn1_node p)
829 {
830   asn1_node p2;
831
832   while (p->right)
833     {
834       p2 = p->right;
835       asn1_delete_structure (&p2);
836     }
837 }
838
839
840 /**
841  * asn1_der_decoding2
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
849  *   error occurred.
850  *
851  * Fill the structure *@element with values of a DER encoding string. The
852  * structure must just be created with function asn1_create_element().
853  *
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.
857  *
858  * If %ASN1_DECODE_FLAG_STRICT_DER flag is set then the function will
859  * not decode any BER-encoded elements.
860  *
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).
865  **/
866 int
867 asn1_der_decoding2 (asn1_node *element, const void *ider, int *max_ider_len,
868                     unsigned int flags, char *errorDescription)
869 {
870   asn1_node node, p, p2, p3;
871   char temp[128];
872   int counter, len2, len3, len4, move, ris, tlen;
873   struct node_tail_cache_st tcache = {NULL, NULL};
874   unsigned char class;
875   unsigned long tag;
876   int tag_len;
877   int indefinite, result, total_len = *max_ider_len, ider_len = *max_ider_len;
878   int inner_tag_len;
879   unsigned char *ptmp;
880   const unsigned char *ptag;
881   const unsigned char *der = ider;
882
883   node = *element;
884
885   if (errorDescription != NULL)
886     errorDescription[0] = 0;
887
888   if (node == NULL)
889     return ASN1_ELEMENT_NOT_FOUND;
890
891   if (node->type & CONST_OPTION)
892     {
893       result = ASN1_GENERIC_ERROR;
894       warn();
895       goto cleanup;
896     }
897
898   counter = 0;
899   move = DOWN;
900   p = node;
901   while (1)
902     {
903       tag_len = 0;
904       inner_tag_len = 0;
905       ris = ASN1_SUCCESS;
906       if (move != UP)
907         {
908           if (p->type & CONST_SET)
909             {
910               p2 = _asn1_find_up (p);
911               len2 = p2->tmp_ival;
912               if (len2 == -1)
913                 {
914                   if (HAVE_TWO(ider_len) && !der[counter] && !der[counter + 1])
915                     {
916                       p = p2;
917                       move = UP;
918                       counter += 2;
919                       DECR_LEN(ider_len, 2);
920                       continue;
921                     }
922                 }
923               else if (counter == len2)
924                 {
925                   p = p2;
926                   move = UP;
927                   continue;
928                 }
929               else if (counter > len2)
930                 {
931                   result = ASN1_DER_ERROR;
932                   warn();
933                   goto cleanup;
934                 }
935               p2 = p2->down;
936               while (p2)
937                 {
938                   if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
939                     {
940                       ris =
941                           extract_tag_der_recursive (p2, der + counter,
942                                                      ider_len, &len2, NULL, flags);
943                       if (ris == ASN1_SUCCESS)
944                         {
945                           p2->type &= ~CONST_NOT_USED;
946                           p = p2;
947                           break;
948                         }
949                     }
950                   p2 = p2->right;
951                 }
952               if (p2 == NULL)
953                 {
954                   result = ASN1_DER_ERROR;
955                   warn();
956                   goto cleanup;
957                 }
958             }
959
960           /* the position in the DER structure this starts */
961           p->start = counter;
962           p->end = total_len - 1;
963
964           if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
965             {
966               p2 = _asn1_find_up (p);
967               len2 = p2->tmp_ival;
968               if (counter == len2)
969                 {
970                   if (p->right)
971                     {
972                       p2 = p->right;
973                       move = RIGHT;
974                     }
975                   else
976                     move = UP;
977
978                   if (p->type & CONST_OPTION)
979                     asn1_delete_structure (&p);
980
981                   p = p2;
982                   continue;
983                 }
984             }
985
986           if (type_field (p->type) == ASN1_ETYPE_CHOICE)
987             {
988               while (p->down)
989                 {
990                   ris =
991                       extract_tag_der_recursive (p->down, der + counter,
992                                                  ider_len, &len2, NULL, flags);
993
994                   if (ris == ASN1_SUCCESS)
995                     {
996                       delete_unneeded_choice_fields(p->down);
997                       break;
998                     }
999                   else if (ris == ASN1_ERROR_TYPE_ANY)
1000                     {
1001                       result = ASN1_ERROR_TYPE_ANY;
1002                       warn();
1003                       goto cleanup;
1004                     }
1005                   else
1006                     {
1007                       p2 = p->down;
1008                       asn1_delete_structure (&p2);
1009                     }
1010                 }
1011
1012               if (p->down == NULL)
1013                 {
1014                   if (!(p->type & CONST_OPTION))
1015                     {
1016                       result = ASN1_DER_ERROR;
1017                       warn();
1018                       goto cleanup;
1019                     }
1020                 }
1021               else if (type_field (p->type) != ASN1_ETYPE_CHOICE)
1022                 p = p->down;
1023
1024               p->start = counter;
1025             }
1026
1027           if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1028             {
1029               p2 = _asn1_find_up (p);
1030               len2 = p2->tmp_ival;
1031
1032               if ((len2 != -1) && (counter > len2))
1033                 ris = ASN1_TAG_ERROR;
1034             }
1035
1036           if (ris == ASN1_SUCCESS)
1037             ris =
1038               extract_tag_der_recursive (p, der + counter, ider_len, 
1039                                          &tag_len, &inner_tag_len, flags);
1040
1041           if (ris != ASN1_SUCCESS)
1042             {
1043               if (p->type & CONST_OPTION)
1044                 {
1045                   p->type |= CONST_NOT_USED;
1046                   move = RIGHT;
1047                 }
1048               else if (p->type & CONST_DEFAULT)
1049                 {
1050                   _asn1_set_value (p, NULL, 0);
1051                   move = RIGHT;
1052                 }
1053               else
1054                 {
1055                   if (errorDescription != NULL)
1056                     _asn1_error_description_tag_error (p, errorDescription);
1057
1058                   result = ASN1_TAG_ERROR;
1059                   warn();
1060                   goto cleanup;
1061                 }
1062             }
1063           else
1064             {
1065               DECR_LEN(ider_len, tag_len);
1066               counter += tag_len;
1067             }
1068         }
1069
1070       if (ris == ASN1_SUCCESS)
1071         {
1072           switch (type_field (p->type))
1073             {
1074             case ASN1_ETYPE_NULL:
1075               DECR_LEN(ider_len, 1);
1076               if (der[counter])
1077                 {
1078                   result = ASN1_DER_ERROR;
1079                   warn();
1080                   goto cleanup;
1081                 }
1082               counter++;
1083               move = RIGHT;
1084               break;
1085             case ASN1_ETYPE_BOOLEAN:
1086               DECR_LEN(ider_len, 2);
1087
1088               if (der[counter++] != 1)
1089                 {
1090                   result = ASN1_DER_ERROR;
1091                   warn();
1092                   goto cleanup;
1093                 }
1094               if (der[counter++] == 0)
1095                 _asn1_set_value (p, "F", 1);
1096               else
1097                 _asn1_set_value (p, "T", 1);
1098               move = RIGHT;
1099               break;
1100             case ASN1_ETYPE_INTEGER:
1101             case ASN1_ETYPE_ENUMERATED:
1102               len2 =
1103                 asn1_get_length_der (der + counter, ider_len, &len3);
1104               if (len2 < 0)
1105                 {
1106                   result = ASN1_DER_ERROR;
1107                   warn();
1108                   goto cleanup;
1109                 }
1110
1111               DECR_LEN(ider_len, len3+len2);
1112
1113               _asn1_set_value (p, der + counter, len3 + len2);
1114               counter += len3 + len2;
1115               move = RIGHT;
1116               break;
1117             case ASN1_ETYPE_OBJECT_ID:
1118               result =
1119                 asn1_get_object_id_der (der + counter, ider_len, &len2,
1120                                         temp, sizeof (temp));
1121               if (result != ASN1_SUCCESS)
1122                 {
1123                   warn();
1124                   goto cleanup;
1125                 }
1126
1127               DECR_LEN(ider_len, len2);
1128
1129               tlen = strlen (temp);
1130               if (tlen > 0)
1131                 _asn1_set_value (p, temp, tlen + 1);
1132
1133               counter += len2;
1134               move = RIGHT;
1135               break;
1136             case ASN1_ETYPE_GENERALIZED_TIME:
1137             case ASN1_ETYPE_UTC_TIME:
1138               result =
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)
1142                 {
1143                   warn();
1144                   goto cleanup;
1145                 }
1146
1147               DECR_LEN(ider_len, len2);
1148
1149               tlen = strlen (temp);
1150               if (tlen > 0)
1151                 _asn1_set_value (p, temp, tlen);
1152
1153               counter += len2;
1154               move = RIGHT;
1155               break;
1156             case ASN1_ETYPE_OCTET_STRING:
1157               if (counter < inner_tag_len)
1158                 {
1159                   result = ASN1_DER_ERROR;
1160                   warn();
1161                   goto cleanup;
1162                 }
1163
1164               ptag = der + counter - inner_tag_len;
1165               if (flags & ASN1_DECODE_FLAG_STRICT_DER || !(ptag[0] & ASN1_CLASS_STRUCTURED))
1166                 {
1167                   len2 =
1168                     asn1_get_length_der (der + counter, ider_len, &len3);
1169                   if (len2 < 0)
1170                     {
1171                       result = ASN1_DER_ERROR;
1172                       warn();
1173                       goto cleanup;
1174                     }
1175
1176                   DECR_LEN(ider_len, len3+len2);
1177
1178                   _asn1_set_value (p, der + counter, len3 + len2);
1179                   counter += len3 + len2;
1180                 }
1181               else
1182                 {
1183                   unsigned dflags = 0, vlen, ber_len;
1184
1185                   if (ptag[0] & ASN1_CLASS_STRUCTURED)
1186                     dflags |= DECODE_FLAG_INDEFINITE;
1187
1188                   result = _asn1_decode_simple_ber(type_field (p->type), der+counter, ider_len, &ptmp, &vlen, &ber_len, dflags);
1189                   if (result != ASN1_SUCCESS)
1190                     {
1191                       warn();
1192                       goto cleanup;
1193                     }
1194
1195                   DECR_LEN(ider_len, ber_len);
1196
1197                   _asn1_set_value_lv (p, ptmp, vlen);
1198
1199                   counter += ber_len;
1200                   free(ptmp);
1201                 }
1202               move = RIGHT;
1203               break;
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:
1214               len2 =
1215                 asn1_get_length_der (der + counter, ider_len, &len3);
1216               if (len2 < 0)
1217                 {
1218                   result = ASN1_DER_ERROR;
1219                   warn();
1220                   goto cleanup;
1221                 }
1222
1223               DECR_LEN(ider_len, len3+len2);
1224
1225               _asn1_set_value (p, der + counter, len3 + len2);
1226               counter += len3 + len2;
1227               move = RIGHT;
1228               break;
1229             case ASN1_ETYPE_SEQUENCE:
1230             case ASN1_ETYPE_SET:
1231               if (move == UP)
1232                 {
1233                   len2 = p->tmp_ival;
1234                   p->tmp_ival = 0;
1235                   if (len2 == -1)
1236                     {           /* indefinite length method */
1237                       DECR_LEN(ider_len, 2);
1238                       if ((der[counter]) || der[counter + 1])
1239                         {
1240                           result = ASN1_DER_ERROR;
1241                           warn();
1242                           goto cleanup;
1243                         }
1244                       counter += 2;
1245                     }
1246                   else
1247                     {           /* definite length method */
1248                       if (len2 != counter)
1249                         {
1250                           result = ASN1_DER_ERROR;
1251                           warn();
1252                           goto cleanup;
1253                         }
1254                     }
1255                   move = RIGHT;
1256                 }
1257               else
1258                 {               /* move==DOWN || move==RIGHT */
1259                   len3 =
1260                     asn1_get_length_der (der + counter, ider_len, &len2);
1261                   if (IS_ERR(len3, flags))
1262                     {
1263                       result = ASN1_DER_ERROR;
1264                       warn();
1265                       goto cleanup;
1266                     }
1267
1268                   DECR_LEN(ider_len, len2);
1269                   counter += len2;
1270
1271                   if (len3 > 0)
1272                     {
1273                       p->tmp_ival = counter + len3;
1274                       move = DOWN;
1275                     }
1276                   else if (len3 == 0)
1277                     {
1278                       p2 = p->down;
1279                       while (p2)
1280                         {
1281                           if (type_field (p2->type) != ASN1_ETYPE_TAG)
1282                             {
1283                               p3 = p2->right;
1284                               asn1_delete_structure (&p2);
1285                               p2 = p3;
1286                             }
1287                           else
1288                             p2 = p2->right;
1289                         }
1290                       move = RIGHT;
1291                     }
1292                   else
1293                     {           /* indefinite length method */
1294                       p->tmp_ival = -1;
1295                       move = DOWN;
1296                     }
1297                 }
1298               break;
1299             case ASN1_ETYPE_SEQUENCE_OF:
1300             case ASN1_ETYPE_SET_OF:
1301               if (move == UP)
1302                 {
1303                   len2 = p->tmp_ival;
1304                   if (len2 == -1)
1305                     {           /* indefinite length method */
1306                       if (!HAVE_TWO(ider_len) || ((der[counter]) || der[counter + 1]))
1307                         {
1308                           _asn1_append_sequence_set (p, &tcache);
1309                           p = tcache.tail;
1310                           move = RIGHT;
1311                           continue;
1312                         }
1313
1314                       p->tmp_ival = 0;
1315                       tcache.tail = NULL; /* finished decoding this structure */
1316                       tcache.head = NULL;
1317                       DECR_LEN(ider_len, 2);
1318                       counter += 2;
1319                     }
1320                   else
1321                     {           /* definite length method */
1322                       if (len2 > counter)
1323                         {
1324                           _asn1_append_sequence_set (p, &tcache);
1325                           p = tcache.tail;
1326                           move = RIGHT;
1327                           continue;
1328                         }
1329
1330                       p->tmp_ival = 0;
1331                       tcache.tail = NULL; /* finished decoding this structure */
1332                       tcache.head = NULL;
1333
1334                       if (len2 != counter)
1335                         {
1336                           result = ASN1_DER_ERROR;
1337                           warn();
1338                           goto cleanup;
1339                         }
1340                     }
1341                 }
1342               else
1343                 {               /* move==DOWN || move==RIGHT */
1344                   len3 =
1345                     asn1_get_length_der (der + counter, ider_len, &len2);
1346                   if (IS_ERR(len3, flags))
1347                     {
1348                       result = ASN1_DER_ERROR;
1349                       warn();
1350                       goto cleanup;
1351                     }
1352
1353                   DECR_LEN(ider_len, len2);
1354                   counter += len2;
1355                   if (len3)
1356                     {
1357                       if (len3 > 0)
1358                         {       /* definite length method */
1359                           p->tmp_ival = counter + len3;
1360                         }
1361                       else
1362                         {       /* indefinite length method */
1363                           p->tmp_ival = -1;
1364                         }
1365
1366                       p2 = p->down;
1367                       if (p2 == NULL)
1368                         {
1369                           result = ASN1_DER_ERROR;
1370                           warn();
1371                           goto cleanup;
1372                         }
1373
1374                       while ((type_field (p2->type) == ASN1_ETYPE_TAG)
1375                              || (type_field (p2->type) == ASN1_ETYPE_SIZE))
1376                         p2 = p2->right;
1377                       if (p2->right == NULL)
1378                         _asn1_append_sequence_set (p, &tcache);
1379                       p = p2;
1380                     }
1381                 }
1382               move = RIGHT;
1383               break;
1384             case ASN1_ETYPE_ANY:
1385               /* Check indefinite lenth method in an EXPLICIT TAG */
1386               
1387               if (!(flags & ASN1_DECODE_FLAG_STRICT_DER) && (p->type & CONST_TAG) && 
1388                   tag_len == 2 && (der[counter - 1] == 0x80))
1389                 indefinite = 1;
1390               else
1391                 indefinite = 0;
1392
1393               if (asn1_get_tag_der
1394                   (der + counter, ider_len, &class, &len2,
1395                    &tag) != ASN1_SUCCESS)
1396                 {
1397                   result = ASN1_DER_ERROR;
1398                   warn();
1399                   goto cleanup;
1400                 }
1401
1402               DECR_LEN(ider_len, len2);
1403
1404               len4 =
1405                 asn1_get_length_der (der + counter + len2,
1406                                      ider_len, &len3);
1407               if (IS_ERR(len4, flags))
1408                 {
1409                   result = ASN1_DER_ERROR;
1410                   warn();
1411                   goto cleanup;
1412                 }
1413               if (len4 != -1) /* definite */
1414                 {
1415                   len2 += len4;
1416
1417                   DECR_LEN(ider_len, len4+len3);
1418                   _asn1_set_value_lv (p, der + counter, len2 + len3);
1419                   counter += len2 + len3;
1420                 }
1421               else /* == -1 */
1422                 {               /* indefinite length */
1423                   ider_len += len2; /* undo DECR_LEN */
1424
1425                   if (counter == 0)
1426                     {
1427                       result = ASN1_DER_ERROR;
1428                       warn();
1429                       goto cleanup;
1430                     }
1431
1432                   result =
1433                     _asn1_get_indefinite_length_string (der + counter, ider_len, &len2);
1434                   if (result != ASN1_SUCCESS)
1435                     {
1436                       warn();
1437                       goto cleanup;
1438                     }
1439
1440                   DECR_LEN(ider_len, len2);
1441                   _asn1_set_value_lv (p, der + counter, len2);
1442                   counter += len2;
1443
1444                 }
1445
1446                 /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1447                    an indefinite length method. */
1448                 if (indefinite)
1449                   {
1450                     DECR_LEN(ider_len, 2);
1451                     if (!der[counter] && !der[counter + 1])
1452                       {
1453                         counter += 2;
1454                       }
1455                     else
1456                       {
1457                         result = ASN1_DER_ERROR;
1458                         warn();
1459                         goto cleanup;
1460                       }
1461                   }
1462
1463               move = RIGHT;
1464               break;
1465             default:
1466               move = (move == UP) ? RIGHT : DOWN;
1467               break;
1468             }
1469         }
1470
1471       if (p)
1472         {
1473           p->end = counter - 1;
1474         }
1475
1476       if (p == node && move != DOWN)
1477         break;
1478
1479       if (move == DOWN)
1480         {
1481           if (p->down)
1482             p = p->down;
1483           else
1484             move = RIGHT;
1485         }
1486       if ((move == RIGHT) && !(p->type & CONST_SET))
1487         {
1488           if (p->right)
1489             p = p->right;
1490           else
1491             move = UP;
1492         }
1493       if (move == UP)
1494         p = _asn1_find_up (p);
1495     }
1496
1497   _asn1_delete_not_used (*element);
1498
1499   if ((ider_len < 0) ||
1500       (!(flags & ASN1_DECODE_FLAG_ALLOW_PADDING) && (ider_len != 0)))
1501     {
1502       warn();
1503       result = ASN1_DER_ERROR;
1504       goto cleanup;
1505     }
1506
1507   *max_ider_len = total_len - ider_len;
1508
1509   return ASN1_SUCCESS;
1510
1511 cleanup:
1512   asn1_delete_structure (element);
1513   return result;
1514 }
1515
1516
1517 /**
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
1523  *   error occurred.
1524  *
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(). 
1528  *
1529  * Note that the *@element variable is provided as a pointer for
1530  * historical reasons.
1531  *
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).
1536  **/
1537 int
1538 asn1_der_decoding (asn1_node * element, const void *ider, int ider_len,
1539                    char *errorDescription)
1540 {
1541   return asn1_der_decoding2 (element, ider, &ider_len, 0, errorDescription);
1542 }
1543
1544 /**
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
1551  *   error occurred.
1552  *
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
1558  * %NULL.
1559  *
1560  * This function is deprecated and may just be an alias to asn1_der_decoding
1561  * in future versions. Use asn1_der_decoding() instead.
1562  *
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).
1567  **/
1568 int
1569 asn1_der_decoding_element (asn1_node * structure, const char *elementName,
1570                            const void *ider, int len, char *errorDescription)
1571 {
1572   return asn1_der_decoding(structure, ider, len, errorDescription);
1573 }
1574
1575 /**
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
1582  *   (@ider[*start])
1583  * @end: the position of the last byte of NAME_ELEMENT decoding
1584  *  (@ider[*end])
1585  *
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.
1591  *
1592  * One example is the sequence "tbsCertificate" inside an X509
1593  * certificate.
1594  *
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().
1597  *
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.
1602  **/
1603 int
1604 asn1_der_decoding_startEnd (asn1_node element, const void *ider, int ider_len,
1605                             const char *name_element, int *start, int *end)
1606 {
1607   asn1_node node, node_to_find;
1608   int result = ASN1_DER_ERROR;
1609
1610   node = element;
1611
1612   if (node == NULL)
1613     return ASN1_ELEMENT_NOT_FOUND;
1614
1615   node_to_find = asn1_find_node (node, name_element);
1616
1617   if (node_to_find == NULL)
1618     return ASN1_ELEMENT_NOT_FOUND;
1619
1620   *start = node_to_find->start;
1621   *end = node_to_find->end;
1622
1623   if (*start == 0 && *end == 0)
1624     {
1625       if (ider == NULL || ider_len == 0)
1626         return ASN1_GENERIC_ERROR;
1627
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)
1631         {
1632           warn();
1633           return result;
1634         }
1635
1636       node_to_find = asn1_find_node (node, name_element);
1637       if (node_to_find == NULL)
1638         return ASN1_ELEMENT_NOT_FOUND;
1639
1640       *start = node_to_find->start;
1641       *end = node_to_find->end;
1642     }
1643
1644   if (*end < *start)
1645     return ASN1_GENERIC_ERROR;
1646
1647   return ASN1_SUCCESS;
1648 }
1649
1650 /**
1651  * asn1_expand_any_defined_by:
1652  * @definitions: ASN1 definitions
1653  * @element: pointer to an ASN1 structure
1654  *
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.
1660  *
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.
1665  **/
1666 int
1667 asn1_expand_any_defined_by (asn1_node definitions, asn1_node * element)
1668 {
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;
1676
1677   if ((definitions == NULL) || (*element == NULL))
1678     return ASN1_ELEMENT_NOT_FOUND;
1679
1680   definitionsName = definitions->name;
1681
1682   p = *element;
1683   while (p)
1684     {
1685
1686       switch (type_field (p->type))
1687         {
1688         case ASN1_ETYPE_ANY:
1689           if ((p->type & CONST_DEFINED_BY) && (p->value))
1690             {
1691               /* search the "DEF_BY" element */
1692               p2 = p->down;
1693               while ((p2) && (type_field (p2->type) != ASN1_ETYPE_CONSTANT))
1694                 p2 = p2->right;
1695
1696               if (!p2)
1697                 {
1698                   retCode = ASN1_ERROR_TYPE_ANY;
1699                   break;
1700                 }
1701
1702               p3 = _asn1_find_up (p);
1703
1704               if (!p3)
1705                 {
1706                   retCode = ASN1_ERROR_TYPE_ANY;
1707                   break;
1708                 }
1709
1710               p3 = p3->down;
1711               while (p3)
1712                 {
1713                   if (!(strcmp (p3->name, p2->name)))
1714                     break;
1715                   p3 = p3->right;
1716                 }
1717
1718               if ((!p3) || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID) ||
1719                   (p3->value == NULL))
1720                 {
1721
1722                   p3 = _asn1_find_up (p);
1723                   p3 = _asn1_find_up (p3);
1724
1725                   if (!p3)
1726                     {
1727                       retCode = ASN1_ERROR_TYPE_ANY;
1728                       break;
1729                     }
1730
1731                   p3 = p3->down;
1732
1733                   while (p3)
1734                     {
1735                       if (!(strcmp (p3->name, p2->name)))
1736                         break;
1737                       p3 = p3->right;
1738                     }
1739
1740                   if ((!p3) || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID)
1741                       || (p3->value == NULL))
1742                     {
1743                       retCode = ASN1_ERROR_TYPE_ANY;
1744                       break;
1745                     }
1746                 }
1747
1748               /* search the OBJECT_ID into definitions */
1749               p2 = definitions->down;
1750               while (p2)
1751                 {
1752                   if ((type_field (p2->type) == ASN1_ETYPE_OBJECT_ID) &&
1753                       (p2->type & CONST_ASSIGN))
1754                     {
1755                       snprintf(name, sizeof(name), "%s.%s", definitionsName, p2->name);
1756
1757                       len = ASN1_MAX_NAME_SIZE;
1758                       result =
1759                         asn1_read_value (definitions, name, value, &len);
1760
1761                       if ((result == ASN1_SUCCESS)
1762                           && (!_asn1_strcmp (p3->value, value)))
1763                         {
1764                           p2 = p2->right;       /* pointer to the structure to
1765                                                    use for expansion */
1766                           while ((p2) && (p2->type & CONST_ASSIGN))
1767                             p2 = p2->right;
1768
1769                           if (p2)
1770                             {
1771                               snprintf(name, sizeof(name), "%s.%s", definitionsName, p2->name);
1772
1773                               result =
1774                                 asn1_create_element (definitions, name, &aux);
1775                               if (result == ASN1_SUCCESS)
1776                                 {
1777                                   _asn1_cpy_name (aux, p);
1778                                   len2 =
1779                                     asn1_get_length_der (p->value,
1780                                                          p->value_len, &len3);
1781                                   if (len2 < 0)
1782                                     return ASN1_DER_ERROR;
1783
1784                                   result =
1785                                     asn1_der_decoding (&aux, p->value + len3,
1786                                                        len2,
1787                                                        errorDescription);
1788                                   if (result == ASN1_SUCCESS)
1789                                     {
1790
1791                                       _asn1_set_right (aux, p->right);
1792                                       _asn1_set_right (p, aux);
1793
1794                                       result = asn1_delete_structure (&p);
1795                                       if (result == ASN1_SUCCESS)
1796                                         {
1797                                           p = aux;
1798                                           aux = NULL;
1799                                           break;
1800                                         }
1801                                       else
1802                                         {       /* error with asn1_delete_structure */
1803                                           asn1_delete_structure (&aux);
1804                                           retCode = result;
1805                                           break;
1806                                         }
1807                                     }
1808                                   else
1809                                     {   /* error with asn1_der_decoding */
1810                                       retCode = result;
1811                                       break;
1812                                     }
1813                                 }
1814                               else
1815                                 {       /* error with asn1_create_element */
1816                                   retCode = result;
1817                                   break;
1818                                 }
1819                             }
1820                           else
1821                             {   /* error with the pointer to the structure to exapand */
1822                               retCode = ASN1_ERROR_TYPE_ANY;
1823                               break;
1824                             }
1825                         }
1826                     }
1827                   p2 = p2->right;
1828                 }               /* end while */
1829
1830               if (!p2)
1831                 {
1832                   retCode = ASN1_ERROR_TYPE_ANY;
1833                   break;
1834                 }
1835
1836             }
1837           break;
1838         default:
1839           break;
1840         }
1841
1842
1843       if (p->down)
1844         {
1845           p = p->down;
1846         }
1847       else if (p == *element)
1848         {
1849           p = NULL;
1850           break;
1851         }
1852       else if (p->right)
1853         p = p->right;
1854       else
1855         {
1856           while (1)
1857             {
1858               p = _asn1_find_up (p);
1859               if (p == *element)
1860                 {
1861                   p = NULL;
1862                   break;
1863                 }
1864               if (p->right)
1865                 {
1866                   p = p->right;
1867                   break;
1868                 }
1869             }
1870         }
1871     }
1872
1873   return retCode;
1874 }
1875
1876 /**
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.
1883  *
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.
1888  *
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.
1893  **/
1894 int
1895 asn1_expand_octet_string (asn1_node definitions, asn1_node * element,
1896                           const char *octetName, const char *objectName)
1897 {
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];
1904
1905   if ((definitions == NULL) || (*element == NULL))
1906     return ASN1_ELEMENT_NOT_FOUND;
1907
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;
1915
1916   objectNode = asn1_find_node (*element, objectName);
1917   if (objectNode == NULL)
1918     return ASN1_ELEMENT_NOT_FOUND;
1919
1920   if (type_field (objectNode->type) != ASN1_ETYPE_OBJECT_ID)
1921     return ASN1_ELEMENT_NOT_FOUND;
1922
1923   if (objectNode->value == NULL)
1924     return ASN1_VALUE_NOT_FOUND;
1925
1926
1927   /* search the OBJECT_ID into definitions */
1928   p2 = definitions->down;
1929   while (p2)
1930     {
1931       if ((type_field (p2->type) == ASN1_ETYPE_OBJECT_ID) &&
1932           (p2->type & CONST_ASSIGN))
1933         {
1934           strcpy (name, definitions->name);
1935           strcat (name, ".");
1936           strcat (name, p2->name);
1937
1938           len = sizeof (value);
1939           result = asn1_read_value (definitions, name, value, &len);
1940
1941           if ((result == ASN1_SUCCESS)
1942               && (!_asn1_strcmp (objectNode->value, value)))
1943             {
1944
1945               p2 = p2->right;   /* pointer to the structure to
1946                                    use for expansion */
1947               while ((p2) && (p2->type & CONST_ASSIGN))
1948                 p2 = p2->right;
1949
1950               if (p2)
1951                 {
1952                   strcpy (name, definitions->name);
1953                   strcat (name, ".");
1954                   strcat (name, p2->name);
1955
1956                   result = asn1_create_element (definitions, name, &aux);
1957                   if (result == ASN1_SUCCESS)
1958                     {
1959                       _asn1_cpy_name (aux, octetNode);
1960                       len2 =
1961                         asn1_get_length_der (octetNode->value,
1962                                              octetNode->value_len, &len3);
1963                       if (len2 < 0)
1964                         return ASN1_DER_ERROR;
1965
1966                       result =
1967                         asn1_der_decoding (&aux, octetNode->value + len3,
1968                                            len2, errorDescription);
1969                       if (result == ASN1_SUCCESS)
1970                         {
1971
1972                           _asn1_set_right (aux, octetNode->right);
1973                           _asn1_set_right (octetNode, aux);
1974
1975                           result = asn1_delete_structure (&octetNode);
1976                           if (result == ASN1_SUCCESS)
1977                             {
1978                               aux = NULL;
1979                               break;
1980                             }
1981                           else
1982                             {   /* error with asn1_delete_structure */
1983                               asn1_delete_structure (&aux);
1984                               retCode = result;
1985                               break;
1986                             }
1987                         }
1988                       else
1989                         {       /* error with asn1_der_decoding */
1990                           retCode = result;
1991                           break;
1992                         }
1993                     }
1994                   else
1995                     {           /* error with asn1_create_element */
1996                       retCode = result;
1997                       break;
1998                     }
1999                 }
2000               else
2001                 {               /* error with the pointer to the structure to exapand */
2002                   retCode = ASN1_VALUE_NOT_VALID;
2003                   break;
2004                 }
2005             }
2006         }
2007
2008       p2 = p2->right;
2009
2010     }
2011
2012   if (!p2)
2013     retCode = ASN1_VALUE_NOT_VALID;
2014
2015   return retCode;
2016 }
2017
2018 /*-
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_*
2026  *
2027  * Decodes a simple DER encoded type (e.g. a string, which is not constructed).
2028  * The output is a pointer inside the @der.
2029  *
2030  * Returns: %ASN1_SUCCESS if successful or an error value.
2031  -*/
2032 static int
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)
2036 {
2037   int tag_len, len_len;
2038   const unsigned char *p;
2039   int der_len = _der_len;
2040   unsigned char class;
2041   unsigned long tag;
2042   long ret;
2043
2044   if (der == NULL || der_len == 0)
2045     return ASN1_VALUE_NOT_VALID;
2046
2047   if (ETYPE_OK (etype) == 0 || ETYPE_IS_STRING(etype) == 0)
2048     return ASN1_VALUE_NOT_VALID;
2049
2050   /* doesn't handle constructed classes */
2051   class = ETYPE_CLASS(etype);
2052   if (class != ASN1_CLASS_UNIVERSAL)
2053     return ASN1_VALUE_NOT_VALID;
2054
2055   p = der;
2056
2057   if (dflags & DECODE_FLAG_HAVE_TAG)
2058     {
2059       ret = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag);
2060       if (ret != ASN1_SUCCESS)
2061         return ret;
2062
2063       if (class != ETYPE_CLASS (etype) || tag != ETYPE_TAG (etype))
2064         {
2065           warn();
2066           return ASN1_DER_ERROR;
2067         }
2068
2069       p += tag_len;
2070       der_len -= tag_len;
2071       if (der_len <= 0)
2072         return ASN1_DER_ERROR;
2073     }
2074
2075   ret = asn1_get_length_der (p, der_len, &len_len);
2076   if (ret < 0)
2077     return ASN1_DER_ERROR;
2078
2079   p += len_len;
2080   der_len -= len_len;
2081   if (der_len <= 0)
2082     return ASN1_DER_ERROR;
2083
2084   *str_len = ret;
2085   *str = p;
2086
2087   return ASN1_SUCCESS;
2088 }
2089
2090 /**
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
2097  *
2098  * Decodes a simple DER encoded type (e.g. a string, which is not constructed).
2099  * The output is a pointer inside the @der.
2100  *
2101  * Returns: %ASN1_SUCCESS if successful or an error value.
2102  **/
2103 int
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)
2107 {
2108   return _asn1_decode_simple_der(etype, der, _der_len, str, str_len, DECODE_FLAG_HAVE_TAG);
2109 }
2110
2111 static int append(uint8_t **dst, unsigned *dst_size, const unsigned char *src, unsigned src_size)
2112 {
2113   *dst = _asn1_realloc(*dst, *dst_size+src_size);
2114   if (*dst == NULL)
2115     return ASN1_MEM_ERROR;
2116   memcpy(*dst + *dst_size, src, src_size);
2117   *dst_size += src_size;
2118   return ASN1_SUCCESS;
2119 }
2120
2121 /*-
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
2130  *
2131  * Decodes a BER encoded type. The output is an allocated value 
2132  * of the data. This decodes BER STRINGS only. Other types are
2133  * decoded as DER.
2134  *
2135  * Returns: %ASN1_SUCCESS if successful or an error value.
2136  -*/
2137 static int
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,
2141                         unsigned dflags)
2142 {
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;
2149   unsigned long tag;
2150   unsigned char *out = NULL;
2151   const unsigned char *cout = NULL;
2152   unsigned out_len;
2153   long result;
2154
2155   if (ber_len) *ber_len = 0;
2156
2157   if (der == NULL || der_len == 0)
2158     {
2159       warn();
2160       return ASN1_VALUE_NOT_VALID;
2161     }
2162
2163   if (ETYPE_OK (etype) == 0)
2164     {
2165       warn();
2166       return ASN1_VALUE_NOT_VALID;
2167     }
2168
2169   /* doesn't handle constructed + definite classes */
2170   class = ETYPE_CLASS (etype);
2171   if (class != ASN1_CLASS_UNIVERSAL)
2172     {
2173       warn();
2174       return ASN1_VALUE_NOT_VALID;
2175     }
2176
2177   p = der;
2178
2179   if (dflags & DECODE_FLAG_HAVE_TAG)
2180     {
2181       result = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag);
2182         if (result != ASN1_SUCCESS)
2183           {
2184             warn();
2185             return result;
2186           }
2187
2188         if (tag != ETYPE_TAG (etype))
2189           {
2190             warn();
2191             return ASN1_DER_ERROR;
2192           }
2193
2194         p += tag_len;
2195
2196         DECR_LEN(der_len, tag_len);
2197
2198         if (ber_len) *ber_len += tag_len;
2199     }
2200
2201   /* indefinite constructed */
2202   if (((dflags & DECODE_FLAG_INDEFINITE) || class == ASN1_CLASS_STRUCTURED) && ETYPE_IS_STRING(etype))
2203     {
2204       len_len = 1;
2205
2206       DECR_LEN(der_len, len_len);
2207       if (p[0] != 0x80)
2208         {
2209           warn();
2210           result = ASN1_DER_ERROR;
2211           goto cleanup;
2212         }
2213
2214       p += len_len;
2215
2216       if (ber_len) *ber_len += len_len;
2217
2218       /* decode the available octet strings */
2219       do
2220         {
2221           unsigned tmp_len;
2222
2223           result = asn1_decode_simple_ber(etype, p, der_len, &out, &out_len, &tmp_len);
2224           if (result != ASN1_SUCCESS)
2225             {
2226               warn();
2227               goto cleanup;
2228             }
2229
2230           p += tmp_len;
2231           DECR_LEN(der_len, tmp_len);
2232
2233           if (ber_len) *ber_len += tmp_len;
2234
2235           DECR_LEN(der_len, 2); /* we need the EOC */
2236
2237           if (out_len > 0)
2238             {
2239               result = append(&total, &total_size, out, out_len);
2240               if (result != ASN1_SUCCESS)
2241                 {
2242                   warn();
2243                   goto cleanup;
2244                 }
2245             }
2246
2247           free(out);
2248           out = NULL;
2249
2250           if (p[0] == 0 && p[1] == 0) /* EOC */
2251             {
2252               if (ber_len) *ber_len += 2;
2253               break;
2254             }
2255
2256           /* no EOC */
2257           der_len += 2;
2258
2259           if (der_len == 2)
2260             {
2261               warn();
2262               result = ASN1_DER_ERROR;
2263               goto cleanup;
2264             }
2265         }
2266       while(1);
2267     }
2268   else if (class == ETYPE_CLASS(etype))
2269     {
2270       if (ber_len)
2271         {
2272           result = asn1_get_length_der (p, der_len, &len_len);
2273           if (result < 0)
2274             {
2275               warn();
2276               result = ASN1_DER_ERROR;
2277               goto cleanup;
2278             }
2279           *ber_len += result + len_len;
2280         }
2281
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)
2285         {
2286           warn();
2287           goto cleanup;
2288         }
2289
2290       result = append(&total, &total_size, cout, out_len);
2291       if (result != ASN1_SUCCESS)
2292         {
2293           warn();
2294           goto cleanup;
2295         }
2296     }
2297   else
2298     {
2299       warn();
2300       result = ASN1_DER_ERROR;
2301       goto cleanup;
2302     }
2303
2304   *str = total;
2305   *str_len = total_size;
2306
2307   return ASN1_SUCCESS;
2308 cleanup:
2309   free(out);
2310   free(total);
2311   return result;
2312 }
2313
2314 /**
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)
2322  *
2323  * Decodes a BER encoded type. The output is an allocated value 
2324  * of the data. This decodes BER STRINGS only. Other types are
2325  * decoded as DER.
2326  *
2327  * Returns: %ASN1_SUCCESS if successful or an error value.
2328  **/
2329 int
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)
2333 {
2334   return _asn1_decode_simple_ber(etype, der, _der_len, str, str_len, ber_len, DECODE_FLAG_HAVE_TAG);
2335 }