Coverage Report

Created: 2025-12-31 06:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtasn1/lib/decoding.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2002-2025 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, see
18
 * <https://www.gnu.org/licenses/>.
19
 */
20
21
#include <config.h>
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
#include "c-ctype.h"
36
37
#ifdef DEBUG
38
# define warn() fprintf(stderr, "%s: %d\n", __func__, __LINE__)
39
#else
40
# define warn()
41
#endif
42
43
7.63M
#define IS_ERR(len, flags) (len < -1 || ((flags & ASN1_DECODE_FLAG_STRICT_DER) && len < 0))
44
45
11.4M
#define HAVE_TWO(x) (x>=2?1:0)
46
47
/* Decoding flags (dflags) used in several decoding functions.
48
 *  DECODE_FLAG_HAVE_TAG: The provided buffer includes a tag
49
 *  DECODE_FLAG_CONSTRUCTED: The provided buffer is of indefinite encoding (useful
50
 *                           when no tags are present).
51
 *  DECODE_FLAG_LEVEL1: Internal flag to indicate a level of recursion for BER strings.
52
 *  DECODE_FLAG_LEVEL2: Internal flag to indicate two levels of recursion for BER strings.
53
 *  DECODE_FLAG_LEVEL3: Internal flag to indicate three levels of recursion for BER strings.
54
 *                      This is the maximum levels of recursion possible to prevent stack
55
 *                      exhaustion.
56
 */
57
58
439k
#define DECODE_FLAG_HAVE_TAG 1
59
144k
#define DECODE_FLAG_CONSTRUCTED (1<<1)
60
168k
#define DECODE_FLAG_LEVEL1 (1<<2)
61
90.8k
#define DECODE_FLAG_LEVEL2 (1<<3)
62
14.3k
#define DECODE_FLAG_LEVEL3 (1<<4)
63
64
56.4M
#define DECR_LEN(l, s) do { \
65
56.4M
    l -= s; \
66
56.4M
    if (l < 0) { \
67
570
      warn(); \
68
570
      result = ASN1_DER_ERROR; \
69
570
      goto cleanup; \
70
570
    } \
71
56.4M
  } while (0)
72
73
static int
74
_asn1_get_indefinite_length_string (const unsigned char *der, int der_len,
75
            int *len);
76
77
static int
78
_asn1_decode_simple_ber (unsigned int etype, const unsigned char *der,
79
       unsigned int _der_len, unsigned char **str,
80
       unsigned int *str_len, unsigned int *ber_len,
81
       unsigned dflags);
82
83
static int
84
_asn1_decode_simple_der (unsigned int etype, const unsigned char *der,
85
       unsigned int _der_len, const unsigned char **str,
86
       unsigned int *str_len, unsigned dflags);
87
88
static void
89
_asn1_error_description_tag_error (asn1_node node, char *ErrorDescription)
90
1.73k
{
91
92
1.73k
  Estrcpy (ErrorDescription, ":: tag error near element '");
93
1.73k
  _asn1_hierarchical_name (node, ErrorDescription + strlen (ErrorDescription),
94
1.73k
         ASN1_MAX_ERROR_DESCRIPTION_SIZE - 40);
95
1.73k
  Estrcat (ErrorDescription, "'");
96
97
1.73k
}
98
99
/**
100
 * asn1_get_length_der:
101
 * @der: DER data to decode.
102
 * @der_len: Length of DER data to decode.
103
 * @len: Output variable containing the length of the DER length field.
104
 *
105
 * Extract a length field from DER data.
106
 *
107
 * Returns: Return the decoded length value, or -1 on indefinite
108
 *   length, or -2 when the value was too big to fit in a int, or -4
109
 *   when the decoded length value plus @len would exceed @der_len.
110
 **/
111
long
112
asn1_get_length_der (const unsigned char *der, int der_len, int *len)
113
21.9M
{
114
21.9M
  unsigned int ans;
115
21.9M
  int k, punt, sum;
116
117
21.9M
  *len = 0;
118
21.9M
  if (der_len <= 0)
119
10.2k
    return 0;
120
121
21.9M
  if (!(der[0] & 128))
122
18.2M
    {
123
      /* short form */
124
18.2M
      *len = 1;
125
18.2M
      ans = der[0];
126
18.2M
    }
127
3.72M
  else
128
3.72M
    {
129
      /* Long form */
130
3.72M
      k = der[0] & 0x7F;
131
3.72M
      punt = 1;
132
3.72M
      if (k)
133
3.61M
  {     /* definite length method */
134
3.61M
    ans = 0;
135
10.6M
    while (punt <= k && punt < der_len)
136
7.02M
      {
137
7.02M
        if (INT_MULTIPLY_OVERFLOW (ans, 256))
138
3.21k
    return -2;
139
7.02M
        ans *= 256;
140
141
7.02M
        if (INT_ADD_OVERFLOW (ans, ((unsigned) der[punt])))
142
0
    return -2;
143
7.02M
        ans += der[punt];
144
7.02M
        punt++;
145
7.02M
      }
146
3.61M
  }
147
103k
      else
148
103k
  {     /* indefinite length method */
149
103k
    *len = punt;
150
103k
    return -1;
151
103k
  }
152
153
3.61M
      *len = punt;
154
3.61M
    }
155
156
21.8M
  sum = ans;
157
21.8M
  if (ans >= INT_MAX || INT_ADD_OVERFLOW (sum, (*len)))
158
2.81k
    return -2;
159
21.8M
  sum += *len;
160
161
21.8M
  if (sum > der_len)
162
20.4k
    return -4;
163
164
21.8M
  return ans;
165
21.8M
}
166
167
/**
168
 * asn1_get_tag_der:
169
 * @der: DER data to decode.
170
 * @der_len: Length of DER data to decode.
171
 * @cls: Output variable containing decoded class.
172
 * @len: Output variable containing the length of the DER TAG data.
173
 * @tag: Output variable containing the decoded tag (may be %NULL).
174
 *
175
 * Decode the class and TAG from DER code.
176
 *
177
 * Returns: Returns %ASN1_SUCCESS on success, or an error.
178
 **/
179
int
180
asn1_get_tag_der (const unsigned char *der, int der_len,
181
      unsigned char *cls, int *len, unsigned long *tag)
182
26.0M
{
183
26.0M
  unsigned int ris;
184
26.0M
  int punt;
185
186
26.0M
  if (der == NULL || der_len < 2 || len == NULL)
187
2.82k
    return ASN1_DER_ERROR;
188
189
26.0M
  *cls = der[0] & 0xE0;
190
26.0M
  if ((der[0] & 0x1F) != 0x1F)
191
25.6M
    {
192
      /* short form */
193
25.6M
      *len = 1;
194
25.6M
      ris = der[0] & 0x1F;
195
25.6M
    }
196
340k
  else
197
340k
    {
198
      /* Long form */
199
340k
      punt = 1;
200
340k
      ris = 0;
201
339M
      while (punt < der_len && der[punt] & 128)
202
338M
  {
203
204
338M
    if (INT_MULTIPLY_OVERFLOW (ris, 128))
205
1.29k
      return ASN1_DER_ERROR;
206
338M
    ris *= 128;
207
208
338M
    if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
209
0
      return ASN1_DER_ERROR;
210
338M
    ris += (der[punt] & 0x7F);
211
338M
    punt++;
212
338M
  }
213
214
339k
      if (punt >= der_len)
215
1.91k
  return ASN1_DER_ERROR;
216
217
337k
      if (INT_MULTIPLY_OVERFLOW (ris, 128))
218
2.07k
  return ASN1_DER_ERROR;
219
335k
      ris *= 128;
220
221
335k
      if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
222
0
  return ASN1_DER_ERROR;
223
335k
      ris += (der[punt] & 0x7F);
224
335k
      punt++;
225
226
335k
      *len = punt;
227
335k
    }
228
229
26.0M
  if (tag)
230
25.9M
    *tag = ris;
231
26.0M
  return ASN1_SUCCESS;
232
26.0M
}
233
234
/**
235
 * asn1_get_length_ber:
236
 * @ber: BER data to decode.
237
 * @ber_len: Length of BER data to decode.
238
 * @len: Output variable containing the length of the BER length field.
239
 *
240
 * Extract a length field from BER data.  The difference to
241
 * asn1_get_length_der() is that this function will return a length
242
 * even if the value has indefinite encoding.
243
 *
244
 * Returns: Return the decoded length value, or negative value when
245
 *   the value was too big.
246
 *
247
 * Since: 2.0
248
 **/
249
long
250
asn1_get_length_ber (const unsigned char *ber, int ber_len, int *len)
251
103k
{
252
103k
  int ret;
253
103k
  long err;
254
255
103k
  ret = asn1_get_length_der (ber, ber_len, len);
256
257
103k
  if (ret == -1 && ber_len > 1)
258
3.78k
    {       /* indefinite length method */
259
3.78k
      err = _asn1_get_indefinite_length_string (ber + 1, ber_len - 1, &ret);
260
3.78k
      if (err != ASN1_SUCCESS)
261
1.99k
  return -3;
262
3.78k
    }
263
264
101k
  return ret;
265
103k
}
266
267
/**
268
 * asn1_get_octet_der:
269
 * @der: DER data to decode containing the OCTET SEQUENCE.
270
 * @der_len: The length of the @der data to decode.
271
 * @ret_len: Output variable containing the encoded length of the DER data.
272
 * @str: Pre-allocated output buffer to put decoded OCTET SEQUENCE in.
273
 * @str_size: Length of pre-allocated output buffer.
274
 * @str_len: Output variable containing the length of the contents of the OCTET SEQUENCE.
275
 *
276
 * Extract an OCTET SEQUENCE from DER data. Note that this function
277
 * expects the DER data past the tag field, i.e., the length and
278
 * content octets.
279
 *
280
 * Returns: Returns %ASN1_SUCCESS on success, or an error.
281
 **/
282
int
283
asn1_get_octet_der (const unsigned char *der, int der_len,
284
        int *ret_len, unsigned char *str, int str_size,
285
        int *str_len)
286
2.03M
{
287
2.03M
  int len_len = 0;
288
289
2.03M
  if (der_len <= 0)
290
0
    return ASN1_GENERIC_ERROR;
291
292
2.03M
  *str_len = asn1_get_length_der (der, der_len, &len_len);
293
294
2.03M
  if (*str_len < 0)
295
0
    return ASN1_DER_ERROR;
296
297
2.03M
  *ret_len = *str_len + len_len;
298
2.03M
  if (str_size >= *str_len)
299
1.11M
    {
300
1.11M
      if (*str_len > 0 && str != NULL)
301
1.10M
  memcpy (str, der + len_len, *str_len);
302
1.11M
    }
303
925k
  else
304
925k
    {
305
925k
      return ASN1_MEM_ERROR;
306
925k
    }
307
308
1.11M
  return ASN1_SUCCESS;
309
2.03M
}
310
311
312
/*-
313
 * _asn1_get_time_der:
314
 * @type: %ASN1_ETYPE_GENERALIZED_TIME or %ASN1_ETYPE_UTC_TIME
315
 * @der: DER data to decode containing the time
316
 * @der_len: Length of DER data to decode.
317
 * @ret_len: Output variable containing the length of the DER data.
318
 * @str: Pre-allocated output buffer to put the textual time in.
319
 * @str_size: Length of pre-allocated output buffer.
320
 * @flags: Zero or %ASN1_DECODE_FLAG_STRICT_DER
321
 *
322
 * Performs basic checks in the DER encoded time object and returns its textual form.
323
 * The textual form will be in the YYYYMMDD000000Z format for GeneralizedTime
324
 * and YYMMDD000000Z for UTCTime.
325
 *
326
 * Returns: %ASN1_SUCCESS on success, or an error.
327
 -*/
328
static int
329
_asn1_get_time_der (unsigned type, const unsigned char *der, int der_len,
330
        int *ret_len, char *str, int str_size, unsigned flags)
331
238k
{
332
238k
  int len_len, str_len;
333
238k
  unsigned i;
334
238k
  unsigned sign_count = 0;
335
238k
  unsigned dot_count = 0;
336
238k
  const unsigned char *p;
337
338
238k
  if (der_len <= 0 || str == NULL)
339
28
    return ASN1_DER_ERROR;
340
341
238k
  str_len = asn1_get_length_der (der, der_len, &len_len);
342
238k
  if (str_len <= 0 || str_size < str_len)
343
1.28k
    return ASN1_DER_ERROR;
344
345
  /* perform some sanity checks on the data */
346
237k
  if (str_len < 8)
347
61
    {
348
61
      warn ();
349
61
      return ASN1_TIME_ENCODING_ERROR;
350
61
    }
351
352
237k
  if ((flags & ASN1_DECODE_FLAG_STRICT_DER)
353
237k
      && !(flags & ASN1_DECODE_FLAG_ALLOW_INCORRECT_TIME))
354
237k
    {
355
237k
      p = &der[len_len];
356
3.18M
      for (i = 0; i < (unsigned) (str_len - 1); i++)
357
2.94M
  {
358
2.94M
    if (c_isdigit (p[i]) == 0)
359
3.04k
      {
360
3.04k
        if (type == ASN1_ETYPE_GENERALIZED_TIME)
361
2.95k
    {
362
      /* tolerate lax encodings */
363
2.95k
      if (p[i] == '.' && dot_count == 0)
364
2.69k
        {
365
2.69k
          dot_count++;
366
2.69k
          continue;
367
2.69k
        }
368
369
      /* This is not really valid DER, but there are
370
       * structures using that */
371
260
      if (!(flags & ASN1_DECODE_FLAG_STRICT_DER) &&
372
0
          (p[i] == '+' || p[i] == '-') && sign_count == 0)
373
0
        {
374
0
          sign_count++;
375
0
          continue;
376
0
        }
377
260
    }
378
379
350
        warn ();
380
350
        return ASN1_TIME_ENCODING_ERROR;
381
3.04k
      }
382
2.94M
  }
383
384
236k
      if (sign_count == 0 && p[str_len - 1] != 'Z')
385
139
  {
386
139
    warn ();
387
139
    return ASN1_TIME_ENCODING_ERROR;
388
139
  }
389
236k
    }
390
236k
  memcpy (str, der + len_len, str_len);
391
236k
  str[str_len] = 0;
392
236k
  *ret_len = str_len + len_len;
393
394
236k
  return ASN1_SUCCESS;
395
237k
}
396
397
/**
398
 * asn1_get_object_id_der:
399
 * @der: DER data to decode containing the OBJECT IDENTIFIER
400
 * @der_len: Length of DER data to decode.
401
 * @ret_len: Output variable containing the length of the DER data.
402
 * @str: Pre-allocated output buffer to put the textual object id in.
403
 * @str_size: Length of pre-allocated output buffer.
404
 *
405
 * Converts a DER encoded object identifier to its textual form. This
406
 * function expects the DER object identifier without the tag.
407
 *
408
 * Returns: %ASN1_SUCCESS on success, or an error.
409
 **/
410
int
411
asn1_get_object_id_der (const unsigned char *der, int der_len, int *ret_len,
412
      char *str, int str_size)
413
1.66M
{
414
1.66M
  int len_len, len, k;
415
1.66M
  int leading, parsed;
416
1.66M
  char temp[LTOSTR_MAX_SIZE];
417
1.66M
  uint64_t val, val1, val0;
418
419
1.66M
  *ret_len = 0;
420
1.66M
  if (str && str_size > 0)
421
1.66M
    str[0] = 0;     /* no oid */
422
423
1.66M
  if (str == NULL || der_len <= 0)
424
63
    return ASN1_GENERIC_ERROR;
425
426
1.66M
  len = asn1_get_length_der (der, der_len, &len_len);
427
428
1.66M
  if (len <= 0 || len + len_len > der_len)
429
2.13k
    return ASN1_DER_ERROR;
430
431
  /* leading octet can never be 0x80 */
432
1.66M
  if (der[len_len] == 0x80)
433
68
    return ASN1_DER_ERROR;
434
435
1.66M
  val0 = 0;
436
437
1.86M
  for (k = 0; k < len; k++)
438
1.83M
    {
439
1.83M
      if (INT_LEFT_SHIFT_OVERFLOW (val0, 7))
440
212
  return ASN1_DER_ERROR;
441
442
1.83M
      val0 <<= 7;
443
1.83M
      val0 |= der[len_len + k] & 0x7F;
444
1.83M
      if (!(der[len_len + k] & 0x80))
445
1.63M
  break;
446
1.83M
    }
447
1.66M
  parsed = ++k;
448
449
  /* val0 = (X*40) + Y, X={0,1,2}, Y<=39 when X={0,1} */
450
  /* X = val, Y = val1 */
451
452
  /* check if X == 0  */
453
1.66M
  val = 0;
454
1.66M
  val1 = val0;
455
1.66M
  if (val1 > 39)
456
1.59M
    {
457
1.59M
      val = 1;
458
1.59M
      val1 = val0 - 40;
459
1.59M
      if (val1 > 39)
460
1.01M
  {
461
1.01M
    val = 2;
462
1.01M
    val1 = val0 - 80;
463
1.01M
  }
464
1.59M
    }
465
466
1.66M
  _asn1_str_cpy (str, str_size, _asn1_ltostr (val, temp));
467
1.66M
  _asn1_str_cat (str, str_size, ".");
468
1.66M
  _asn1_str_cat (str, str_size, _asn1_ltostr (val1, temp));
469
470
1.66M
  val = 0;
471
1.66M
  leading = 1;
472
16.0M
  for (k = parsed; k < len; k++)
473
14.3M
    {
474
      /* X.690 mandates that the leading byte must never be 0x80
475
       */
476
14.3M
      if (leading != 0 && der[len_len + k] == 0x80)
477
161
  return ASN1_DER_ERROR;
478
14.3M
      leading = 0;
479
480
      /* check for wrap around */
481
14.3M
      if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
482
371
  return ASN1_DER_ERROR;
483
484
14.3M
      val = val << 7;
485
14.3M
      val |= der[len_len + k] & 0x7F;
486
487
14.3M
      if (!(der[len_len + k] & 0x80))
488
12.6M
  {
489
12.6M
    _asn1_str_cat (str, str_size, ".");
490
12.6M
    _asn1_str_cat (str, str_size, _asn1_ltostr (val, temp));
491
12.6M
    val = 0;
492
12.6M
    leading = 1;
493
12.6M
  }
494
14.3M
    }
495
496
1.66M
  if (INT_ADD_OVERFLOW (len, len_len))
497
0
    return ASN1_DER_ERROR;
498
499
1.66M
  *ret_len = len + len_len;
500
501
1.66M
  return ASN1_SUCCESS;
502
1.66M
}
503
504
/**
505
 * asn1_get_bit_der:
506
 * @der: DER data to decode containing the BIT SEQUENCE.
507
 * @der_len: Length of DER data to decode.
508
 * @ret_len: Output variable containing the length of the DER data.
509
 * @str: Pre-allocated output buffer to put decoded BIT SEQUENCE in.
510
 * @str_size: Length of pre-allocated output buffer.
511
 * @bit_len: Output variable containing the size of the BIT SEQUENCE.
512
 *
513
 * Extract a BIT SEQUENCE from DER data.
514
 *
515
 * Returns: %ASN1_SUCCESS on success, or an error.
516
 **/
517
int
518
asn1_get_bit_der (const unsigned char *der, int der_len,
519
      int *ret_len, unsigned char *str, int str_size,
520
      int *bit_len)
521
333k
{
522
333k
  int len_len = 0, len_byte;
523
524
333k
  if (der_len <= 0)
525
0
    return ASN1_GENERIC_ERROR;
526
527
333k
  len_byte = asn1_get_length_der (der, der_len, &len_len) - 1;
528
333k
  if (len_byte < 0)
529
2.72k
    return ASN1_DER_ERROR;
530
531
330k
  *ret_len = len_byte + len_len + 1;
532
330k
  *bit_len = len_byte * 8 - der[len_len];
533
534
330k
  if (*bit_len < 0)
535
895
    return ASN1_DER_ERROR;
536
537
329k
  if (str_size >= len_byte)
538
183k
    {
539
183k
      if (len_byte > 0 && str)
540
183k
  memcpy (str, der + len_len + 1, len_byte);
541
183k
    }
542
146k
  else
543
146k
    {
544
146k
      return ASN1_MEM_ERROR;
545
146k
    }
546
547
183k
  return ASN1_SUCCESS;
548
329k
}
549
550
/* tag_len: the total tag length (explicit+inner)
551
 * inner_tag_len: the inner_tag length
552
 */
553
static int
554
_asn1_extract_tag_der (asn1_node node, const unsigned char *der, int der_len,
555
           int *tag_len, int *inner_tag_len, unsigned flags)
556
17.2M
{
557
17.2M
  asn1_node p;
558
17.2M
  int counter, len2, len3, is_tag_implicit;
559
17.2M
  int result;
560
17.2M
  unsigned long tag, tag_implicit = 0;
561
17.2M
  unsigned char class, class2, class_implicit = 0;
562
563
17.2M
  if (der_len <= 0)
564
4.54k
    return ASN1_GENERIC_ERROR;
565
566
17.2M
  counter = is_tag_implicit = 0;
567
568
17.2M
  if (node->type & CONST_TAG)
569
1.65M
    {
570
1.65M
      p = node->down;
571
3.97M
      while (p)
572
2.40M
  {
573
2.40M
    if (type_field (p->type) == ASN1_ETYPE_TAG)
574
1.66M
      {
575
1.66M
        if (p->type & CONST_APPLICATION)
576
0
    class2 = ASN1_CLASS_APPLICATION;
577
1.66M
        else if (p->type & CONST_UNIVERSAL)
578
0
    class2 = ASN1_CLASS_UNIVERSAL;
579
1.66M
        else if (p->type & CONST_PRIVATE)
580
0
    class2 = ASN1_CLASS_PRIVATE;
581
1.66M
        else
582
1.66M
    class2 = ASN1_CLASS_CONTEXT_SPECIFIC;
583
584
1.66M
        if (p->type & CONST_EXPLICIT)
585
422k
    {
586
422k
      if (asn1_get_tag_der
587
422k
          (der + counter, der_len, &class, &len2,
588
422k
           &tag) != ASN1_SUCCESS)
589
1.41k
        return ASN1_DER_ERROR;
590
591
420k
      DECR_LEN (der_len, len2);
592
420k
      counter += len2;
593
594
420k
      if (flags & ASN1_DECODE_FLAG_STRICT_DER)
595
374k
        len3 =
596
374k
          asn1_get_length_der (der + counter, der_len, &len2);
597
46.3k
      else
598
46.3k
        len3 =
599
46.3k
          asn1_get_length_ber (der + counter, der_len, &len2);
600
420k
      if (len3 < 0)
601
9.06k
        return ASN1_DER_ERROR;
602
603
411k
      DECR_LEN (der_len, len2);
604
411k
      counter += len2;
605
606
411k
      if (!is_tag_implicit)
607
411k
        {
608
411k
          if ((class != (class2 | ASN1_CLASS_STRUCTURED)) ||
609
344k
        (tag != strtoul ((char *) p->value, NULL, 10)))
610
75.3k
      return ASN1_TAG_ERROR;
611
411k
        }
612
0
      else
613
0
        {   /* ASN1_TAG_IMPLICIT */
614
0
          if ((class != class_implicit) || (tag != tag_implicit))
615
0
      return ASN1_TAG_ERROR;
616
0
        }
617
336k
      is_tag_implicit = 0;
618
336k
    }
619
1.24M
        else
620
1.24M
    {   /* ASN1_TAG_IMPLICIT */
621
1.24M
      if (!is_tag_implicit)
622
1.24M
        {
623
1.24M
          if ((type_field (node->type) == ASN1_ETYPE_SEQUENCE) ||
624
1.04M
        (type_field (node->type) == ASN1_ETYPE_SEQUENCE_OF)
625
1.03M
        || (type_field (node->type) == ASN1_ETYPE_SET)
626
1.03M
        || (type_field (node->type) == ASN1_ETYPE_SET_OF))
627
243k
      class2 |= ASN1_CLASS_STRUCTURED;
628
1.24M
          class_implicit = class2;
629
1.24M
          tag_implicit = strtoul ((char *) p->value, NULL, 10);
630
1.24M
          is_tag_implicit = 1;
631
1.24M
        }
632
1.24M
    }
633
1.66M
      }
634
2.32M
    p = p->right;
635
2.32M
  }
636
1.65M
    }
637
638
17.1M
  if (is_tag_implicit)
639
1.24M
    {
640
1.24M
      if (asn1_get_tag_der
641
1.24M
    (der + counter, der_len, &class, &len2, &tag) != ASN1_SUCCESS)
642
2.69k
  return ASN1_DER_ERROR;
643
644
1.24M
      DECR_LEN (der_len, len2);
645
646
1.24M
      if ((class != class_implicit) || (tag != tag_implicit))
647
737k
  {
648
737k
    if (type_field (node->type) == ASN1_ETYPE_OCTET_STRING)
649
28.0k
      {
650
28.0k
        class_implicit |= ASN1_CLASS_STRUCTURED;
651
28.0k
        if ((class != class_implicit) || (tag != tag_implicit))
652
22.6k
    return ASN1_TAG_ERROR;
653
28.0k
      }
654
709k
    else
655
709k
      return ASN1_TAG_ERROR;
656
737k
  }
657
1.24M
    }
658
15.8M
  else
659
15.8M
    {
660
15.8M
      unsigned type = type_field (node->type);
661
15.8M
      if (type == ASN1_ETYPE_TAG)
662
20.7k
  {
663
20.7k
    *tag_len = 0;
664
20.7k
    if (inner_tag_len)
665
20.7k
      *inner_tag_len = 0;
666
20.7k
    return ASN1_SUCCESS;
667
20.7k
  }
668
669
15.8M
      if (asn1_get_tag_der
670
15.8M
    (der + counter, der_len, &class, &len2, &tag) != ASN1_SUCCESS)
671
2.51k
  return ASN1_DER_ERROR;
672
673
15.8M
      DECR_LEN (der_len, len2);
674
675
15.8M
      switch (type)
676
15.8M
  {
677
0
  case ASN1_ETYPE_NULL:
678
581k
  case ASN1_ETYPE_BOOLEAN:
679
1.65M
  case ASN1_ETYPE_INTEGER:
680
1.65M
  case ASN1_ETYPE_ENUMERATED:
681
3.37M
  case ASN1_ETYPE_OBJECT_ID:
682
3.37M
  case ASN1_ETYPE_GENERALSTRING:
683
3.37M
  case ASN1_ETYPE_NUMERIC_STRING:
684
3.37M
  case ASN1_ETYPE_IA5_STRING:
685
3.48M
  case ASN1_ETYPE_TELETEX_STRING:
686
3.64M
  case ASN1_ETYPE_PRINTABLE_STRING:
687
3.70M
  case ASN1_ETYPE_UNIVERSAL_STRING:
688
3.79M
  case ASN1_ETYPE_BMP_STRING:
689
3.84M
  case ASN1_ETYPE_UTF8_STRING:
690
3.84M
  case ASN1_ETYPE_VISIBLE_STRING:
691
4.08M
  case ASN1_ETYPE_BIT_STRING:
692
6.83M
  case ASN1_ETYPE_SEQUENCE:
693
7.67M
  case ASN1_ETYPE_SEQUENCE_OF:
694
7.67M
  case ASN1_ETYPE_SET:
695
8.46M
  case ASN1_ETYPE_SET_OF:
696
8.67M
  case ASN1_ETYPE_GENERALIZED_TIME:
697
9.23M
  case ASN1_ETYPE_UTC_TIME:
698
9.23M
    if ((class != _asn1_tags[type].class)
699
9.10M
        || (tag != _asn1_tags[type].tag))
700
806k
      return ASN1_DER_ERROR;
701
8.42M
    break;
702
703
8.42M
  case ASN1_ETYPE_OCTET_STRING:
704
    /* OCTET STRING is handled differently to allow
705
     * BER encodings (structured class). */
706
739k
    if (((class != ASN1_CLASS_UNIVERSAL)
707
29.7k
         && (class != (ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED)))
708
739k
        || (tag != ASN1_TAG_OCTET_STRING))
709
27.9k
      return ASN1_DER_ERROR;
710
711k
    break;
711
5.90M
  case ASN1_ETYPE_ANY:
712
5.90M
    counter -= len2;
713
5.90M
    break;
714
0
  case ASN1_ETYPE_CHOICE:
715
0
    counter -= len2;
716
0
    break;
717
0
  default:
718
0
    return ASN1_DER_ERROR;
719
0
    break;
720
15.8M
  }
721
15.8M
    }
722
723
15.5M
  counter += len2;
724
15.5M
  *tag_len = counter;
725
15.5M
  if (inner_tag_len)
726
13.7M
    *inner_tag_len = len2;
727
15.5M
  return ASN1_SUCCESS;
728
729
0
cleanup:
730
0
  return result;
731
17.1M
}
732
733
static int
734
extract_tag_der_recursive (asn1_node node, const unsigned char *der,
735
         int der_len, int *ret_len, int *inner_len,
736
         unsigned flags)
737
17.2M
{
738
17.2M
  asn1_node p;
739
17.2M
  int ris = ASN1_DER_ERROR;
740
741
17.2M
  if (type_field (node->type) == ASN1_ETYPE_CHOICE)
742
1.84M
    {
743
1.84M
      p = node->down;
744
1.84M
      while (p)
745
1.84M
  {
746
1.84M
    ris =
747
1.84M
      _asn1_extract_tag_der (p, der, der_len, ret_len, inner_len,
748
1.84M
           flags);
749
1.84M
    if (ris == ASN1_SUCCESS)
750
1.84M
      break;
751
0
    p = p->right;
752
0
  }
753
754
1.84M
      *ret_len = 0;
755
1.84M
      return ris;
756
1.84M
    }
757
15.3M
  else
758
15.3M
    return _asn1_extract_tag_der (node, der, der_len, ret_len, inner_len,
759
15.3M
          flags);
760
17.2M
}
761
762
static int
763
_asn1_delete_not_used (asn1_node node)
764
616k
{
765
616k
  asn1_node p, p2;
766
767
616k
  if (node == NULL)
768
0
    return ASN1_ELEMENT_NOT_FOUND;
769
770
616k
  p = node;
771
19.4M
  while (p)
772
18.8M
    {
773
18.8M
      if (p->type & CONST_NOT_USED)
774
190k
  {
775
190k
    p2 = NULL;
776
190k
    if (p != node)
777
190k
      {
778
190k
        p2 = _asn1_find_left (p);
779
190k
        if (!p2)
780
1.88k
    p2 = _asn1_find_up (p);
781
190k
      }
782
190k
    asn1_delete_structure (&p);
783
190k
    p = p2;
784
190k
  }
785
786
18.8M
      if (!p)
787
0
  break;     /* reach node */
788
789
18.8M
      if (p->down)
790
8.71M
  {
791
8.71M
    p = p->down;
792
8.71M
  }
793
10.1M
      else
794
10.1M
  {
795
10.1M
    if (p == node)
796
60.8k
      p = NULL;
797
10.0M
    else if (p->right)
798
3.97M
      p = p->right;
799
6.11M
    else
800
6.11M
      {
801
8.71M
        while (1)
802
8.71M
    {
803
8.71M
      p = _asn1_find_up (p);
804
8.71M
      if (p == node)
805
555k
        {
806
555k
          p = NULL;
807
555k
          break;
808
555k
        }
809
8.15M
      if (p->right)
810
5.55M
        {
811
5.55M
          p = p->right;
812
5.55M
          break;
813
5.55M
        }
814
8.15M
    }
815
6.11M
      }
816
10.1M
  }
817
18.8M
    }
818
616k
  return ASN1_SUCCESS;
819
616k
}
820
821
static int
822
_asn1_get_indefinite_length_string (const unsigned char *der,
823
            int der_len, int *len)
824
13.6k
{
825
13.6k
  int len2, len3, counter, indefinite;
826
13.6k
  int result;
827
13.6k
  unsigned long tag;
828
13.6k
  unsigned char class;
829
830
13.6k
  counter = indefinite = 0;
831
832
4.43M
  while (1)
833
4.43M
    {
834
4.43M
      if (HAVE_TWO (der_len) && (der[counter] == 0)
835
153k
    && (der[counter + 1] == 0))
836
34.2k
  {
837
34.2k
    counter += 2;
838
34.2k
    DECR_LEN (der_len, 2);
839
840
34.2k
    indefinite--;
841
34.2k
    if (indefinite <= 0)
842
11.5k
      break;
843
22.7k
    else
844
22.7k
      continue;
845
34.2k
  }
846
847
4.39M
      if (asn1_get_tag_der
848
4.39M
    (der + counter, der_len, &class, &len2, &tag) != ASN1_SUCCESS)
849
828
  return ASN1_DER_ERROR;
850
851
4.39M
      DECR_LEN (der_len, len2);
852
4.39M
      counter += len2;
853
854
4.39M
      len2 = asn1_get_length_der (der + counter, der_len, &len3);
855
4.39M
      if (len2 < -1)
856
1.23k
  return ASN1_DER_ERROR;
857
858
4.39M
      if (len2 == -1)
859
46.2k
  {
860
46.2k
    indefinite++;
861
46.2k
    counter += 1;
862
46.2k
    DECR_LEN (der_len, 1);
863
46.2k
  }
864
4.35M
      else
865
4.35M
  {
866
4.35M
    counter += len2 + len3;
867
4.35M
    DECR_LEN (der_len, len2 + len3);
868
4.35M
  }
869
4.39M
    }
870
871
11.5k
  *len = counter;
872
11.5k
  return ASN1_SUCCESS;
873
874
0
cleanup:
875
0
  return result;
876
13.6k
}
877
878
static void
879
delete_unneeded_choice_fields (asn1_node p)
880
1.84M
{
881
1.84M
  asn1_node p2;
882
883
2.96M
  while (p->right)
884
1.11M
    {
885
1.11M
      p2 = p->right;
886
1.11M
      asn1_delete_structure (&p2);
887
1.11M
    }
888
1.84M
}
889
890
891
/**
892
 * asn1_der_decoding2
893
 * @element: pointer to an ASN1 structure.
894
 * @ider: vector that contains the DER encoding.
895
 * @max_ider_len: pointer to an integer giving the information about the
896
 *   maximal number of bytes occupied by *@ider. The real size of the DER
897
 *   encoding is returned through this pointer.
898
 * @flags: flags controlling the behaviour of the function.
899
 * @errorDescription: null-terminated string contains details when an
900
 *   error occurred.
901
 *
902
 * Fill the structure *@element with values of a DER encoding string. The
903
 * structure must just be created with function asn1_create_element().
904
 *
905
 * If %ASN1_DECODE_FLAG_ALLOW_PADDING flag is set then the function will ignore
906
 * padding after the decoded DER data. Upon a successful return the value of
907
 * *@max_ider_len will be set to the number of bytes decoded.
908
 *
909
 * If %ASN1_DECODE_FLAG_STRICT_DER flag is set then the function will
910
 * not decode any BER-encoded elements.
911
 *
912
 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
913
 *   if @ELEMENT is %NULL, and %ASN1_TAG_ERROR or
914
 *   %ASN1_DER_ERROR if the der encoding doesn't match the structure
915
 *   name (*@ELEMENT deleted).
916
 **/
917
int
918
asn1_der_decoding2 (asn1_node *element, const void *ider, int *max_ider_len,
919
        unsigned int flags, char *errorDescription)
920
790k
{
921
790k
  asn1_node node, p, p2, p3;
922
790k
  char temp[128];
923
790k
  int counter, len2, len3, len4, move, ris, tlen;
924
790k
  struct node_tail_cache_st tcache = { NULL, NULL };
925
790k
  unsigned char class;
926
790k
  unsigned long tag;
927
790k
  int tag_len;
928
790k
  int indefinite, result, total_len = *max_ider_len, ider_len = *max_ider_len;
929
790k
  int inner_tag_len;
930
790k
  unsigned char *ptmp;
931
790k
  const unsigned char *ptag;
932
790k
  const unsigned char *der = ider;
933
934
790k
  node = *element;
935
936
790k
  if (errorDescription != NULL)
937
129k
    errorDescription[0] = 0;
938
939
790k
  if (node == NULL)
940
0
    return ASN1_ELEMENT_NOT_FOUND;
941
942
790k
  if (node->type & CONST_OPTION)
943
0
    {
944
0
      result = ASN1_GENERIC_ERROR;
945
0
      warn ();
946
0
      goto cleanup;
947
0
    }
948
949
790k
  counter = 0;
950
790k
  move = DOWN;
951
790k
  p = node;
952
24.1M
  while (1)
953
24.1M
    {
954
24.1M
      tag_len = 0;
955
24.1M
      inner_tag_len = 0;
956
24.1M
      ris = ASN1_SUCCESS;
957
24.1M
      if (move != UP)
958
14.8M
  {
959
14.8M
    if (p->type & CONST_SET)
960
0
      {
961
0
        p2 = _asn1_find_up (p);
962
0
        len2 = p2->tmp_ival;
963
0
        if (len2 == -1)
964
0
    {
965
0
      if (HAVE_TWO (ider_len) && !der[counter]
966
0
          && !der[counter + 1])
967
0
        {
968
0
          p = p2;
969
0
          move = UP;
970
0
          counter += 2;
971
0
          DECR_LEN (ider_len, 2);
972
0
          continue;
973
0
        }
974
0
    }
975
0
        else if (counter == len2)
976
0
    {
977
0
      p = p2;
978
0
      move = UP;
979
0
      continue;
980
0
    }
981
0
        else if (counter > len2)
982
0
    {
983
0
      result = ASN1_DER_ERROR;
984
0
      warn ();
985
0
      goto cleanup;
986
0
    }
987
0
        p2 = p2->down;
988
0
        while (p2)
989
0
    {
990
0
      if ((p2->type & CONST_SET) && (p2->type & CONST_NOT_USED))
991
0
        {
992
0
          ris =
993
0
      extract_tag_der_recursive (p2, der + counter,
994
0
               ider_len, &len2, NULL,
995
0
               flags);
996
0
          if (ris == ASN1_SUCCESS)
997
0
      {
998
0
        p2->type &= ~CONST_NOT_USED;
999
0
        p = p2;
1000
0
        break;
1001
0
      }
1002
0
        }
1003
0
      p2 = p2->right;
1004
0
    }
1005
0
        if (p2 == NULL)
1006
0
    {
1007
0
      result = ASN1_DER_ERROR;
1008
0
      warn ();
1009
0
      goto cleanup;
1010
0
    }
1011
0
      }
1012
1013
    /* the position in the DER structure this starts */
1014
14.8M
    p->start = counter;
1015
14.8M
    p->end = total_len - 1;
1016
1017
14.8M
    if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1018
1.61M
      {
1019
1.61M
        p2 = _asn1_find_up (p);
1020
1.61M
        len2 = p2->tmp_ival;
1021
1.61M
        if (counter == len2)
1022
317k
    {
1023
317k
      if (p->right)
1024
53.5k
        {
1025
53.5k
          p2 = p->right;
1026
53.5k
          move = RIGHT;
1027
53.5k
        }
1028
263k
      else
1029
263k
        move = UP;
1030
1031
317k
      if (p->type & CONST_OPTION)
1032
314k
        asn1_delete_structure (&p);
1033
1034
317k
      p = p2;
1035
317k
      continue;
1036
317k
    }
1037
1.61M
      }
1038
1039
14.5M
    if (type_field (p->type) == ASN1_ETYPE_CHOICE)
1040
1.86M
      {
1041
2.77M
        while (p->down)
1042
2.75M
    {
1043
2.75M
      ris =
1044
2.75M
        extract_tag_der_recursive (p->down, der + counter,
1045
2.75M
                 ider_len, &len2, NULL, flags);
1046
1047
2.75M
      if (ris == ASN1_SUCCESS)
1048
1.84M
        {
1049
1.84M
          delete_unneeded_choice_fields (p->down);
1050
1.84M
          break;
1051
1.84M
        }
1052
909k
      else if (ris == ASN1_ERROR_TYPE_ANY)
1053
0
        {
1054
0
          result = ASN1_ERROR_TYPE_ANY;
1055
0
          warn ();
1056
0
          goto cleanup;
1057
0
        }
1058
909k
      else
1059
909k
        {
1060
909k
          p2 = p->down;
1061
909k
          asn1_delete_structure (&p2);
1062
909k
        }
1063
2.75M
    }
1064
1065
1.86M
        if (p->down == NULL)
1066
17.6k
    {
1067
17.6k
      if (!(p->type & CONST_OPTION))
1068
14.3k
        {
1069
14.3k
          result = ASN1_DER_ERROR;
1070
14.3k
          warn ();
1071
14.3k
          goto cleanup;
1072
14.3k
        }
1073
17.6k
    }
1074
1.84M
        else if (type_field (p->type) != ASN1_ETYPE_CHOICE)
1075
0
    p = p->down;
1076
1077
1.84M
        p->start = counter;
1078
1.84M
      }
1079
1080
14.4M
    if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1081
1.29M
      {
1082
1.29M
        p2 = _asn1_find_up (p);
1083
1.29M
        len2 = p2->tmp_ival;
1084
1085
1.29M
        if ((len2 != -1) && (counter > len2))
1086
3.65k
    ris = ASN1_TAG_ERROR;
1087
1.29M
      }
1088
1089
14.4M
    if (ris == ASN1_SUCCESS)
1090
14.4M
      ris =
1091
14.4M
        extract_tag_der_recursive (p, der + counter, ider_len,
1092
14.4M
           &tag_len, &inner_tag_len, flags);
1093
1094
14.4M
    if (ris != ASN1_SUCCESS)
1095
760k
      {
1096
760k
        if (p->type & CONST_OPTION)
1097
214k
    {
1098
214k
      p->type |= CONST_NOT_USED;
1099
214k
      move = RIGHT;
1100
214k
    }
1101
545k
        else if (p->type & CONST_DEFAULT)
1102
416k
    {
1103
416k
      _asn1_set_value (p, NULL, 0);
1104
416k
      move = RIGHT;
1105
416k
    }
1106
128k
        else
1107
128k
    {
1108
128k
      if (errorDescription != NULL)
1109
1.73k
        _asn1_error_description_tag_error (p, errorDescription);
1110
1111
128k
      result = ASN1_TAG_ERROR;
1112
128k
      warn ();
1113
128k
      goto cleanup;
1114
128k
    }
1115
760k
      }
1116
13.7M
    else
1117
13.7M
      {
1118
13.7M
        DECR_LEN (ider_len, tag_len);
1119
13.7M
        counter += tag_len;
1120
13.7M
      }
1121
14.4M
  }
1122
1123
23.7M
      if (ris == ASN1_SUCCESS)
1124
23.0M
  {
1125
23.0M
    switch (type_field (p->type))
1126
23.0M
      {
1127
1.96k
      case ASN1_ETYPE_NULL:
1128
1.96k
        DECR_LEN (ider_len, 1);
1129
1.96k
        if (der[counter])
1130
2
    {
1131
2
      result = ASN1_DER_ERROR;
1132
2
      warn ();
1133
2
      goto cleanup;
1134
2
    }
1135
1.96k
        counter++;
1136
1.96k
        move = RIGHT;
1137
1.96k
        break;
1138
177k
      case ASN1_ETYPE_BOOLEAN:
1139
177k
        DECR_LEN (ider_len, 2);
1140
1141
177k
        if (der[counter++] != 1)
1142
91
    {
1143
91
      result = ASN1_DER_ERROR;
1144
91
      warn ();
1145
91
      goto cleanup;
1146
91
    }
1147
177k
        if (der[counter++] == 0)
1148
10.1k
    _asn1_set_value (p, "F", 1);
1149
167k
        else
1150
167k
    _asn1_set_value (p, "T", 1);
1151
177k
        move = RIGHT;
1152
177k
        break;
1153
986k
      case ASN1_ETYPE_INTEGER:
1154
991k
      case ASN1_ETYPE_ENUMERATED:
1155
991k
        len2 = asn1_get_length_der (der + counter, ider_len, &len3);
1156
991k
        if (len2 < 0)
1157
2.15k
    {
1158
2.15k
      result = ASN1_DER_ERROR;
1159
2.15k
      warn ();
1160
2.15k
      goto cleanup;
1161
2.15k
    }
1162
1163
989k
        DECR_LEN (ider_len, len3 + len2);
1164
1165
989k
        _asn1_set_value (p, der + counter, len3 + len2);
1166
989k
        counter += len3 + len2;
1167
989k
        move = RIGHT;
1168
989k
        break;
1169
1.66M
      case ASN1_ETYPE_OBJECT_ID:
1170
1.66M
        result =
1171
1.66M
    asn1_get_object_id_der (der + counter, ider_len, &len2,
1172
1.66M
          temp, sizeof (temp));
1173
1.66M
        if (result != ASN1_SUCCESS)
1174
3.00k
    {
1175
3.00k
      warn ();
1176
3.00k
      goto cleanup;
1177
3.00k
    }
1178
1179
1.66M
        DECR_LEN (ider_len, len2);
1180
1181
1.66M
        tlen = strlen (temp);
1182
1.66M
        if (tlen > 0)
1183
1.66M
    _asn1_set_value (p, temp, tlen + 1);
1184
1185
1.66M
        counter += len2;
1186
1.66M
        move = RIGHT;
1187
1.66M
        break;
1188
72.8k
      case ASN1_ETYPE_GENERALIZED_TIME:
1189
238k
      case ASN1_ETYPE_UTC_TIME:
1190
238k
        result =
1191
238k
    _asn1_get_time_der (type_field (p->type), der + counter,
1192
238k
            ider_len, &len2, temp, sizeof (temp) - 1,
1193
238k
            flags);
1194
238k
        if (result != ASN1_SUCCESS)
1195
1.86k
    {
1196
1.86k
      warn ();
1197
1.86k
      goto cleanup;
1198
1.86k
    }
1199
1200
236k
        DECR_LEN (ider_len, len2);
1201
1202
236k
        tlen = strlen (temp);
1203
236k
        if (tlen > 0)
1204
236k
    _asn1_set_value (p, temp, tlen);
1205
1206
236k
        counter += len2;
1207
236k
        move = RIGHT;
1208
236k
        break;
1209
721k
      case ASN1_ETYPE_OCTET_STRING:
1210
721k
        if (counter < inner_tag_len)
1211
0
    {
1212
0
      result = ASN1_DER_ERROR;
1213
0
      warn ();
1214
0
      goto cleanup;
1215
0
    }
1216
1217
721k
        ptag = der + counter - inner_tag_len;
1218
721k
        if ((flags & ASN1_DECODE_FLAG_STRICT_DER)
1219
23.7k
      || !(ptag[0] & ASN1_CLASS_STRUCTURED))
1220
717k
    {
1221
717k
      if (ptag[0] & ASN1_CLASS_STRUCTURED)
1222
144
        {
1223
144
          result = ASN1_DER_ERROR;
1224
144
          warn ();
1225
144
          goto cleanup;
1226
144
        }
1227
1228
717k
      len2 = asn1_get_length_der (der + counter, ider_len, &len3);
1229
717k
      if (len2 < 0)
1230
2.00k
        {
1231
2.00k
          result = ASN1_DER_ERROR;
1232
2.00k
          warn ();
1233
2.00k
          goto cleanup;
1234
2.00k
        }
1235
1236
715k
      DECR_LEN (ider_len, len3 + len2);
1237
1238
715k
      _asn1_set_value (p, der + counter, len3 + len2);
1239
715k
      counter += len3 + len2;
1240
715k
    }
1241
3.93k
        else
1242
3.93k
    {
1243
3.93k
      unsigned dflags = 0, vlen, ber_len;
1244
1245
3.93k
      if (ptag[0] & ASN1_CLASS_STRUCTURED)
1246
3.93k
        dflags |= DECODE_FLAG_CONSTRUCTED;
1247
1248
3.93k
      result =
1249
3.93k
        _asn1_decode_simple_ber (type_field (p->type),
1250
3.93k
               der + counter, ider_len, &ptmp,
1251
3.93k
               &vlen, &ber_len, dflags);
1252
3.93k
      if (result != ASN1_SUCCESS)
1253
1.12k
        {
1254
1.12k
          warn ();
1255
1.12k
          goto cleanup;
1256
1.12k
        }
1257
1258
2.81k
      DECR_LEN (ider_len, ber_len);
1259
1260
2.81k
      _asn1_set_value_lv (p, ptmp, vlen);
1261
1262
2.81k
      counter += ber_len;
1263
2.81k
      free (ptmp);
1264
2.81k
    }
1265
717k
        move = RIGHT;
1266
717k
        break;
1267
138
      case ASN1_ETYPE_GENERALSTRING:
1268
138
      case ASN1_ETYPE_NUMERIC_STRING:
1269
100k
      case ASN1_ETYPE_IA5_STRING:
1270
107k
      case ASN1_ETYPE_TELETEX_STRING:
1271
143k
      case ASN1_ETYPE_PRINTABLE_STRING:
1272
149k
      case ASN1_ETYPE_UNIVERSAL_STRING:
1273
175k
      case ASN1_ETYPE_BMP_STRING:
1274
179k
      case ASN1_ETYPE_UTF8_STRING:
1275
179k
      case ASN1_ETYPE_VISIBLE_STRING:
1276
427k
      case ASN1_ETYPE_BIT_STRING:
1277
427k
        len2 = asn1_get_length_der (der + counter, ider_len, &len3);
1278
427k
        if (len2 < 0)
1279
2.28k
    {
1280
2.28k
      result = ASN1_DER_ERROR;
1281
2.28k
      warn ();
1282
2.28k
      goto cleanup;
1283
2.28k
    }
1284
1285
425k
        DECR_LEN (ider_len, len3 + len2);
1286
1287
425k
        _asn1_set_value (p, der + counter, len3 + len2);
1288
425k
        counter += len3 + len2;
1289
425k
        move = RIGHT;
1290
425k
        break;
1291
5.00M
      case ASN1_ETYPE_SEQUENCE:
1292
5.00M
      case ASN1_ETYPE_SET:
1293
5.00M
        if (move == UP)
1294
2.27M
    {
1295
2.27M
      len2 = p->tmp_ival;
1296
2.27M
      p->tmp_ival = 0;
1297
2.27M
      if (len2 == -1)
1298
26.4k
        {   /* indefinite length method */
1299
26.4k
          DECR_LEN (ider_len, 2);
1300
26.1k
          if ((der[counter]) || der[counter + 1])
1301
225
      {
1302
225
        result = ASN1_DER_ERROR;
1303
225
        warn ();
1304
225
        goto cleanup;
1305
225
      }
1306
25.9k
          counter += 2;
1307
25.9k
        }
1308
2.25M
      else
1309
2.25M
        {   /* definite length method */
1310
2.25M
          if (len2 != counter)
1311
8.92k
      {
1312
8.92k
        result = ASN1_DER_ERROR;
1313
8.92k
        warn ();
1314
8.92k
        goto cleanup;
1315
8.92k
      }
1316
2.25M
        }
1317
2.26M
      move = RIGHT;
1318
2.26M
    }
1319
2.72M
        else
1320
2.72M
    {   /* move==DOWN || move==RIGHT */
1321
2.72M
      len3 = asn1_get_length_der (der + counter, ider_len, &len2);
1322
2.72M
      if (IS_ERR (len3, flags))
1323
2.96k
        {
1324
2.96k
          result = ASN1_DER_ERROR;
1325
2.96k
          warn ();
1326
2.96k
          goto cleanup;
1327
2.96k
        }
1328
1329
2.72M
      DECR_LEN (ider_len, len2);
1330
2.72M
      counter += len2;
1331
1332
2.72M
      if (len3 > 0)
1333
2.41M
        {
1334
2.41M
          p->tmp_ival = counter + len3;
1335
2.41M
          move = DOWN;
1336
2.41M
        }
1337
308k
      else if (len3 == 0)
1338
276k
        {
1339
276k
          p2 = p->down;
1340
1.01M
          while (p2)
1341
742k
      {
1342
742k
        if (type_field (p2->type) != ASN1_ETYPE_TAG)
1343
741k
          {
1344
741k
            p3 = p2->right;
1345
741k
            asn1_delete_structure (&p2);
1346
741k
            p2 = p3;
1347
741k
          }
1348
830
        else
1349
830
          p2 = p2->right;
1350
742k
      }
1351
276k
          move = RIGHT;
1352
276k
        }
1353
31.8k
      else
1354
31.8k
        {   /* indefinite length method */
1355
31.8k
          p->tmp_ival = -1;
1356
31.8k
          move = DOWN;
1357
31.8k
        }
1358
2.72M
    }
1359
4.99M
        break;
1360
4.99M
      case ASN1_ETYPE_SEQUENCE_OF:
1361
6.44M
      case ASN1_ETYPE_SET_OF:
1362
6.44M
        if (move == UP)
1363
5.24M
    {
1364
5.24M
      len2 = p->tmp_ival;
1365
5.24M
      if (len2 == -1)
1366
1.29M
        {   /* indefinite length method */
1367
1.29M
          if (!HAVE_TWO (ider_len)
1368
1.29M
        || ((der[counter]) || der[counter + 1]))
1369
1.28M
      {
1370
1.28M
        result = _asn1_append_sequence_set (p, &tcache);
1371
1.28M
        if (result != 0)
1372
0
          {
1373
0
            warn ();
1374
0
            goto cleanup;
1375
0
          }
1376
1.28M
        p = tcache.tail;
1377
1.28M
        move = RIGHT;
1378
1.28M
        continue;
1379
1.28M
      }
1380
1381
9.52k
          p->tmp_ival = 0;
1382
9.52k
          tcache.tail = NULL; /* finished decoding this structure */
1383
9.52k
          tcache.head = NULL;
1384
9.52k
          DECR_LEN (ider_len, 2);
1385
9.52k
          counter += 2;
1386
9.52k
        }
1387
3.94M
      else
1388
3.94M
        {   /* definite length method */
1389
3.94M
          if (len2 > counter)
1390
2.99M
      {
1391
2.99M
        result = _asn1_append_sequence_set (p, &tcache);
1392
2.99M
        if (result != 0)
1393
0
          {
1394
0
            warn ();
1395
0
            goto cleanup;
1396
0
          }
1397
2.99M
        p = tcache.tail;
1398
2.99M
        move = RIGHT;
1399
2.99M
        continue;
1400
2.99M
      }
1401
1402
949k
          p->tmp_ival = 0;
1403
949k
          tcache.tail = NULL; /* finished decoding this structure */
1404
949k
          tcache.head = NULL;
1405
1406
949k
          if (len2 != counter)
1407
803
      {
1408
803
        result = ASN1_DER_ERROR;
1409
803
        warn ();
1410
803
        goto cleanup;
1411
803
      }
1412
949k
        }
1413
5.24M
    }
1414
1.20M
        else
1415
1.20M
    {   /* move==DOWN || move==RIGHT */
1416
1.20M
      len3 = asn1_get_length_der (der + counter, ider_len, &len2);
1417
1.20M
      if (IS_ERR (len3, flags))
1418
2.06k
        {
1419
2.06k
          result = ASN1_DER_ERROR;
1420
2.06k
          warn ();
1421
2.06k
          goto cleanup;
1422
2.06k
        }
1423
1424
1.19M
      DECR_LEN (ider_len, len2);
1425
1.19M
      counter += len2;
1426
1.19M
      if (len3)
1427
972k
        {
1428
972k
          if (len3 > 0)
1429
962k
      { /* definite length method */
1430
962k
        p->tmp_ival = counter + len3;
1431
962k
      }
1432
10.3k
          else
1433
10.3k
      { /* indefinite length method */
1434
10.3k
        p->tmp_ival = -1;
1435
10.3k
      }
1436
1437
972k
          p2 = p->down;
1438
972k
          if (p2 == NULL)
1439
0
      {
1440
0
        result = ASN1_DER_ERROR;
1441
0
        warn ();
1442
0
        goto cleanup;
1443
0
      }
1444
1445
1.07M
          while ((type_field (p2->type) == ASN1_ETYPE_TAG)
1446
972k
           || (type_field (p2->type) == ASN1_ETYPE_SIZE))
1447
104k
      p2 = p2->right;
1448
972k
          if (p2->right == NULL)
1449
972k
      {
1450
972k
        result = _asn1_append_sequence_set (p, &tcache);
1451
972k
        if (result != 0)
1452
0
          {
1453
0
            warn ();
1454
0
            goto cleanup;
1455
0
          }
1456
972k
      }
1457
972k
          p = p2;
1458
972k
        }
1459
1.19M
    }
1460
2.15M
        move = RIGHT;
1461
2.15M
        break;
1462
3.70M
      case ASN1_ETYPE_ANY:
1463
        /* Check indefinite length method in an EXPLICIT TAG */
1464
1465
3.70M
        if (!(flags & ASN1_DECODE_FLAG_STRICT_DER)
1466
1.34M
      && (p->type & CONST_TAG) && tag_len == 2
1467
8.36k
      && (der[counter - 1] == 0x80))
1468
1.20k
    indefinite = 1;
1469
3.70M
        else
1470
3.70M
    indefinite = 0;
1471
1472
3.70M
        if (asn1_get_tag_der
1473
3.70M
      (der + counter, ider_len, &class, &len2,
1474
3.70M
       &tag) != ASN1_SUCCESS)
1475
200
    {
1476
200
      result = ASN1_DER_ERROR;
1477
200
      warn ();
1478
200
      goto cleanup;
1479
200
    }
1480
1481
3.70M
        DECR_LEN (ider_len, len2);
1482
1483
3.70M
        len4 =
1484
3.70M
    asn1_get_length_der (der + counter + len2, ider_len, &len3);
1485
3.70M
        if (IS_ERR (len4, flags))
1486
2.18k
    {
1487
2.18k
      result = ASN1_DER_ERROR;
1488
2.18k
      warn ();
1489
2.18k
      goto cleanup;
1490
2.18k
    }
1491
3.70M
        if (len4 != -1)  /* definite */
1492
3.69M
    {
1493
3.69M
      len2 += len4;
1494
1495
3.69M
      DECR_LEN (ider_len, len4 + len3);
1496
3.69M
      _asn1_set_value_lv (p, der + counter, len2 + len3);
1497
3.69M
      counter += len2 + len3;
1498
3.69M
    }
1499
9.83k
        else    /* == -1 */
1500
9.83k
    {   /* indefinite length */
1501
9.83k
      ider_len += len2; /* undo DECR_LEN */
1502
1503
9.83k
      if (counter == 0)
1504
0
        {
1505
0
          result = ASN1_DER_ERROR;
1506
0
          warn ();
1507
0
          goto cleanup;
1508
0
        }
1509
1510
9.83k
      result =
1511
9.83k
        _asn1_get_indefinite_length_string (der + counter,
1512
9.83k
              ider_len, &len2);
1513
9.83k
      if (result != ASN1_SUCCESS)
1514
67
        {
1515
67
          warn ();
1516
67
          goto cleanup;
1517
67
        }
1518
1519
9.77k
      DECR_LEN (ider_len, len2);
1520
9.77k
      _asn1_set_value_lv (p, der + counter, len2);
1521
9.77k
      counter += len2;
1522
1523
9.77k
    }
1524
1525
        /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1526
           an indefinite length method. */
1527
3.70M
        if (indefinite)
1528
1.20k
    {
1529
1.20k
      DECR_LEN (ider_len, 2);
1530
1.12k
      if (!der[counter] && !der[counter + 1])
1531
993
        {
1532
993
          counter += 2;
1533
993
        }
1534
135
      else
1535
135
        {
1536
135
          result = ASN1_DER_ERROR;
1537
135
          warn ();
1538
135
          goto cleanup;
1539
135
        }
1540
1.12k
    }
1541
1542
3.70M
        move = RIGHT;
1543
3.70M
        break;
1544
3.70M
      default:
1545
3.70M
        move = (move == UP) ? RIGHT : DOWN;
1546
3.70M
        break;
1547
23.0M
      }
1548
23.0M
  }
1549
1550
19.4M
      if (p)
1551
19.4M
  {
1552
19.4M
    p->end = counter - 1;
1553
19.4M
  }
1554
1555
19.4M
      if (p == node && move != DOWN)
1556
616k
  break;
1557
1558
18.7M
      if (move == DOWN)
1559
4.31M
  {
1560
4.31M
    if (p->down)
1561
4.29M
      p = p->down;
1562
20.7k
    else
1563
20.7k
      move = RIGHT;
1564
4.31M
  }
1565
18.7M
      if ((move == RIGHT) && !(p->type & CONST_SET))
1566
14.4M
  {
1567
14.4M
    if (p->right)
1568
5.39M
      p = p->right;
1569
9.09M
    else
1570
9.09M
      move = UP;
1571
14.4M
  }
1572
18.7M
      if (move == UP)
1573
9.09M
  {
1574
    /* If we are parsing a sequence or set and p is a direct
1575
       child of it, no need to traverse the list back to the leftmost node. */
1576
9.09M
    if (tcache.tail == p)
1577
4.63M
      p = tcache.head;
1578
4.45M
    else
1579
4.45M
      p = _asn1_find_up (p);
1580
9.09M
  }
1581
18.7M
    }
1582
1583
616k
  _asn1_delete_not_used (*element);
1584
1585
616k
  if ((ider_len < 0) ||
1586
616k
      (!(flags & ASN1_DECODE_FLAG_ALLOW_PADDING) && (ider_len != 0)))
1587
1.06k
    {
1588
1.06k
      warn ();
1589
1.06k
      result = ASN1_DER_ERROR;
1590
1.06k
      goto cleanup;
1591
1.06k
    }
1592
1593
615k
  *max_ider_len = total_len - ider_len;
1594
1595
615k
  return ASN1_SUCCESS;
1596
1597
174k
cleanup:
1598
174k
  asn1_delete_structure (element);
1599
174k
  return result;
1600
616k
}
1601
1602
1603
/**
1604
 * asn1_der_decoding:
1605
 * @element: pointer to an ASN1 structure.
1606
 * @ider: vector that contains the DER encoding.
1607
 * @ider_len: number of bytes of *@ider: @ider[0]..@ider[len-1].
1608
 * @errorDescription: null-terminated string contains details when an
1609
 *   error occurred.
1610
 *
1611
 * Fill the structure *@element with values of a DER encoding
1612
 * string. The structure must just be created with function
1613
 * asn1_create_element().
1614
 *
1615
 * Note that the *@element variable is provided as a pointer for
1616
 * historical reasons.
1617
 *
1618
 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1619
 *   if @ELEMENT is %NULL, and %ASN1_TAG_ERROR or
1620
 *   %ASN1_DER_ERROR if the der encoding doesn't match the structure
1621
 *   name (*@ELEMENT deleted).
1622
 **/
1623
int
1624
asn1_der_decoding (asn1_node *element, const void *ider, int ider_len,
1625
       char *errorDescription)
1626
157k
{
1627
157k
  return asn1_der_decoding2 (element, ider, &ider_len, 0, errorDescription);
1628
157k
}
1629
1630
/**
1631
 * asn1_der_decoding_element:
1632
 * @structure: pointer to an ASN1 structure
1633
 * @elementName: name of the element to fill
1634
 * @ider: vector that contains the DER encoding of the whole structure.
1635
 * @len: number of bytes of *der: der[0]..der[len-1]
1636
 * @errorDescription: null-terminated string contains details when an
1637
 *   error occurred.
1638
 *
1639
 * Fill the element named @ELEMENTNAME with values of a DER encoding
1640
 * string.  The structure must just be created with function
1641
 * asn1_create_element().  The DER vector must contain the encoding
1642
 * string of the whole @STRUCTURE.  If an error occurs during the
1643
 * decoding procedure, the *@STRUCTURE is deleted and set equal to
1644
 * %NULL.
1645
 *
1646
 * This function is deprecated and may just be an alias to asn1_der_decoding
1647
 * in future versions. Use asn1_der_decoding() instead.
1648
 *
1649
 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1650
 *   if ELEMENT is %NULL or @elementName == NULL, and
1651
 *   %ASN1_TAG_ERROR or %ASN1_DER_ERROR if the der encoding doesn't
1652
 *   match the structure @structure (*ELEMENT deleted).
1653
 **/
1654
int
1655
asn1_der_decoding_element (asn1_node *structure, const char *elementName,
1656
         const void *ider, int len, char *errorDescription)
1657
0
{
1658
0
  return asn1_der_decoding (structure, ider, len, errorDescription);
1659
0
}
1660
1661
/**
1662
 * asn1_der_decoding_startEnd:
1663
 * @element: pointer to an ASN1 element
1664
 * @ider: vector that contains the DER encoding.
1665
 * @ider_len: number of bytes of *@ider: @ider[0]..@ider[len-1]
1666
 * @name_element: an element of NAME structure.
1667
 * @start: the position of the first byte of NAME_ELEMENT decoding
1668
 *   (@ider[*start])
1669
 * @end: the position of the last byte of NAME_ELEMENT decoding
1670
 *  (@ider[*end])
1671
 *
1672
 * Find the start and end point of an element in a DER encoding
1673
 * string. I mean that if you have a der encoding and you have already
1674
 * used the function asn1_der_decoding() to fill a structure, it may
1675
 * happen that you want to find the piece of string concerning an
1676
 * element of the structure.
1677
 *
1678
 * One example is the sequence "tbsCertificate" inside an X509
1679
 * certificate.
1680
 *
1681
 * Note that since libtasn1 3.7 the @ider and @ider_len parameters
1682
 * can be omitted, if the element is already decoded using asn1_der_decoding().
1683
 *
1684
 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1685
 *   if ELEMENT is %asn1_node EMPTY or @name_element is not a valid
1686
 *   element, %ASN1_TAG_ERROR or %ASN1_DER_ERROR if the der encoding
1687
 *   doesn't match the structure ELEMENT.
1688
 **/
1689
int
1690
asn1_der_decoding_startEnd (asn1_node element, const void *ider, int ider_len,
1691
          const char *name_element, int *start, int *end)
1692
796k
{
1693
796k
  asn1_node node, node_to_find;
1694
796k
  int result = ASN1_DER_ERROR;
1695
1696
796k
  node = element;
1697
1698
796k
  if (node == NULL)
1699
0
    return ASN1_ELEMENT_NOT_FOUND;
1700
1701
796k
  node_to_find = asn1_find_node (node, name_element);
1702
1703
796k
  if (node_to_find == NULL)
1704
969
    return ASN1_ELEMENT_NOT_FOUND;
1705
1706
795k
  *start = node_to_find->start;
1707
795k
  *end = node_to_find->end;
1708
1709
795k
  if (*start == 0 && *end == 0)
1710
0
    {
1711
0
      if (ider == NULL || ider_len == 0)
1712
0
  return ASN1_GENERIC_ERROR;
1713
1714
      /* it seems asn1_der_decoding() wasn't called before. Do it now */
1715
0
      result = asn1_der_decoding (&node, ider, ider_len, NULL);
1716
0
      if (result != ASN1_SUCCESS)
1717
0
  {
1718
0
    warn ();
1719
0
    return result;
1720
0
  }
1721
1722
0
      node_to_find = asn1_find_node (node, name_element);
1723
0
      if (node_to_find == NULL)
1724
0
  return ASN1_ELEMENT_NOT_FOUND;
1725
1726
0
      *start = node_to_find->start;
1727
0
      *end = node_to_find->end;
1728
0
    }
1729
1730
795k
  if (*end < *start)
1731
0
    return ASN1_GENERIC_ERROR;
1732
1733
795k
  return ASN1_SUCCESS;
1734
795k
}
1735
1736
/**
1737
 * asn1_expand_any_defined_by:
1738
 * @definitions: ASN1 definitions
1739
 * @element: pointer to an ASN1 structure
1740
 *
1741
 * Expands every "ANY DEFINED BY" element of a structure created from
1742
 * a DER decoding process (asn1_der_decoding function). The element
1743
 * ANY must be defined by an OBJECT IDENTIFIER. The type used to
1744
 * expand the element ANY is the first one following the definition of
1745
 * the actual value of the OBJECT IDENTIFIER.
1746
 *
1747
 * Returns: %ASN1_SUCCESS if Substitution OK, %ASN1_ERROR_TYPE_ANY if
1748
 *   some "ANY DEFINED BY" element couldn't be expanded due to a
1749
 *   problem in OBJECT_ID -> TYPE association, or other error codes
1750
 *   depending on DER decoding.
1751
 **/
1752
int
1753
asn1_expand_any_defined_by (asn1_node_const definitions, asn1_node *element)
1754
0
{
1755
0
  char name[2 * ASN1_MAX_NAME_SIZE + 2], value[ASN1_MAX_NAME_SIZE];
1756
0
  int retCode = ASN1_SUCCESS, result;
1757
0
  int len, len2, len3;
1758
0
  asn1_node_const p2;
1759
0
  asn1_node p, p3, aux = NULL;
1760
0
  char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
1761
0
  const char *definitionsName;
1762
1763
0
  if ((definitions == NULL) || (*element == NULL))
1764
0
    return ASN1_ELEMENT_NOT_FOUND;
1765
1766
0
  definitionsName = definitions->name;
1767
1768
0
  p = *element;
1769
0
  while (p)
1770
0
    {
1771
1772
0
      switch (type_field (p->type))
1773
0
  {
1774
0
  case ASN1_ETYPE_ANY:
1775
0
    if ((p->type & CONST_DEFINED_BY) && (p->value))
1776
0
      {
1777
        /* search the "DEF_BY" element */
1778
0
        p2 = p->down;
1779
0
        while ((p2) && (type_field (p2->type) != ASN1_ETYPE_CONSTANT))
1780
0
    p2 = p2->right;
1781
1782
0
        if (!p2)
1783
0
    {
1784
0
      retCode = ASN1_ERROR_TYPE_ANY;
1785
0
      break;
1786
0
    }
1787
1788
0
        p3 = _asn1_find_up (p);
1789
1790
0
        if (!p3)
1791
0
    {
1792
0
      retCode = ASN1_ERROR_TYPE_ANY;
1793
0
      break;
1794
0
    }
1795
1796
0
        p3 = p3->down;
1797
0
        while (p3)
1798
0
    {
1799
0
      if (!(strcmp (p3->name, p2->name)))
1800
0
        break;
1801
0
      p3 = p3->right;
1802
0
    }
1803
1804
0
        if ((!p3) || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID) ||
1805
0
      (p3->value == NULL))
1806
0
    {
1807
1808
0
      p3 = _asn1_find_up (p);
1809
0
      p3 = _asn1_find_up (p3);
1810
1811
0
      if (!p3)
1812
0
        {
1813
0
          retCode = ASN1_ERROR_TYPE_ANY;
1814
0
          break;
1815
0
        }
1816
1817
0
      p3 = p3->down;
1818
1819
0
      while (p3)
1820
0
        {
1821
0
          if (!(strcmp (p3->name, p2->name)))
1822
0
      break;
1823
0
          p3 = p3->right;
1824
0
        }
1825
1826
0
      if ((!p3) || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID)
1827
0
          || (p3->value == NULL))
1828
0
        {
1829
0
          retCode = ASN1_ERROR_TYPE_ANY;
1830
0
          break;
1831
0
        }
1832
0
    }
1833
1834
        /* search the OBJECT_ID into definitions */
1835
0
        p2 = definitions->down;
1836
0
        while (p2)
1837
0
    {
1838
0
      if ((type_field (p2->type) == ASN1_ETYPE_OBJECT_ID) &&
1839
0
          (p2->type & CONST_ASSIGN))
1840
0
        {
1841
0
          snprintf (name, sizeof (name), "%s.%s", definitionsName,
1842
0
        p2->name);
1843
1844
0
          len = ASN1_MAX_NAME_SIZE;
1845
0
          result =
1846
0
      asn1_read_value (definitions, name, value, &len);
1847
1848
0
          if ((result == ASN1_SUCCESS)
1849
0
        && (!_asn1_strcmp (p3->value, value)))
1850
0
      {
1851
0
        p2 = p2->right; /* pointer to the structure to
1852
               use for expansion */
1853
0
        while ((p2) && (p2->type & CONST_ASSIGN))
1854
0
          p2 = p2->right;
1855
1856
0
        if (p2)
1857
0
          {
1858
0
            snprintf (name, sizeof (name), "%s.%s",
1859
0
          definitionsName, p2->name);
1860
1861
0
            result =
1862
0
        asn1_create_element (definitions, name, &aux);
1863
0
            if (result == ASN1_SUCCESS)
1864
0
        {
1865
0
          _asn1_cpy_name (aux, p);
1866
0
          len2 =
1867
0
            asn1_get_length_der (p->value,
1868
0
               p->value_len, &len3);
1869
0
          if (len2 < 0)
1870
0
            return ASN1_DER_ERROR;
1871
1872
0
          result =
1873
0
            asn1_der_decoding (&aux, p->value + len3,
1874
0
                   len2,
1875
0
                   errorDescription);
1876
0
          if (result == ASN1_SUCCESS)
1877
0
            {
1878
1879
0
              _asn1_set_right (aux, p->right);
1880
0
              _asn1_set_right (p, aux);
1881
1882
0
              result = asn1_delete_structure (&p);
1883
0
              if (result == ASN1_SUCCESS)
1884
0
          {
1885
0
            p = aux;
1886
0
            aux = NULL;
1887
0
            break;
1888
0
          }
1889
0
              else
1890
0
          { /* error with asn1_delete_structure */
1891
0
            asn1_delete_structure (&aux);
1892
0
            retCode = result;
1893
0
            break;
1894
0
          }
1895
0
            }
1896
0
          else
1897
0
            { /* error with asn1_der_decoding */
1898
0
              retCode = result;
1899
0
              break;
1900
0
            }
1901
0
        }
1902
0
            else
1903
0
        { /* error with asn1_create_element */
1904
0
          retCode = result;
1905
0
          break;
1906
0
        }
1907
0
          }
1908
0
        else
1909
0
          { /* error with the pointer to the structure to expand */
1910
0
            retCode = ASN1_ERROR_TYPE_ANY;
1911
0
            break;
1912
0
          }
1913
0
      }
1914
0
        }
1915
0
      p2 = p2->right;
1916
0
    }    /* end while */
1917
1918
0
        if (!p2)
1919
0
    {
1920
0
      retCode = ASN1_ERROR_TYPE_ANY;
1921
0
      break;
1922
0
    }
1923
1924
0
      }
1925
0
    break;
1926
0
  default:
1927
0
    break;
1928
0
  }
1929
1930
1931
0
      if (p->down)
1932
0
  {
1933
0
    p = p->down;
1934
0
  }
1935
0
      else if (p == *element)
1936
0
  {
1937
0
    p = NULL;
1938
0
    break;
1939
0
  }
1940
0
      else if (p->right)
1941
0
  p = p->right;
1942
0
      else
1943
0
  {
1944
0
    while (1)
1945
0
      {
1946
0
        p = _asn1_find_up (p);
1947
0
        if (p == *element)
1948
0
    {
1949
0
      p = NULL;
1950
0
      break;
1951
0
    }
1952
0
        if (p->right)
1953
0
    {
1954
0
      p = p->right;
1955
0
      break;
1956
0
    }
1957
0
      }
1958
0
  }
1959
0
    }
1960
1961
0
  return retCode;
1962
0
}
1963
1964
/**
1965
 * asn1_expand_octet_string:
1966
 * @definitions: ASN1 definitions
1967
 * @element: pointer to an ASN1 structure
1968
 * @octetName: name of the OCTET STRING field to expand.
1969
 * @objectName: name of the OBJECT IDENTIFIER field to use to define
1970
 *    the type for expansion.
1971
 *
1972
 * Expands an "OCTET STRING" element of a structure created from a DER
1973
 * decoding process (the asn1_der_decoding() function).  The type used
1974
 * for expansion is the first one following the definition of the
1975
 * actual value of the OBJECT IDENTIFIER indicated by OBJECTNAME.
1976
 *
1977
 * Returns: %ASN1_SUCCESS if substitution OK, %ASN1_ELEMENT_NOT_FOUND
1978
 *   if @objectName or @octetName are not correct,
1979
 *   %ASN1_VALUE_NOT_VALID if it wasn't possible to find the type to
1980
 *   use for expansion, or other errors depending on DER decoding.
1981
 **/
1982
int
1983
asn1_expand_octet_string (asn1_node_const definitions, asn1_node *element,
1984
        const char *octetName, const char *objectName)
1985
0
{
1986
0
  char name[2 * ASN1_MAX_NAME_SIZE + 1], value[ASN1_MAX_NAME_SIZE];
1987
0
  int retCode = ASN1_SUCCESS, result;
1988
0
  int len, len2, len3;
1989
0
  asn1_node_const p2;
1990
0
  asn1_node aux = NULL;
1991
0
  asn1_node octetNode = NULL, objectNode = NULL;
1992
0
  char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
1993
1994
0
  if ((definitions == NULL) || (*element == NULL))
1995
0
    return ASN1_ELEMENT_NOT_FOUND;
1996
1997
0
  octetNode = asn1_find_node (*element, octetName);
1998
0
  if (octetNode == NULL)
1999
0
    return ASN1_ELEMENT_NOT_FOUND;
2000
0
  if (type_field (octetNode->type) != ASN1_ETYPE_OCTET_STRING)
2001
0
    return ASN1_ELEMENT_NOT_FOUND;
2002
0
  if (octetNode->value == NULL)
2003
0
    return ASN1_VALUE_NOT_FOUND;
2004
2005
0
  objectNode = asn1_find_node (*element, objectName);
2006
0
  if (objectNode == NULL)
2007
0
    return ASN1_ELEMENT_NOT_FOUND;
2008
2009
0
  if (type_field (objectNode->type) != ASN1_ETYPE_OBJECT_ID)
2010
0
    return ASN1_ELEMENT_NOT_FOUND;
2011
2012
0
  if (objectNode->value == NULL)
2013
0
    return ASN1_VALUE_NOT_FOUND;
2014
2015
2016
  /* search the OBJECT_ID into definitions */
2017
0
  p2 = definitions->down;
2018
0
  while (p2)
2019
0
    {
2020
0
      if ((type_field (p2->type) == ASN1_ETYPE_OBJECT_ID) &&
2021
0
    (p2->type & CONST_ASSIGN))
2022
0
  {
2023
0
    strcpy (name, definitions->name);
2024
0
    strcat (name, ".");
2025
0
    strcat (name, p2->name);
2026
2027
0
    len = sizeof (value);
2028
0
    result = asn1_read_value (definitions, name, value, &len);
2029
2030
0
    if ((result == ASN1_SUCCESS)
2031
0
        && (!_asn1_strcmp (objectNode->value, value)))
2032
0
      {
2033
2034
0
        p2 = p2->right; /* pointer to the structure to
2035
           use for expansion */
2036
0
        while ((p2) && (p2->type & CONST_ASSIGN))
2037
0
    p2 = p2->right;
2038
2039
0
        if (p2)
2040
0
    {
2041
0
      strcpy (name, definitions->name);
2042
0
      strcat (name, ".");
2043
0
      strcat (name, p2->name);
2044
2045
0
      result = asn1_create_element (definitions, name, &aux);
2046
0
      if (result == ASN1_SUCCESS)
2047
0
        {
2048
0
          _asn1_cpy_name (aux, octetNode);
2049
0
          len2 =
2050
0
      asn1_get_length_der (octetNode->value,
2051
0
               octetNode->value_len, &len3);
2052
0
          if (len2 < 0)
2053
0
      return ASN1_DER_ERROR;
2054
2055
0
          result =
2056
0
      asn1_der_decoding (&aux, octetNode->value + len3,
2057
0
             len2, errorDescription);
2058
0
          if (result == ASN1_SUCCESS)
2059
0
      {
2060
2061
0
        _asn1_set_right (aux, octetNode->right);
2062
0
        _asn1_set_right (octetNode, aux);
2063
2064
0
        result = asn1_delete_structure (&octetNode);
2065
0
        if (result == ASN1_SUCCESS)
2066
0
          {
2067
0
            aux = NULL;
2068
0
            break;
2069
0
          }
2070
0
        else
2071
0
          { /* error with asn1_delete_structure */
2072
0
            asn1_delete_structure (&aux);
2073
0
            retCode = result;
2074
0
            break;
2075
0
          }
2076
0
      }
2077
0
          else
2078
0
      { /* error with asn1_der_decoding */
2079
0
        retCode = result;
2080
0
        break;
2081
0
      }
2082
0
        }
2083
0
      else
2084
0
        {   /* error with asn1_create_element */
2085
0
          retCode = result;
2086
0
          break;
2087
0
        }
2088
0
    }
2089
0
        else
2090
0
    {   /* error with the pointer to the structure to expand */
2091
0
      retCode = ASN1_VALUE_NOT_VALID;
2092
0
      break;
2093
0
    }
2094
0
      }
2095
0
  }
2096
2097
0
      p2 = p2->right;
2098
2099
0
    }
2100
2101
0
  if (!p2)
2102
0
    retCode = ASN1_VALUE_NOT_VALID;
2103
2104
0
  return retCode;
2105
0
}
2106
2107
/*-
2108
 * _asn1_decode_simple_der:
2109
 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2110
 * @der: the encoded string
2111
 * @_der_len: the bytes of the encoded string
2112
 * @str: a pointer to the data
2113
 * @str_len: the length of the data
2114
 * @dflags: DECODE_FLAG_*
2115
 *
2116
 * Decodes a simple DER encoded type (e.g. a string, which is not constructed).
2117
 * The output is a pointer inside the @der.
2118
 *
2119
 * Returns: %ASN1_SUCCESS if successful or an error value.
2120
 -*/
2121
static int
2122
_asn1_decode_simple_der (unsigned int etype, const unsigned char *der,
2123
       unsigned int _der_len, const unsigned char **str,
2124
       unsigned int *str_len, unsigned dflags)
2125
143k
{
2126
143k
  int tag_len, len_len;
2127
143k
  const unsigned char *p;
2128
143k
  int der_len = _der_len;
2129
143k
  unsigned char class;
2130
143k
  unsigned long tag;
2131
143k
  long ret;
2132
2133
143k
  if (der == NULL || der_len == 0)
2134
0
    return ASN1_VALUE_NOT_VALID;
2135
2136
143k
  if (ETYPE_OK (etype) == 0 || ETYPE_IS_STRING (etype) == 0)
2137
0
    return ASN1_VALUE_NOT_VALID;
2138
2139
  /* doesn't handle constructed classes */
2140
143k
  class = ETYPE_CLASS (etype);
2141
143k
  if (class != ASN1_CLASS_UNIVERSAL)
2142
0
    return ASN1_VALUE_NOT_VALID;
2143
2144
143k
  p = der;
2145
2146
143k
  if (dflags & DECODE_FLAG_HAVE_TAG)
2147
143k
    {
2148
143k
      ret = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag);
2149
143k
      if (ret != ASN1_SUCCESS)
2150
0
  return ret;
2151
2152
143k
      if (class != ETYPE_CLASS (etype) || tag != ETYPE_TAG (etype))
2153
6.64k
  {
2154
6.64k
    warn ();
2155
6.64k
    return ASN1_DER_ERROR;
2156
6.64k
  }
2157
2158
137k
      p += tag_len;
2159
137k
      der_len -= tag_len;
2160
137k
      if (der_len <= 0)
2161
22
  return ASN1_DER_ERROR;
2162
137k
    }
2163
2164
137k
  ret = asn1_get_length_der (p, der_len, &len_len);
2165
137k
  if (ret < 0)
2166
349
    return ASN1_DER_ERROR;
2167
2168
136k
  p += len_len;
2169
136k
  der_len -= len_len;
2170
136k
  if (der_len <= 0)
2171
77
    return ASN1_DER_ERROR;
2172
2173
136k
  *str_len = ret;
2174
136k
  *str = p;
2175
2176
136k
  return ASN1_SUCCESS;
2177
136k
}
2178
2179
/**
2180
 * asn1_decode_simple_der:
2181
 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2182
 * @der: the encoded string
2183
 * @_der_len: the bytes of the encoded string
2184
 * @str: a pointer to the data
2185
 * @str_len: the length of the data
2186
 *
2187
 * Decodes a simple DER encoded type (e.g. a string, which is not constructed).
2188
 * The output is a pointer inside the @der.
2189
 *
2190
 * Returns: %ASN1_SUCCESS if successful or an error value.
2191
 **/
2192
int
2193
asn1_decode_simple_der (unsigned int etype, const unsigned char *der,
2194
      unsigned int _der_len, const unsigned char **str,
2195
      unsigned int *str_len)
2196
15.7k
{
2197
15.7k
  return _asn1_decode_simple_der (etype, der, _der_len, str, str_len,
2198
15.7k
          DECODE_FLAG_HAVE_TAG);
2199
15.7k
}
2200
2201
static int
2202
append (uint8_t **dst, unsigned *dst_size, const unsigned char *src,
2203
  unsigned src_size)
2204
216k
{
2205
216k
  if (src_size == 0)
2206
28.2k
    return ASN1_SUCCESS;
2207
2208
188k
  *dst = _asn1_realloc (*dst, *dst_size + src_size);
2209
188k
  if (*dst == NULL)
2210
0
    return ASN1_MEM_ALLOC_ERROR;
2211
188k
  memcpy (*dst + *dst_size, src, src_size);
2212
188k
  *dst_size += src_size;
2213
188k
  return ASN1_SUCCESS;
2214
188k
}
2215
2216
/*-
2217
 * _asn1_decode_simple_ber:
2218
 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2219
 * @der: the encoded string
2220
 * @_der_len: the bytes of the encoded string
2221
 * @str: a pointer to the data
2222
 * @str_len: the length of the data
2223
 * @ber_len: the total length occupied by BER (may be %NULL)
2224
 * @have_tag: whether a DER tag is included
2225
 *
2226
 * Decodes a BER encoded type. The output is an allocated value
2227
 * of the data. This decodes BER STRINGS only. Other types are
2228
 * decoded as DER.
2229
 *
2230
 * Returns: %ASN1_SUCCESS if successful or an error value.
2231
 -*/
2232
static int
2233
_asn1_decode_simple_ber (unsigned int etype, const unsigned char *der,
2234
       unsigned int _der_len, unsigned char **str,
2235
       unsigned int *str_len, unsigned int *ber_len,
2236
       unsigned dflags)
2237
141k
{
2238
141k
  int tag_len, len_len;
2239
141k
  const unsigned char *p;
2240
141k
  int der_len = _der_len;
2241
141k
  uint8_t *total = NULL;
2242
141k
  unsigned total_size = 0;
2243
141k
  unsigned char class;
2244
141k
  unsigned long tag;
2245
141k
  unsigned char *out = NULL;
2246
141k
  const unsigned char *cout = NULL;
2247
141k
  unsigned out_len;
2248
141k
  long result;
2249
2250
141k
  if (ber_len)
2251
94.7k
    *ber_len = 0;
2252
2253
141k
  if (der == NULL || der_len == 0)
2254
35
    {
2255
35
      warn ();
2256
35
      return ASN1_VALUE_NOT_VALID;
2257
35
    }
2258
2259
141k
  if (ETYPE_OK (etype) == 0)
2260
0
    {
2261
0
      warn ();
2262
0
      return ASN1_VALUE_NOT_VALID;
2263
0
    }
2264
2265
  /* doesn't handle constructed + definite classes */
2266
141k
  class = ETYPE_CLASS (etype);
2267
141k
  if (class != ASN1_CLASS_UNIVERSAL)
2268
0
    {
2269
0
      warn ();
2270
0
      return ASN1_VALUE_NOT_VALID;
2271
0
    }
2272
2273
141k
  p = der;
2274
2275
141k
  if (dflags & DECODE_FLAG_HAVE_TAG)
2276
137k
    {
2277
137k
      result = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag);
2278
137k
      if (result != ASN1_SUCCESS)
2279
143
  {
2280
143
    warn ();
2281
143
    return result;
2282
143
  }
2283
2284
137k
      if (tag != ETYPE_TAG (etype))
2285
1.03k
  {
2286
1.03k
    warn ();
2287
1.03k
    return ASN1_DER_ERROR;
2288
1.03k
  }
2289
2290
136k
      p += tag_len;
2291
2292
136k
      DECR_LEN (der_len, tag_len);
2293
2294
136k
      if (ber_len)
2295
90.0k
  *ber_len += tag_len;
2296
136k
    }
2297
2298
  /* indefinite constructed */
2299
140k
  if ((((dflags & DECODE_FLAG_CONSTRUCTED) || class == ASN1_CLASS_STRUCTURED)
2300
12.1k
       && ETYPE_IS_STRING (etype)) && !(dflags & DECODE_FLAG_LEVEL3))
2301
12.1k
    {
2302
12.1k
      if (der_len == 0)
2303
8
  {
2304
8
    warn ();
2305
8
    result = ASN1_DER_ERROR;
2306
8
    goto cleanup;
2307
8
  }
2308
2309
12.1k
      if (der_len > 0 && p[0] == 0x80)  /* indefinite */
2310
2.41k
  {
2311
2.41k
    len_len = 1;
2312
2.41k
    DECR_LEN (der_len, len_len);
2313
2.41k
    p += len_len;
2314
2315
2.41k
    if (ber_len)
2316
1.61k
      *ber_len += len_len;
2317
2318
    /* decode the available octet strings */
2319
2.41k
    do
2320
84.3k
      {
2321
84.3k
        unsigned tmp_len;
2322
84.3k
        unsigned flags = DECODE_FLAG_HAVE_TAG;
2323
2324
84.3k
        if (dflags & DECODE_FLAG_LEVEL1)
2325
8.24k
    flags |= DECODE_FLAG_LEVEL2;
2326
76.1k
        else if (dflags & DECODE_FLAG_LEVEL2)
2327
1.22k
    flags |= DECODE_FLAG_LEVEL3;
2328
74.8k
        else
2329
74.8k
    flags |= DECODE_FLAG_LEVEL1;
2330
2331
84.3k
        result =
2332
84.3k
    _asn1_decode_simple_ber (etype, p, der_len, &out, &out_len,
2333
84.3k
           &tmp_len, flags);
2334
84.3k
        if (result != ASN1_SUCCESS)
2335
1.08k
    {
2336
1.08k
      warn ();
2337
1.08k
      goto cleanup;
2338
1.08k
    }
2339
2340
83.2k
        p += tmp_len;
2341
83.2k
        DECR_LEN (der_len, tmp_len);
2342
2343
83.2k
        if (ber_len)
2344
70.6k
    *ber_len += tmp_len;
2345
2346
83.2k
        DECR_LEN (der_len, 2); /* we need the EOC */
2347
2348
83.2k
        result = append (&total, &total_size, out, out_len);
2349
83.2k
        if (result != ASN1_SUCCESS)
2350
0
    {
2351
0
      warn ();
2352
0
      goto cleanup;
2353
0
    }
2354
2355
83.2k
        free (out);
2356
83.2k
        out = NULL;
2357
2358
83.2k
        if (p[0] == 0 && p[1] == 0)  /* EOC */
2359
1.19k
    {
2360
1.19k
      if (ber_len)
2361
946
        *ber_len += 2;
2362
1.19k
      break;
2363
1.19k
    }
2364
2365
        /* no EOC */
2366
82.0k
        der_len += 2;
2367
2368
82.0k
        if (der_len == 2)
2369
73
    {
2370
73
      warn ();
2371
73
      result = ASN1_DER_ERROR;
2372
73
      goto cleanup;
2373
73
    }
2374
82.0k
      }
2375
81.9k
    while (1);
2376
2.41k
  }
2377
9.69k
      else      /* constructed */
2378
9.69k
  {
2379
9.69k
    long const_len;
2380
2381
9.69k
    result = asn1_get_length_ber (p, der_len, &len_len);
2382
9.69k
    if (result < 0)
2383
357
      {
2384
357
        warn ();
2385
357
        result = ASN1_DER_ERROR;
2386
357
        goto cleanup;
2387
357
      }
2388
2389
9.34k
    DECR_LEN (der_len, len_len);
2390
9.34k
    p += len_len;
2391
2392
9.34k
    const_len = result;
2393
2394
9.34k
    if (ber_len)
2395
9.09k
      *ber_len += len_len;
2396
2397
    /* decode the available octet strings */
2398
14.8k
    while (const_len > 0)
2399
6.47k
      {
2400
6.47k
        unsigned tmp_len;
2401
6.47k
        unsigned flags = DECODE_FLAG_HAVE_TAG;
2402
2403
6.47k
        if (dflags & DECODE_FLAG_LEVEL1)
2404
2.82k
    flags |= DECODE_FLAG_LEVEL2;
2405
3.65k
        else if (dflags & DECODE_FLAG_LEVEL2)
2406
937
    flags |= DECODE_FLAG_LEVEL3;
2407
2.71k
        else
2408
2.71k
    flags |= DECODE_FLAG_LEVEL1;
2409
2410
6.47k
        result =
2411
6.47k
    _asn1_decode_simple_ber (etype, p, der_len, &out, &out_len,
2412
6.47k
           &tmp_len, flags);
2413
6.47k
        if (result != ASN1_SUCCESS)
2414
854
    {
2415
854
      warn ();
2416
854
      goto cleanup;
2417
854
    }
2418
2419
5.62k
        p += tmp_len;
2420
5.62k
        DECR_LEN (der_len, tmp_len);
2421
5.62k
        DECR_LEN (const_len, tmp_len);
2422
2423
5.47k
        if (ber_len)
2424
4.48k
    *ber_len += tmp_len;
2425
2426
5.47k
        result = append (&total, &total_size, out, out_len);
2427
5.47k
        if (result != ASN1_SUCCESS)
2428
0
    {
2429
0
      warn ();
2430
0
      goto cleanup;
2431
0
    }
2432
2433
5.47k
        free (out);
2434
5.47k
        out = NULL;
2435
5.47k
      }
2436
9.34k
  }
2437
12.1k
    }
2438
128k
  else if (class == ETYPE_CLASS (etype))
2439
128k
    {
2440
128k
      if (ber_len)
2441
82.8k
  {
2442
82.8k
    result = asn1_get_length_der (p, der_len, &len_len);
2443
82.8k
    if (result < 0)
2444
322
      {
2445
322
        warn ();
2446
322
        result = ASN1_DER_ERROR;
2447
322
        goto cleanup;
2448
322
      }
2449
82.5k
    *ber_len += result + len_len;
2450
82.5k
  }
2451
2452
      /* non-string values are decoded as DER */
2453
128k
      result =
2454
128k
  _asn1_decode_simple_der (etype, der, _der_len, &cout, &out_len,
2455
128k
         dflags);
2456
128k
      if (result != ASN1_SUCCESS)
2457
263
  {
2458
263
    warn ();
2459
263
    goto cleanup;
2460
263
  }
2461
2462
127k
      result = append (&total, &total_size, cout, out_len);
2463
127k
      if (result != ASN1_SUCCESS)
2464
0
  {
2465
0
    warn ();
2466
0
    goto cleanup;
2467
0
  }
2468
127k
    }
2469
133
  else
2470
133
    {
2471
133
      warn ();
2472
133
      result = ASN1_DER_ERROR;
2473
133
      goto cleanup;
2474
133
    }
2475
2476
137k
  *str = total;
2477
137k
  *str_len = total_size;
2478
2479
137k
  return ASN1_SUCCESS;
2480
3.30k
cleanup:
2481
3.30k
  free (out);
2482
3.30k
  free (total);
2483
3.30k
  return result;
2484
140k
}
2485
2486
/**
2487
 * asn1_decode_simple_ber:
2488
 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2489
 * @der: the encoded string
2490
 * @_der_len: the bytes of the encoded string
2491
 * @str: a pointer to the data
2492
 * @str_len: the length of the data
2493
 * @ber_len: the total length occupied by BER (may be %NULL)
2494
 *
2495
 * Decodes a BER encoded type. The output is an allocated value
2496
 * of the data. This decodes BER STRINGS only. Other types are
2497
 * decoded as DER.
2498
 *
2499
 * Returns: %ASN1_SUCCESS if successful or an error value.
2500
 **/
2501
int
2502
asn1_decode_simple_ber (unsigned int etype, const unsigned char *der,
2503
      unsigned int _der_len, unsigned char **str,
2504
      unsigned int *str_len, unsigned int *ber_len)
2505
47.1k
{
2506
47.1k
  return _asn1_decode_simple_ber (etype, der, _der_len, str, str_len, ber_len,
2507
47.1k
          DECODE_FLAG_HAVE_TAG);
2508
47.1k
}