Coverage Report

Created: 2025-11-16 06:14

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
67.4k
#define IS_ERR(len, flags) (len < -1 || ((flags & ASN1_DECODE_FLAG_STRICT_DER) && len < 0))
44
45
108k
#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
12.9k
#define DECODE_FLAG_HAVE_TAG 1
59
7.99k
#define DECODE_FLAG_CONSTRUCTED (1<<1)
60
6.81k
#define DECODE_FLAG_LEVEL1 (1<<2)
61
4.38k
#define DECODE_FLAG_LEVEL2 (1<<3)
62
4.54k
#define DECODE_FLAG_LEVEL3 (1<<4)
63
64
318k
#define DECR_LEN(l, s) do { \
65
318k
    l -= s; \
66
318k
    if (l < 0) { \
67
400
      warn(); \
68
400
      result = ASN1_DER_ERROR; \
69
400
      goto cleanup; \
70
400
    } \
71
318k
  } 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
0
{
91
92
0
  Estrcpy (ErrorDescription, ":: tag error near element '");
93
0
  _asn1_hierarchical_name (node, ErrorDescription + strlen (ErrorDescription),
94
0
         ASN1_MAX_ERROR_DESCRIPTION_SIZE - 40);
95
0
  Estrcat (ErrorDescription, "'");
96
97
0
}
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
103k
{
114
103k
  unsigned int ans;
115
103k
  int k, punt, sum;
116
117
103k
  *len = 0;
118
103k
  if (der_len <= 0)
119
334
    return 0;
120
121
103k
  if (!(der[0] & 128))
122
85.6k
    {
123
      /* short form */
124
85.6k
      *len = 1;
125
85.6k
      ans = der[0];
126
85.6k
    }
127
17.5k
  else
128
17.5k
    {
129
      /* Long form */
130
17.5k
      k = der[0] & 0x7F;
131
17.5k
      punt = 1;
132
17.5k
      if (k)
133
4.47k
  {     /* definite length method */
134
4.47k
    ans = 0;
135
27.0k
    while (punt <= k && punt < der_len)
136
22.6k
      {
137
22.6k
        if (INT_MULTIPLY_OVERFLOW (ans, 256))
138
64
    return -2;
139
22.6k
        ans *= 256;
140
141
22.6k
        if (INT_ADD_OVERFLOW (ans, ((unsigned) der[punt])))
142
0
    return -2;
143
22.6k
        ans += der[punt];
144
22.6k
        punt++;
145
22.6k
      }
146
4.47k
  }
147
13.0k
      else
148
13.0k
  {     /* indefinite length method */
149
13.0k
    *len = punt;
150
13.0k
    return -1;
151
13.0k
  }
152
153
4.41k
      *len = punt;
154
4.41k
    }
155
156
90.0k
  sum = ans;
157
90.0k
  if (ans >= INT_MAX || INT_ADD_OVERFLOW (sum, (*len)))
158
82
    return -2;
159
90.0k
  sum += *len;
160
161
90.0k
  if (sum > der_len)
162
1.27k
    return -4;
163
164
88.7k
  return ans;
165
90.0k
}
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
123k
{
183
123k
  unsigned int ris;
184
123k
  int punt;
185
186
123k
  if (der == NULL || der_len < 2 || len == NULL)
187
238
    return ASN1_DER_ERROR;
188
189
123k
  *cls = der[0] & 0xE0;
190
123k
  if ((der[0] & 0x1F) != 0x1F)
191
119k
    {
192
      /* short form */
193
119k
      *len = 1;
194
119k
      ris = der[0] & 0x1F;
195
119k
    }
196
3.37k
  else
197
3.37k
    {
198
      /* Long form */
199
3.37k
      punt = 1;
200
3.37k
      ris = 0;
201
574k
      while (punt < der_len && der[punt] & 128)
202
570k
  {
203
204
570k
    if (INT_MULTIPLY_OVERFLOW (ris, 128))
205
22
      return ASN1_DER_ERROR;
206
570k
    ris *= 128;
207
208
570k
    if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
209
0
      return ASN1_DER_ERROR;
210
570k
    ris += (der[punt] & 0x7F);
211
570k
    punt++;
212
570k
  }
213
214
3.34k
      if (punt >= der_len)
215
102
  return ASN1_DER_ERROR;
216
217
3.24k
      if (INT_MULTIPLY_OVERFLOW (ris, 128))
218
108
  return ASN1_DER_ERROR;
219
3.13k
      ris *= 128;
220
221
3.13k
      if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
222
0
  return ASN1_DER_ERROR;
223
3.13k
      ris += (der[punt] & 0x7F);
224
3.13k
      punt++;
225
226
3.13k
      *len = punt;
227
3.13k
    }
228
229
122k
  if (tag)
230
122k
    *tag = ris;
231
122k
  return ASN1_SUCCESS;
232
123k
}
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
6.56k
{
252
6.56k
  int ret;
253
6.56k
  long err;
254
255
6.56k
  ret = asn1_get_length_der (ber, ber_len, len);
256
257
6.56k
  if (ret == -1 && ber_len > 1)
258
325
    {       /* indefinite length method */
259
325
      err = _asn1_get_indefinite_length_string (ber + 1, ber_len - 1, &ret);
260
325
      if (err != ASN1_SUCCESS)
261
278
  return -3;
262
325
    }
263
264
6.28k
  return ret;
265
6.56k
}
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
0
{
287
0
  int len_len = 0;
288
289
0
  if (der_len <= 0)
290
0
    return ASN1_GENERIC_ERROR;
291
292
0
  *str_len = asn1_get_length_der (der, der_len, &len_len);
293
294
0
  if (*str_len < 0)
295
0
    return ASN1_DER_ERROR;
296
297
0
  *ret_len = *str_len + len_len;
298
0
  if (str_size >= *str_len)
299
0
    {
300
0
      if (*str_len > 0 && str != NULL)
301
0
  memcpy (str, der + len_len, *str_len);
302
0
    }
303
0
  else
304
0
    {
305
0
      return ASN1_MEM_ERROR;
306
0
    }
307
308
0
  return ASN1_SUCCESS;
309
0
}
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
161
{
332
161
  int len_len, str_len;
333
161
  unsigned i;
334
161
  unsigned sign_count = 0;
335
161
  unsigned dot_count = 0;
336
161
  const unsigned char *p;
337
338
161
  if (der_len <= 0 || str == NULL)
339
2
    return ASN1_DER_ERROR;
340
341
159
  str_len = asn1_get_length_der (der, der_len, &len_len);
342
159
  if (str_len <= 0 || str_size < str_len)
343
118
    return ASN1_DER_ERROR;
344
345
  /* perform some sanity checks on the data */
346
41
  if (str_len < 8)
347
2
    {
348
2
      warn ();
349
2
      return ASN1_TIME_ENCODING_ERROR;
350
2
    }
351
352
39
  if ((flags & ASN1_DECODE_FLAG_STRICT_DER)
353
0
      && !(flags & ASN1_DECODE_FLAG_ALLOW_INCORRECT_TIME))
354
0
    {
355
0
      p = &der[len_len];
356
0
      for (i = 0; i < (unsigned) (str_len - 1); i++)
357
0
  {
358
0
    if (c_isdigit (p[i]) == 0)
359
0
      {
360
0
        if (type == ASN1_ETYPE_GENERALIZED_TIME)
361
0
    {
362
      /* tolerate lax encodings */
363
0
      if (p[i] == '.' && dot_count == 0)
364
0
        {
365
0
          dot_count++;
366
0
          continue;
367
0
        }
368
369
      /* This is not really valid DER, but there are
370
       * structures using that */
371
0
      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
0
    }
378
379
0
        warn ();
380
0
        return ASN1_TIME_ENCODING_ERROR;
381
0
      }
382
0
  }
383
384
0
      if (sign_count == 0 && p[str_len - 1] != 'Z')
385
0
  {
386
0
    warn ();
387
0
    return ASN1_TIME_ENCODING_ERROR;
388
0
  }
389
0
    }
390
39
  memcpy (str, der + len_len, str_len);
391
39
  str[str_len] = 0;
392
39
  *ret_len = str_len + len_len;
393
394
39
  return ASN1_SUCCESS;
395
39
}
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
9.51k
{
414
9.51k
  int len_len, len, k;
415
9.51k
  int leading, parsed;
416
9.51k
  char temp[LTOSTR_MAX_SIZE];
417
9.51k
  uint64_t val, val1, val0;
418
419
9.51k
  *ret_len = 0;
420
9.51k
  if (str && str_size > 0)
421
9.51k
    str[0] = 0;     /* no oid */
422
423
9.51k
  if (str == NULL || der_len <= 0)
424
1
    return ASN1_GENERIC_ERROR;
425
426
9.51k
  len = asn1_get_length_der (der, der_len, &len_len);
427
428
9.51k
  if (len <= 0 || len + len_len > der_len)
429
138
    return ASN1_DER_ERROR;
430
431
  /* leading octet can never be 0x80 */
432
9.37k
  if (der[len_len] == 0x80)
433
4
    return ASN1_DER_ERROR;
434
435
9.37k
  val0 = 0;
436
437
13.2k
  for (k = 0; k < len; k++)
438
10.8k
    {
439
10.8k
      if (INT_LEFT_SHIFT_OVERFLOW (val0, 7))
440
12
  return ASN1_DER_ERROR;
441
442
10.8k
      val0 <<= 7;
443
10.8k
      val0 |= der[len_len + k] & 0x7F;
444
10.8k
      if (!(der[len_len + k] & 0x80))
445
7.05k
  break;
446
10.8k
    }
447
9.36k
  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
9.36k
  val = 0;
454
9.36k
  val1 = val0;
455
9.36k
  if (val1 > 39)
456
4.55k
    {
457
4.55k
      val = 1;
458
4.55k
      val1 = val0 - 40;
459
4.55k
      if (val1 > 39)
460
2.23k
  {
461
2.23k
    val = 2;
462
2.23k
    val1 = val0 - 80;
463
2.23k
  }
464
4.55k
    }
465
466
9.36k
  _asn1_str_cpy (str, str_size, _asn1_ltostr (val, temp));
467
9.36k
  _asn1_str_cat (str, str_size, ".");
468
9.36k
  _asn1_str_cat (str, str_size, _asn1_ltostr (val1, temp));
469
470
9.36k
  val = 0;
471
9.36k
  leading = 1;
472
54.4k
  for (k = parsed; k < len; k++)
473
45.1k
    {
474
      /* X.690 mandates that the leading byte must never be 0x80
475
       */
476
45.1k
      if (leading != 0 && der[len_len + k] == 0x80)
477
4
  return ASN1_DER_ERROR;
478
45.1k
      leading = 0;
479
480
      /* check for wrap around */
481
45.1k
      if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
482
24
  return ASN1_DER_ERROR;
483
484
45.0k
      val = val << 7;
485
45.0k
      val |= der[len_len + k] & 0x7F;
486
487
45.0k
      if (!(der[len_len + k] & 0x80))
488
32.4k
  {
489
32.4k
    _asn1_str_cat (str, str_size, ".");
490
32.4k
    _asn1_str_cat (str, str_size, _asn1_ltostr (val, temp));
491
32.4k
    val = 0;
492
32.4k
    leading = 1;
493
32.4k
  }
494
45.0k
    }
495
496
9.33k
  if (INT_ADD_OVERFLOW (len, len_len))
497
0
    return ASN1_DER_ERROR;
498
499
9.33k
  *ret_len = len + len_len;
500
501
9.33k
  return ASN1_SUCCESS;
502
9.33k
}
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
0
{
522
0
  int len_len = 0, len_byte;
523
524
0
  if (der_len <= 0)
525
0
    return ASN1_GENERIC_ERROR;
526
527
0
  len_byte = asn1_get_length_der (der, der_len, &len_len) - 1;
528
0
  if (len_byte < 0)
529
0
    return ASN1_DER_ERROR;
530
531
0
  *ret_len = len_byte + len_len + 1;
532
0
  *bit_len = len_byte * 8 - der[len_len];
533
534
0
  if (*bit_len < 0)
535
0
    return ASN1_DER_ERROR;
536
537
0
  if (str_size >= len_byte)
538
0
    {
539
0
      if (len_byte > 0 && str)
540
0
  memcpy (str, der + len_len + 1, len_byte);
541
0
    }
542
0
  else
543
0
    {
544
0
      return ASN1_MEM_ERROR;
545
0
    }
546
547
0
  return ASN1_SUCCESS;
548
0
}
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
100k
{
557
100k
  asn1_node p;
558
100k
  int counter, len2, len3, is_tag_implicit;
559
100k
  int result;
560
100k
  unsigned long tag, tag_implicit = 0;
561
100k
  unsigned char class, class2, class_implicit = 0;
562
563
100k
  if (der_len <= 0)
564
735
    return ASN1_GENERIC_ERROR;
565
566
100k
  counter = is_tag_implicit = 0;
567
568
100k
  if (node->type & CONST_TAG)
569
6.03k
    {
570
6.03k
      p = node->down;
571
13.8k
      while (p)
572
10.4k
  {
573
10.4k
    if (type_field (p->type) == ASN1_ETYPE_TAG)
574
6.03k
      {
575
6.03k
        if (p->type & CONST_APPLICATION)
576
0
    class2 = ASN1_CLASS_APPLICATION;
577
6.03k
        else if (p->type & CONST_UNIVERSAL)
578
0
    class2 = ASN1_CLASS_UNIVERSAL;
579
6.03k
        else if (p->type & CONST_PRIVATE)
580
0
    class2 = ASN1_CLASS_PRIVATE;
581
6.03k
        else
582
6.03k
    class2 = ASN1_CLASS_CONTEXT_SPECIFIC;
583
584
6.03k
        if (p->type & CONST_EXPLICIT)
585
3.68k
    {
586
3.68k
      if (asn1_get_tag_der
587
3.68k
          (der + counter, der_len, &class, &len2,
588
3.68k
           &tag) != ASN1_SUCCESS)
589
66
        return ASN1_DER_ERROR;
590
591
3.61k
      DECR_LEN (der_len, len2);
592
3.61k
      counter += len2;
593
594
3.61k
      if (flags & ASN1_DECODE_FLAG_STRICT_DER)
595
0
        len3 =
596
0
          asn1_get_length_der (der + counter, der_len, &len2);
597
3.61k
      else
598
3.61k
        len3 =
599
3.61k
          asn1_get_length_ber (der + counter, der_len, &len2);
600
3.61k
      if (len3 < 0)
601
420
        return ASN1_DER_ERROR;
602
603
3.19k
      DECR_LEN (der_len, len2);
604
3.19k
      counter += len2;
605
606
3.19k
      if (!is_tag_implicit)
607
3.19k
        {
608
3.19k
          if ((class != (class2 | ASN1_CLASS_STRUCTURED)) ||
609
1.07k
        (tag != strtoul ((char *) p->value, NULL, 10)))
610
2.19k
      return ASN1_TAG_ERROR;
611
3.19k
        }
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
999
      is_tag_implicit = 0;
618
999
    }
619
2.35k
        else
620
2.35k
    {   /* ASN1_TAG_IMPLICIT */
621
2.35k
      if (!is_tag_implicit)
622
2.35k
        {
623
2.35k
          if ((type_field (node->type) == ASN1_ETYPE_SEQUENCE) ||
624
2.35k
        (type_field (node->type) == ASN1_ETYPE_SEQUENCE_OF)
625
2.35k
        || (type_field (node->type) == ASN1_ETYPE_SET)
626
2.35k
        || (type_field (node->type) == ASN1_ETYPE_SET_OF))
627
0
      class2 |= ASN1_CLASS_STRUCTURED;
628
2.35k
          class_implicit = class2;
629
2.35k
          tag_implicit = strtoul ((char *) p->value, NULL, 10);
630
2.35k
          is_tag_implicit = 1;
631
2.35k
        }
632
2.35k
    }
633
6.03k
      }
634
7.80k
    p = p->right;
635
7.80k
  }
636
6.03k
    }
637
638
97.4k
  if (is_tag_implicit)
639
2.35k
    {
640
2.35k
      if (asn1_get_tag_der
641
2.35k
    (der + counter, der_len, &class, &len2, &tag) != ASN1_SUCCESS)
642
65
  return ASN1_DER_ERROR;
643
644
2.28k
      DECR_LEN (der_len, len2);
645
646
2.28k
      if ((class != class_implicit) || (tag != tag_implicit))
647
2.22k
  {
648
2.22k
    if (type_field (node->type) == ASN1_ETYPE_OCTET_STRING)
649
0
      {
650
0
        class_implicit |= ASN1_CLASS_STRUCTURED;
651
0
        if ((class != class_implicit) || (tag != tag_implicit))
652
0
    return ASN1_TAG_ERROR;
653
0
      }
654
2.22k
    else
655
2.22k
      return ASN1_TAG_ERROR;
656
2.22k
  }
657
2.28k
    }
658
95.1k
  else
659
95.1k
    {
660
95.1k
      unsigned type = type_field (node->type);
661
95.1k
      if (type == ASN1_ETYPE_TAG)
662
0
  {
663
0
    *tag_len = 0;
664
0
    if (inner_tag_len)
665
0
      *inner_tag_len = 0;
666
0
    return ASN1_SUCCESS;
667
0
  }
668
669
95.1k
      if (asn1_get_tag_der
670
95.1k
    (der + counter, der_len, &class, &len2, &tag) != ASN1_SUCCESS)
671
127
  return ASN1_DER_ERROR;
672
673
94.9k
      DECR_LEN (der_len, len2);
674
675
94.9k
      switch (type)
676
94.9k
  {
677
0
  case ASN1_ETYPE_NULL:
678
4.06k
  case ASN1_ETYPE_BOOLEAN:
679
6.60k
  case ASN1_ETYPE_INTEGER:
680
6.60k
  case ASN1_ETYPE_ENUMERATED:
681
16.1k
  case ASN1_ETYPE_OBJECT_ID:
682
16.1k
  case ASN1_ETYPE_GENERALSTRING:
683
16.1k
  case ASN1_ETYPE_NUMERIC_STRING:
684
16.1k
  case ASN1_ETYPE_IA5_STRING:
685
16.1k
  case ASN1_ETYPE_TELETEX_STRING:
686
16.1k
  case ASN1_ETYPE_PRINTABLE_STRING:
687
16.1k
  case ASN1_ETYPE_UNIVERSAL_STRING:
688
16.1k
  case ASN1_ETYPE_BMP_STRING:
689
16.1k
  case ASN1_ETYPE_UTF8_STRING:
690
16.1k
  case ASN1_ETYPE_VISIBLE_STRING:
691
16.3k
  case ASN1_ETYPE_BIT_STRING:
692
40.9k
  case ASN1_ETYPE_SEQUENCE:
693
51.3k
  case ASN1_ETYPE_SEQUENCE_OF:
694
51.3k
  case ASN1_ETYPE_SET:
695
85.5k
  case ASN1_ETYPE_SET_OF:
696
85.8k
  case ASN1_ETYPE_GENERALIZED_TIME:
697
86.1k
  case ASN1_ETYPE_UTC_TIME:
698
86.1k
    if ((class != _asn1_tags[type].class)
699
83.7k
        || (tag != _asn1_tags[type].tag))
700
4.28k
      return ASN1_DER_ERROR;
701
81.8k
    break;
702
703
81.8k
  case ASN1_ETYPE_OCTET_STRING:
704
    /* OCTET STRING is handled differently to allow
705
     * BER encodings (structured class). */
706
4.07k
    if (((class != ASN1_CLASS_UNIVERSAL)
707
1.92k
         && (class != (ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED)))
708
4.06k
        || (tag != ASN1_TAG_OCTET_STRING))
709
52
      return ASN1_DER_ERROR;
710
4.02k
    break;
711
4.79k
  case ASN1_ETYPE_ANY:
712
4.79k
    counter -= len2;
713
4.79k
    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
94.9k
  }
721
94.9k
    }
722
723
90.7k
  counter += len2;
724
90.7k
  *tag_len = counter;
725
90.7k
  if (inner_tag_len)
726
87.3k
    *inner_tag_len = len2;
727
90.7k
  return ASN1_SUCCESS;
728
729
0
cleanup:
730
0
  return result;
731
97.4k
}
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
100k
{
738
100k
  asn1_node p;
739
100k
  int ris = ASN1_DER_ERROR;
740
741
100k
  if (type_field (node->type) == ASN1_ETYPE_CHOICE)
742
3.30k
    {
743
3.30k
      p = node->down;
744
3.30k
      while (p)
745
3.30k
  {
746
3.30k
    ris =
747
3.30k
      _asn1_extract_tag_der (p, der, der_len, ret_len, inner_len,
748
3.30k
           flags);
749
3.30k
    if (ris == ASN1_SUCCESS)
750
3.30k
      break;
751
0
    p = p->right;
752
0
  }
753
754
3.30k
      *ret_len = 0;
755
3.30k
      return ris;
756
3.30k
    }
757
97.5k
  else
758
97.5k
    return _asn1_extract_tag_der (node, der, der_len, ret_len, inner_len,
759
97.5k
          flags);
760
100k
}
761
762
static int
763
_asn1_delete_not_used (asn1_node node)
764
91
{
765
91
  asn1_node p, p2;
766
767
91
  if (node == NULL)
768
0
    return ASN1_ELEMENT_NOT_FOUND;
769
770
91
  p = node;
771
21.0k
  while (p)
772
20.9k
    {
773
20.9k
      if (p->type & CONST_NOT_USED)
774
117
  {
775
117
    p2 = NULL;
776
117
    if (p != node)
777
117
      {
778
117
        p2 = _asn1_find_left (p);
779
117
        if (!p2)
780
0
    p2 = _asn1_find_up (p);
781
117
      }
782
117
    asn1_delete_structure (&p);
783
117
    p = p2;
784
117
  }
785
786
20.9k
      if (!p)
787
0
  break;     /* reach node */
788
789
20.9k
      if (p->down)
790
8.37k
  {
791
8.37k
    p = p->down;
792
8.37k
  }
793
12.5k
      else
794
12.5k
  {
795
12.5k
    if (p == node)
796
30
      p = NULL;
797
12.5k
    else if (p->right)
798
8.33k
      p = p->right;
799
4.19k
    else
800
4.19k
      {
801
8.37k
        while (1)
802
8.37k
    {
803
8.37k
      p = _asn1_find_up (p);
804
8.37k
      if (p == node)
805
61
        {
806
61
          p = NULL;
807
61
          break;
808
61
        }
809
8.31k
      if (p->right)
810
4.12k
        {
811
4.12k
          p = p->right;
812
4.12k
          break;
813
4.12k
        }
814
8.31k
    }
815
4.19k
      }
816
12.5k
  }
817
20.9k
    }
818
91
  return ASN1_SUCCESS;
819
91
}
820
821
static int
822
_asn1_get_indefinite_length_string (const unsigned char *der,
823
            int der_len, int *len)
824
962
{
825
962
  int len2, len3, counter, indefinite;
826
962
  int result;
827
962
  unsigned long tag;
828
962
  unsigned char class;
829
830
962
  counter = indefinite = 0;
831
832
11.6k
  while (1)
833
11.6k
    {
834
11.6k
      if (HAVE_TWO (der_len) && (der[counter] == 0)
835
1.72k
    && (der[counter + 1] == 0))
836
1.00k
  {
837
1.00k
    counter += 2;
838
1.00k
    DECR_LEN (der_len, 2);
839
840
1.00k
    indefinite--;
841
1.00k
    if (indefinite <= 0)
842
653
      break;
843
350
    else
844
350
      continue;
845
1.00k
  }
846
847
10.6k
      if (asn1_get_tag_der
848
10.6k
    (der + counter, der_len, &class, &len2, &tag) != ASN1_SUCCESS)
849
156
  return ASN1_DER_ERROR;
850
851
10.4k
      DECR_LEN (der_len, len2);
852
10.4k
      counter += len2;
853
854
10.4k
      len2 = asn1_get_length_der (der + counter, der_len, &len3);
855
10.4k
      if (len2 < -1)
856
153
  return ASN1_DER_ERROR;
857
858
10.3k
      if (len2 == -1)
859
2.98k
  {
860
2.98k
    indefinite++;
861
2.98k
    counter += 1;
862
2.98k
    DECR_LEN (der_len, 1);
863
2.98k
  }
864
7.31k
      else
865
7.31k
  {
866
7.31k
    counter += len2 + len3;
867
7.31k
    DECR_LEN (der_len, len2 + len3);
868
7.31k
  }
869
10.3k
    }
870
871
653
  *len = counter;
872
653
  return ASN1_SUCCESS;
873
874
0
cleanup:
875
0
  return result;
876
962
}
877
878
static void
879
delete_unneeded_choice_fields (asn1_node p)
880
3.30k
{
881
3.30k
  asn1_node p2;
882
883
3.37k
  while (p->right)
884
63
    {
885
63
      p2 = p->right;
886
63
      asn1_delete_structure (&p2);
887
63
    }
888
3.30k
}
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
3.72k
{
921
3.72k
  asn1_node node, p, p2, p3;
922
3.72k
  char temp[128];
923
3.72k
  int counter, len2, len3, len4, move, ris, tlen;
924
3.72k
  struct node_tail_cache_st tcache = { NULL, NULL };
925
3.72k
  unsigned char class;
926
3.72k
  unsigned long tag;
927
3.72k
  int tag_len;
928
3.72k
  int indefinite, result, total_len = *max_ider_len, ider_len = *max_ider_len;
929
3.72k
  int inner_tag_len;
930
3.72k
  unsigned char *ptmp;
931
3.72k
  const unsigned char *ptag;
932
3.72k
  const unsigned char *der = ider;
933
934
3.72k
  node = *element;
935
936
3.72k
  if (errorDescription != NULL)
937
0
    errorDescription[0] = 0;
938
939
3.72k
  if (node == NULL)
940
0
    return ASN1_ELEMENT_NOT_FOUND;
941
942
3.72k
  if (node->type & CONST_OPTION)
943
0
    {
944
0
      result = ASN1_GENERIC_ERROR;
945
0
      warn ();
946
0
      goto cleanup;
947
0
    }
948
949
3.72k
  counter = 0;
950
3.72k
  move = DOWN;
951
3.72k
  p = node;
952
153k
  while (1)
953
153k
    {
954
153k
      tag_len = 0;
955
153k
      inner_tag_len = 0;
956
153k
      ris = ASN1_SUCCESS;
957
153k
      if (move != UP)
958
97.7k
  {
959
97.7k
    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
97.7k
    p->start = counter;
1015
97.7k
    p->end = total_len - 1;
1016
1017
97.7k
    if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1018
11.0k
      {
1019
11.0k
        p2 = _asn1_find_up (p);
1020
11.0k
        len2 = p2->tmp_ival;
1021
11.0k
        if (counter == len2)
1022
130
    {
1023
130
      if (p->right)
1024
30
        {
1025
30
          p2 = p->right;
1026
30
          move = RIGHT;
1027
30
        }
1028
100
      else
1029
100
        move = UP;
1030
1031
130
      if (p->type & CONST_OPTION)
1032
123
        asn1_delete_structure (&p);
1033
1034
130
      p = p2;
1035
130
      continue;
1036
130
    }
1037
11.0k
      }
1038
1039
97.5k
    if (type_field (p->type) == ASN1_ETYPE_CHOICE)
1040
3.35k
      {
1041
3.53k
        while (p->down)
1042
3.48k
    {
1043
3.48k
      ris =
1044
3.48k
        extract_tag_der_recursive (p->down, der + counter,
1045
3.48k
                 ider_len, &len2, NULL, flags);
1046
1047
3.48k
      if (ris == ASN1_SUCCESS)
1048
3.30k
        {
1049
3.30k
          delete_unneeded_choice_fields (p->down);
1050
3.30k
          break;
1051
3.30k
        }
1052
171
      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
171
      else
1059
171
        {
1060
171
          p2 = p->down;
1061
171
          asn1_delete_structure (&p2);
1062
171
        }
1063
3.48k
    }
1064
1065
3.35k
        if (p->down == NULL)
1066
50
    {
1067
50
      if (!(p->type & CONST_OPTION))
1068
50
        {
1069
50
          result = ASN1_DER_ERROR;
1070
50
          warn ();
1071
50
          goto cleanup;
1072
50
        }
1073
50
    }
1074
3.30k
        else if (type_field (p->type) != ASN1_ETYPE_CHOICE)
1075
0
    p = p->down;
1076
1077
3.30k
        p->start = counter;
1078
3.30k
      }
1079
1080
97.5k
    if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1081
10.8k
      {
1082
10.8k
        p2 = _asn1_find_up (p);
1083
10.8k
        len2 = p2->tmp_ival;
1084
1085
10.8k
        if ((len2 != -1) && (counter > len2))
1086
142
    ris = ASN1_TAG_ERROR;
1087
10.8k
      }
1088
1089
97.5k
    if (ris == ASN1_SUCCESS)
1090
97.3k
      ris =
1091
97.3k
        extract_tag_der_recursive (p, der + counter, ider_len,
1092
97.3k
           &tag_len, &inner_tag_len, flags);
1093
1094
97.5k
    if (ris != ASN1_SUCCESS)
1095
10.1k
      {
1096
10.1k
        if (p->type & CONST_OPTION)
1097
2.85k
    {
1098
2.85k
      p->type |= CONST_NOT_USED;
1099
2.85k
      move = RIGHT;
1100
2.85k
    }
1101
7.29k
        else if (p->type & CONST_DEFAULT)
1102
6.14k
    {
1103
6.14k
      _asn1_set_value (p, NULL, 0);
1104
6.14k
      move = RIGHT;
1105
6.14k
    }
1106
1.14k
        else
1107
1.14k
    {
1108
1.14k
      if (errorDescription != NULL)
1109
0
        _asn1_error_description_tag_error (p, errorDescription);
1110
1111
1.14k
      result = ASN1_TAG_ERROR;
1112
1.14k
      warn ();
1113
1.14k
      goto cleanup;
1114
1.14k
    }
1115
10.1k
      }
1116
87.3k
    else
1117
87.3k
      {
1118
87.3k
        DECR_LEN (ider_len, tag_len);
1119
87.3k
        counter += tag_len;
1120
87.3k
      }
1121
97.5k
  }
1122
1123
152k
      if (ris == ASN1_SUCCESS)
1124
143k
  {
1125
143k
    switch (type_field (p->type))
1126
143k
      {
1127
0
      case ASN1_ETYPE_NULL:
1128
0
        DECR_LEN (ider_len, 1);
1129
0
        if (der[counter])
1130
0
    {
1131
0
      result = ASN1_DER_ERROR;
1132
0
      warn ();
1133
0
      goto cleanup;
1134
0
    }
1135
0
        counter++;
1136
0
        move = RIGHT;
1137
0
        break;
1138
469
      case ASN1_ETYPE_BOOLEAN:
1139
469
        DECR_LEN (ider_len, 2);
1140
1141
468
        if (der[counter++] != 1)
1142
7
    {
1143
7
      result = ASN1_DER_ERROR;
1144
7
      warn ();
1145
7
      goto cleanup;
1146
7
    }
1147
461
        if (der[counter++] == 0)
1148
201
    _asn1_set_value (p, "F", 1);
1149
260
        else
1150
260
    _asn1_set_value (p, "T", 1);
1151
461
        move = RIGHT;
1152
461
        break;
1153
2.15k
      case ASN1_ETYPE_INTEGER:
1154
2.15k
      case ASN1_ETYPE_ENUMERATED:
1155
2.15k
        len2 = asn1_get_length_der (der + counter, ider_len, &len3);
1156
2.15k
        if (len2 < 0)
1157
137
    {
1158
137
      result = ASN1_DER_ERROR;
1159
137
      warn ();
1160
137
      goto cleanup;
1161
137
    }
1162
1163
2.02k
        DECR_LEN (ider_len, len3 + len2);
1164
1165
2.02k
        _asn1_set_value (p, der + counter, len3 + len2);
1166
2.02k
        counter += len3 + len2;
1167
2.02k
        move = RIGHT;
1168
2.02k
        break;
1169
9.51k
      case ASN1_ETYPE_OBJECT_ID:
1170
9.51k
        result =
1171
9.51k
    asn1_get_object_id_der (der + counter, ider_len, &len2,
1172
9.51k
          temp, sizeof (temp));
1173
9.51k
        if (result != ASN1_SUCCESS)
1174
183
    {
1175
183
      warn ();
1176
183
      goto cleanup;
1177
183
    }
1178
1179
9.33k
        DECR_LEN (ider_len, len2);
1180
1181
9.33k
        tlen = strlen (temp);
1182
9.33k
        if (tlen > 0)
1183
9.33k
    _asn1_set_value (p, temp, tlen + 1);
1184
1185
9.33k
        counter += len2;
1186
9.33k
        move = RIGHT;
1187
9.33k
        break;
1188
98
      case ASN1_ETYPE_GENERALIZED_TIME:
1189
161
      case ASN1_ETYPE_UTC_TIME:
1190
161
        result =
1191
161
    _asn1_get_time_der (type_field (p->type), der + counter,
1192
161
            ider_len, &len2, temp, sizeof (temp) - 1,
1193
161
            flags);
1194
161
        if (result != ASN1_SUCCESS)
1195
122
    {
1196
122
      warn ();
1197
122
      goto cleanup;
1198
122
    }
1199
1200
39
        DECR_LEN (ider_len, len2);
1201
1202
39
        tlen = strlen (temp);
1203
39
        if (tlen > 0)
1204
32
    _asn1_set_value (p, temp, tlen);
1205
1206
39
        counter += len2;
1207
39
        move = RIGHT;
1208
39
        break;
1209
4.02k
      case ASN1_ETYPE_OCTET_STRING:
1210
4.02k
        if (counter < inner_tag_len)
1211
0
    {
1212
0
      result = ASN1_DER_ERROR;
1213
0
      warn ();
1214
0
      goto cleanup;
1215
0
    }
1216
1217
4.02k
        ptag = der + counter - inner_tag_len;
1218
4.02k
        if ((flags & ASN1_DECODE_FLAG_STRICT_DER)
1219
4.02k
      || !(ptag[0] & ASN1_CLASS_STRUCTURED))
1220
2.12k
    {
1221
2.12k
      if (ptag[0] & ASN1_CLASS_STRUCTURED)
1222
0
        {
1223
0
          result = ASN1_DER_ERROR;
1224
0
          warn ();
1225
0
          goto cleanup;
1226
0
        }
1227
1228
2.12k
      len2 = asn1_get_length_der (der + counter, ider_len, &len3);
1229
2.12k
      if (len2 < 0)
1230
114
        {
1231
114
          result = ASN1_DER_ERROR;
1232
114
          warn ();
1233
114
          goto cleanup;
1234
114
        }
1235
1236
2.00k
      DECR_LEN (ider_len, len3 + len2);
1237
1238
2.00k
      _asn1_set_value (p, der + counter, len3 + len2);
1239
2.00k
      counter += len3 + len2;
1240
2.00k
    }
1241
1.89k
        else
1242
1.89k
    {
1243
1.89k
      unsigned dflags = 0, vlen, ber_len;
1244
1245
1.89k
      if (ptag[0] & ASN1_CLASS_STRUCTURED)
1246
1.89k
        dflags |= DECODE_FLAG_CONSTRUCTED;
1247
1248
1.89k
      result =
1249
1.89k
        _asn1_decode_simple_ber (type_field (p->type),
1250
1.89k
               der + counter, ider_len, &ptmp,
1251
1.89k
               &vlen, &ber_len, dflags);
1252
1.89k
      if (result != ASN1_SUCCESS)
1253
565
        {
1254
565
          warn ();
1255
565
          goto cleanup;
1256
565
        }
1257
1258
1.33k
      DECR_LEN (ider_len, ber_len);
1259
1260
1.33k
      _asn1_set_value_lv (p, ptmp, vlen);
1261
1262
1.33k
      counter += ber_len;
1263
1.33k
      free (ptmp);
1264
1.33k
    }
1265
3.34k
        move = RIGHT;
1266
3.34k
        break;
1267
0
      case ASN1_ETYPE_GENERALSTRING:
1268
0
      case ASN1_ETYPE_NUMERIC_STRING:
1269
0
      case ASN1_ETYPE_IA5_STRING:
1270
0
      case ASN1_ETYPE_TELETEX_STRING:
1271
0
      case ASN1_ETYPE_PRINTABLE_STRING:
1272
0
      case ASN1_ETYPE_UNIVERSAL_STRING:
1273
0
      case ASN1_ETYPE_BMP_STRING:
1274
0
      case ASN1_ETYPE_UTF8_STRING:
1275
0
      case ASN1_ETYPE_VISIBLE_STRING:
1276
284
      case ASN1_ETYPE_BIT_STRING:
1277
284
        len2 = asn1_get_length_der (der + counter, ider_len, &len3);
1278
284
        if (len2 < 0)
1279
107
    {
1280
107
      result = ASN1_DER_ERROR;
1281
107
      warn ();
1282
107
      goto cleanup;
1283
107
    }
1284
1285
177
        DECR_LEN (ider_len, len3 + len2);
1286
1287
177
        _asn1_set_value (p, der + counter, len3 + len2);
1288
177
        counter += len3 + len2;
1289
177
        move = RIGHT;
1290
177
        break;
1291
33.2k
      case ASN1_ETYPE_SEQUENCE:
1292
33.2k
      case ASN1_ETYPE_SET:
1293
33.2k
        if (move == UP)
1294
8.76k
    {
1295
8.76k
      len2 = p->tmp_ival;
1296
8.76k
      p->tmp_ival = 0;
1297
8.76k
      if (len2 == -1)
1298
1.50k
        {   /* indefinite length method */
1299
1.50k
          DECR_LEN (ider_len, 2);
1300
1.17k
          if ((der[counter]) || der[counter + 1])
1301
163
      {
1302
163
        result = ASN1_DER_ERROR;
1303
163
        warn ();
1304
163
        goto cleanup;
1305
163
      }
1306
1.01k
          counter += 2;
1307
1.01k
        }
1308
7.25k
      else
1309
7.25k
        {   /* definite length method */
1310
7.25k
          if (len2 != counter)
1311
283
      {
1312
283
        result = ASN1_DER_ERROR;
1313
283
        warn ();
1314
283
        goto cleanup;
1315
283
      }
1316
7.25k
        }
1317
7.98k
      move = RIGHT;
1318
7.98k
    }
1319
24.4k
        else
1320
24.4k
    {   /* move==DOWN || move==RIGHT */
1321
24.4k
      len3 = asn1_get_length_der (der + counter, ider_len, &len2);
1322
24.4k
      if (IS_ERR (len3, flags))
1323
132
        {
1324
132
          result = ASN1_DER_ERROR;
1325
132
          warn ();
1326
132
          goto cleanup;
1327
132
        }
1328
1329
24.3k
      DECR_LEN (ider_len, len2);
1330
24.3k
      counter += len2;
1331
1332
24.3k
      if (len3 > 0)
1333
8.55k
        {
1334
8.55k
          p->tmp_ival = counter + len3;
1335
8.55k
          move = DOWN;
1336
8.55k
        }
1337
15.7k
      else if (len3 == 0)
1338
8.59k
        {
1339
8.59k
          p2 = p->down;
1340
33.8k
          while (p2)
1341
25.2k
      {
1342
25.2k
        if (type_field (p2->type) != ASN1_ETYPE_TAG)
1343
25.2k
          {
1344
25.2k
            p3 = p2->right;
1345
25.2k
            asn1_delete_structure (&p2);
1346
25.2k
            p2 = p3;
1347
25.2k
          }
1348
0
        else
1349
0
          p2 = p2->right;
1350
25.2k
      }
1351
8.59k
          move = RIGHT;
1352
8.59k
        }
1353
7.16k
      else
1354
7.16k
        {   /* indefinite length method */
1355
7.16k
          p->tmp_ival = -1;
1356
7.16k
          move = DOWN;
1357
7.16k
        }
1358
24.3k
    }
1359
32.2k
        break;
1360
41.9k
      case ASN1_ETYPE_SEQUENCE_OF:
1361
83.0k
      case ASN1_ETYPE_SET_OF:
1362
83.0k
        if (move == UP)
1363
44.7k
    {
1364
44.7k
      len2 = p->tmp_ival;
1365
44.7k
      if (len2 == -1)
1366
42.4k
        {   /* indefinite length method */
1367
42.4k
          if (!HAVE_TWO (ider_len)
1368
42.2k
        || ((der[counter]) || der[counter + 1]))
1369
41.9k
      {
1370
41.9k
        result = _asn1_append_sequence_set (p, &tcache);
1371
41.9k
        if (result != 0)
1372
0
          {
1373
0
            warn ();
1374
0
            goto cleanup;
1375
0
          }
1376
41.9k
        p = tcache.tail;
1377
41.9k
        move = RIGHT;
1378
41.9k
        continue;
1379
41.9k
      }
1380
1381
494
          p->tmp_ival = 0;
1382
494
          tcache.tail = NULL; /* finished decoding this structure */
1383
494
          tcache.head = NULL;
1384
494
          DECR_LEN (ider_len, 2);
1385
494
          counter += 2;
1386
494
        }
1387
2.33k
      else
1388
2.33k
        {   /* definite length method */
1389
2.33k
          if (len2 > counter)
1390
943
      {
1391
943
        result = _asn1_append_sequence_set (p, &tcache);
1392
943
        if (result != 0)
1393
0
          {
1394
0
            warn ();
1395
0
            goto cleanup;
1396
0
          }
1397
943
        p = tcache.tail;
1398
943
        move = RIGHT;
1399
943
        continue;
1400
943
      }
1401
1402
1.38k
          p->tmp_ival = 0;
1403
1.38k
          tcache.tail = NULL; /* finished decoding this structure */
1404
1.38k
          tcache.head = NULL;
1405
1406
1.38k
          if (len2 != counter)
1407
22
      {
1408
22
        result = ASN1_DER_ERROR;
1409
22
        warn ();
1410
22
        goto cleanup;
1411
22
      }
1412
1.38k
        }
1413
44.7k
    }
1414
38.2k
        else
1415
38.2k
    {   /* move==DOWN || move==RIGHT */
1416
38.2k
      len3 = asn1_get_length_der (der + counter, ider_len, &len2);
1417
38.2k
      if (IS_ERR (len3, flags))
1418
116
        {
1419
116
          result = ASN1_DER_ERROR;
1420
116
          warn ();
1421
116
          goto cleanup;
1422
116
        }
1423
1424
38.1k
      DECR_LEN (ider_len, len2);
1425
38.1k
      counter += len2;
1426
38.1k
      if (len3)
1427
3.40k
        {
1428
3.40k
          if (len3 > 0)
1429
1.52k
      { /* definite length method */
1430
1.52k
        p->tmp_ival = counter + len3;
1431
1.52k
      }
1432
1.87k
          else
1433
1.87k
      { /* indefinite length method */
1434
1.87k
        p->tmp_ival = -1;
1435
1.87k
      }
1436
1437
3.40k
          p2 = p->down;
1438
3.40k
          if (p2 == NULL)
1439
0
      {
1440
0
        result = ASN1_DER_ERROR;
1441
0
        warn ();
1442
0
        goto cleanup;
1443
0
      }
1444
1445
7.26k
          while ((type_field (p2->type) == ASN1_ETYPE_TAG)
1446
6.31k
           || (type_field (p2->type) == ASN1_ETYPE_SIZE))
1447
3.86k
      p2 = p2->right;
1448
3.40k
          if (p2->right == NULL)
1449
3.40k
      {
1450
3.40k
        result = _asn1_append_sequence_set (p, &tcache);
1451
3.40k
        if (result != 0)
1452
0
          {
1453
0
            warn ();
1454
0
            goto cleanup;
1455
0
          }
1456
3.40k
      }
1457
3.40k
          p = p2;
1458
3.40k
        }
1459
38.1k
    }
1460
39.9k
        move = RIGHT;
1461
39.9k
        break;
1462
4.79k
      case ASN1_ETYPE_ANY:
1463
        /* Check indefinite length method in an EXPLICIT TAG */
1464
1465
4.79k
        if (!(flags & ASN1_DECODE_FLAG_STRICT_DER)
1466
4.79k
      && (p->type & CONST_TAG) && tag_len == 2
1467
0
      && (der[counter - 1] == 0x80))
1468
0
    indefinite = 1;
1469
4.79k
        else
1470
4.79k
    indefinite = 0;
1471
1472
4.79k
        if (asn1_get_tag_der
1473
4.79k
      (der + counter, ider_len, &class, &len2,
1474
4.79k
       &tag) != ASN1_SUCCESS)
1475
0
    {
1476
0
      result = ASN1_DER_ERROR;
1477
0
      warn ();
1478
0
      goto cleanup;
1479
0
    }
1480
1481
4.79k
        DECR_LEN (ider_len, len2);
1482
1483
4.79k
        len4 =
1484
4.79k
    asn1_get_length_der (der + counter + len2, ider_len, &len3);
1485
4.79k
        if (IS_ERR (len4, flags))
1486
125
    {
1487
125
      result = ASN1_DER_ERROR;
1488
125
      warn ();
1489
125
      goto cleanup;
1490
125
    }
1491
4.66k
        if (len4 != -1)  /* definite */
1492
4.02k
    {
1493
4.02k
      len2 += len4;
1494
1495
4.02k
      DECR_LEN (ider_len, len4 + len3);
1496
4.02k
      _asn1_set_value_lv (p, der + counter, len2 + len3);
1497
4.02k
      counter += len2 + len3;
1498
4.02k
    }
1499
637
        else    /* == -1 */
1500
637
    {   /* indefinite length */
1501
637
      ider_len += len2; /* undo DECR_LEN */
1502
1503
637
      if (counter == 0)
1504
0
        {
1505
0
          result = ASN1_DER_ERROR;
1506
0
          warn ();
1507
0
          goto cleanup;
1508
0
        }
1509
1510
637
      result =
1511
637
        _asn1_get_indefinite_length_string (der + counter,
1512
637
              ider_len, &len2);
1513
637
      if (result != ASN1_SUCCESS)
1514
31
        {
1515
31
          warn ();
1516
31
          goto cleanup;
1517
31
        }
1518
1519
606
      DECR_LEN (ider_len, len2);
1520
606
      _asn1_set_value_lv (p, der + counter, len2);
1521
606
      counter += len2;
1522
1523
606
    }
1524
1525
        /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1526
           an indefinite length method. */
1527
4.63k
        if (indefinite)
1528
0
    {
1529
0
      DECR_LEN (ider_len, 2);
1530
0
      if (!der[counter] && !der[counter + 1])
1531
0
        {
1532
0
          counter += 2;
1533
0
        }
1534
0
      else
1535
0
        {
1536
0
          result = ASN1_DER_ERROR;
1537
0
          warn ();
1538
0
          goto cleanup;
1539
0
        }
1540
0
    }
1541
1542
4.63k
        move = RIGHT;
1543
4.63k
        break;
1544
5.99k
      default:
1545
5.99k
        move = (move == UP) ? RIGHT : DOWN;
1546
5.99k
        break;
1547
143k
      }
1548
143k
  }
1549
1550
107k
      if (p)
1551
107k
  {
1552
107k
    p->end = counter - 1;
1553
107k
  }
1554
1555
107k
      if (p == node && move != DOWN)
1556
91
  break;
1557
1558
107k
      if (move == DOWN)
1559
19.0k
  {
1560
19.0k
    if (p->down)
1561
19.0k
      p = p->down;
1562
0
    else
1563
0
      move = RIGHT;
1564
19.0k
  }
1565
107k
      if ((move == RIGHT) && !(p->type & CONST_SET))
1566
88.1k
  {
1567
88.1k
    if (p->right)
1568
32.0k
      p = p->right;
1569
56.1k
    else
1570
56.1k
      move = UP;
1571
88.1k
  }
1572
107k
      if (move == UP)
1573
56.1k
  {
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
56.1k
    if (tcache.tail == p)
1577
43.0k
      p = tcache.head;
1578
13.1k
    else
1579
13.1k
      p = _asn1_find_up (p);
1580
56.1k
  }
1581
107k
    }
1582
1583
91
  _asn1_delete_not_used (*element);
1584
1585
91
  if ((ider_len < 0) ||
1586
91
      (!(flags & ASN1_DECODE_FLAG_ALLOW_PADDING) && (ider_len != 0)))
1587
22
    {
1588
22
      warn ();
1589
22
      result = ASN1_DER_ERROR;
1590
22
      goto cleanup;
1591
22
    }
1592
1593
69
  *max_ider_len = total_len - ider_len;
1594
1595
69
  return ASN1_SUCCESS;
1596
1597
3.66k
cleanup:
1598
3.66k
  asn1_delete_structure (element);
1599
3.66k
  return result;
1600
91
}
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
3.72k
{
1627
3.72k
  return asn1_der_decoding2 (element, ider, &ider_len, 0, errorDescription);
1628
3.72k
}
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
69
{
1693
69
  asn1_node node, node_to_find;
1694
69
  int result = ASN1_DER_ERROR;
1695
1696
69
  node = element;
1697
1698
69
  if (node == NULL)
1699
0
    return ASN1_ELEMENT_NOT_FOUND;
1700
1701
69
  node_to_find = asn1_find_node (node, name_element);
1702
1703
69
  if (node_to_find == NULL)
1704
22
    return ASN1_ELEMENT_NOT_FOUND;
1705
1706
47
  *start = node_to_find->start;
1707
47
  *end = node_to_find->end;
1708
1709
47
  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
47
  if (*end < *start)
1731
0
    return ASN1_GENERIC_ERROR;
1732
1733
47
  return ASN1_SUCCESS;
1734
47
}
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
2.35k
{
2126
2.35k
  int tag_len, len_len;
2127
2.35k
  const unsigned char *p;
2128
2.35k
  int der_len = _der_len;
2129
2.35k
  unsigned char class;
2130
2.35k
  unsigned long tag;
2131
2.35k
  long ret;
2132
2133
2.35k
  if (der == NULL || der_len == 0)
2134
0
    return ASN1_VALUE_NOT_VALID;
2135
2136
2.35k
  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
2.35k
  class = ETYPE_CLASS (etype);
2141
2.35k
  if (class != ASN1_CLASS_UNIVERSAL)
2142
0
    return ASN1_VALUE_NOT_VALID;
2143
2144
2.35k
  p = der;
2145
2146
2.35k
  if (dflags & DECODE_FLAG_HAVE_TAG)
2147
2.35k
    {
2148
2.35k
      ret = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag);
2149
2.35k
      if (ret != ASN1_SUCCESS)
2150
0
  return ret;
2151
2152
2.35k
      if (class != ETYPE_CLASS (etype) || tag != ETYPE_TAG (etype))
2153
0
  {
2154
0
    warn ();
2155
0
    return ASN1_DER_ERROR;
2156
0
  }
2157
2158
2.35k
      p += tag_len;
2159
2.35k
      der_len -= tag_len;
2160
2.35k
      if (der_len <= 0)
2161
23
  return ASN1_DER_ERROR;
2162
2.35k
    }
2163
2164
2.32k
  ret = asn1_get_length_der (p, der_len, &len_len);
2165
2.32k
  if (ret < 0)
2166
0
    return ASN1_DER_ERROR;
2167
2168
2.32k
  p += len_len;
2169
2.32k
  der_len -= len_len;
2170
2.32k
  if (der_len <= 0)
2171
44
    return ASN1_DER_ERROR;
2172
2173
2.28k
  *str_len = ret;
2174
2.28k
  *str = p;
2175
2176
2.28k
  return ASN1_SUCCESS;
2177
2.32k
}
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
0
{
2197
0
  return _asn1_decode_simple_der (etype, der, _der_len, str, str_len,
2198
0
          DECODE_FLAG_HAVE_TAG);
2199
0
}
2200
2201
static int
2202
append (uint8_t **dst, unsigned *dst_size, const unsigned char *src,
2203
  unsigned src_size)
2204
6.12k
{
2205
6.12k
  if (src_size == 0)
2206
5.06k
    return ASN1_SUCCESS;
2207
2208
1.05k
  *dst = _asn1_realloc (*dst, *dst_size + src_size);
2209
1.05k
  if (*dst == NULL)
2210
0
    return ASN1_MEM_ALLOC_ERROR;
2211
1.05k
  memcpy (*dst + *dst_size, src, src_size);
2212
1.05k
  *dst_size += src_size;
2213
1.05k
  return ASN1_SUCCESS;
2214
1.05k
}
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
6.27k
{
2238
6.27k
  int tag_len, len_len;
2239
6.27k
  const unsigned char *p;
2240
6.27k
  int der_len = _der_len;
2241
6.27k
  uint8_t *total = NULL;
2242
6.27k
  unsigned total_size = 0;
2243
6.27k
  unsigned char class;
2244
6.27k
  unsigned long tag;
2245
6.27k
  unsigned char *out = NULL;
2246
6.27k
  const unsigned char *cout = NULL;
2247
6.27k
  unsigned out_len;
2248
6.27k
  long result;
2249
2250
6.27k
  if (ber_len)
2251
6.27k
    *ber_len = 0;
2252
2253
6.27k
  if (der == NULL || der_len == 0)
2254
10
    {
2255
10
      warn ();
2256
10
      return ASN1_VALUE_NOT_VALID;
2257
10
    }
2258
2259
6.26k
  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
6.26k
  class = ETYPE_CLASS (etype);
2267
6.26k
  if (class != ASN1_CLASS_UNIVERSAL)
2268
0
    {
2269
0
      warn ();
2270
0
      return ASN1_VALUE_NOT_VALID;
2271
0
    }
2272
2273
6.26k
  p = der;
2274
2275
6.26k
  if (dflags & DECODE_FLAG_HAVE_TAG)
2276
4.37k
    {
2277
4.37k
      result = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag);
2278
4.37k
      if (result != ASN1_SUCCESS)
2279
56
  {
2280
56
    warn ();
2281
56
    return result;
2282
56
  }
2283
2284
4.31k
      if (tag != ETYPE_TAG (etype))
2285
115
  {
2286
115
    warn ();
2287
115
    return ASN1_DER_ERROR;
2288
115
  }
2289
2290
4.19k
      p += tag_len;
2291
2292
4.19k
      DECR_LEN (der_len, tag_len);
2293
2294
4.19k
      if (ber_len)
2295
4.19k
  *ber_len += tag_len;
2296
4.19k
    }
2297
2298
  /* indefinite constructed */
2299
6.09k
  if ((((dflags & DECODE_FLAG_CONSTRUCTED) || class == ASN1_CLASS_STRUCTURED)
2300
3.64k
       && ETYPE_IS_STRING (etype)) && !(dflags & DECODE_FLAG_LEVEL3))
2301
3.63k
    {
2302
3.63k
      if (der_len == 0)
2303
3
  {
2304
3
    warn ();
2305
3
    result = ASN1_DER_ERROR;
2306
3
    goto cleanup;
2307
3
  }
2308
2309
3.63k
      if (der_len > 0 && p[0] == 0x80)  /* indefinite */
2310
691
  {
2311
691
    len_len = 1;
2312
691
    DECR_LEN (der_len, len_len);
2313
691
    p += len_len;
2314
2315
691
    if (ber_len)
2316
691
      *ber_len += len_len;
2317
2318
    /* decode the available octet strings */
2319
691
    do
2320
2.65k
      {
2321
2.65k
        unsigned tmp_len;
2322
2.65k
        unsigned flags = DECODE_FLAG_HAVE_TAG;
2323
2324
2.65k
        if (dflags & DECODE_FLAG_LEVEL1)
2325
562
    flags |= DECODE_FLAG_LEVEL2;
2326
2.09k
        else if (dflags & DECODE_FLAG_LEVEL2)
2327
547
    flags |= DECODE_FLAG_LEVEL3;
2328
1.54k
        else
2329
1.54k
    flags |= DECODE_FLAG_LEVEL1;
2330
2331
2.65k
        result =
2332
2.65k
    _asn1_decode_simple_ber (etype, p, der_len, &out, &out_len,
2333
2.65k
           &tmp_len, flags);
2334
2.65k
        if (result != ASN1_SUCCESS)
2335
270
    {
2336
270
      warn ();
2337
270
      goto cleanup;
2338
270
    }
2339
2340
2.38k
        p += tmp_len;
2341
2.38k
        DECR_LEN (der_len, tmp_len);
2342
2343
2.38k
        if (ber_len)
2344
2.38k
    *ber_len += tmp_len;
2345
2346
2.38k
        DECR_LEN (der_len, 2); /* we need the EOC */
2347
2348
2.36k
        result = append (&total, &total_size, out, out_len);
2349
2.36k
        if (result != ASN1_SUCCESS)
2350
0
    {
2351
0
      warn ();
2352
0
      goto cleanup;
2353
0
    }
2354
2355
2.36k
        free (out);
2356
2.36k
        out = NULL;
2357
2358
2.36k
        if (p[0] == 0 && p[1] == 0)  /* EOC */
2359
378
    {
2360
378
      if (ber_len)
2361
378
        *ber_len += 2;
2362
378
      break;
2363
378
    }
2364
2365
        /* no EOC */
2366
1.98k
        der_len += 2;
2367
2368
1.98k
        if (der_len == 2)
2369
24
    {
2370
24
      warn ();
2371
24
      result = ASN1_DER_ERROR;
2372
24
      goto cleanup;
2373
24
    }
2374
1.98k
      }
2375
1.96k
    while (1);
2376
691
  }
2377
2.94k
      else      /* constructed */
2378
2.94k
  {
2379
2.94k
    long const_len;
2380
2381
2.94k
    result = asn1_get_length_ber (p, der_len, &len_len);
2382
2.94k
    if (result < 0)
2383
117
      {
2384
117
        warn ();
2385
117
        result = ASN1_DER_ERROR;
2386
117
        goto cleanup;
2387
117
      }
2388
2389
2.82k
    DECR_LEN (der_len, len_len);
2390
2.82k
    p += len_len;
2391
2392
2.82k
    const_len = result;
2393
2394
2.82k
    if (ber_len)
2395
2.82k
      *ber_len += len_len;
2396
2397
    /* decode the available octet strings */
2398
4.30k
    while (const_len > 0)
2399
1.72k
      {
2400
1.72k
        unsigned tmp_len;
2401
1.72k
        unsigned flags = DECODE_FLAG_HAVE_TAG;
2402
2403
1.72k
        if (dflags & DECODE_FLAG_LEVEL1)
2404
480
    flags |= DECODE_FLAG_LEVEL2;
2405
1.24k
        else if (dflags & DECODE_FLAG_LEVEL2)
2406
353
    flags |= DECODE_FLAG_LEVEL3;
2407
894
        else
2408
894
    flags |= DECODE_FLAG_LEVEL1;
2409
2410
1.72k
        result =
2411
1.72k
    _asn1_decode_simple_ber (etype, p, der_len, &out, &out_len,
2412
1.72k
           &tmp_len, flags);
2413
1.72k
        if (result != ASN1_SUCCESS)
2414
204
    {
2415
204
      warn ();
2416
204
      goto cleanup;
2417
204
    }
2418
2419
1.52k
        p += tmp_len;
2420
1.52k
        DECR_LEN (der_len, tmp_len);
2421
1.52k
        DECR_LEN (const_len, tmp_len);
2422
2423
1.47k
        if (ber_len)
2424
1.47k
    *ber_len += tmp_len;
2425
2426
1.47k
        result = append (&total, &total_size, out, out_len);
2427
1.47k
        if (result != ASN1_SUCCESS)
2428
0
    {
2429
0
      warn ();
2430
0
      goto cleanup;
2431
0
    }
2432
2433
1.47k
        free (out);
2434
1.47k
        out = NULL;
2435
1.47k
      }
2436
2.82k
  }
2437
3.63k
    }
2438
2.45k
  else if (class == ETYPE_CLASS (etype))
2439
2.45k
    {
2440
2.45k
      if (ber_len)
2441
2.45k
  {
2442
2.45k
    result = asn1_get_length_der (p, der_len, &len_len);
2443
2.45k
    if (result < 0)
2444
101
      {
2445
101
        warn ();
2446
101
        result = ASN1_DER_ERROR;
2447
101
        goto cleanup;
2448
101
      }
2449
2.35k
    *ber_len += result + len_len;
2450
2.35k
  }
2451
2452
      /* non-string values are decoded as DER */
2453
2.35k
      result =
2454
2.35k
  _asn1_decode_simple_der (etype, der, _der_len, &cout, &out_len,
2455
2.35k
         dflags);
2456
2.35k
      if (result != ASN1_SUCCESS)
2457
67
  {
2458
67
    warn ();
2459
67
    goto cleanup;
2460
67
  }
2461
2462
2.28k
      result = append (&total, &total_size, cout, out_len);
2463
2.28k
      if (result != ASN1_SUCCESS)
2464
0
  {
2465
0
    warn ();
2466
0
    goto cleanup;
2467
0
  }
2468
2.28k
    }
2469
8
  else
2470
8
    {
2471
8
      warn ();
2472
8
      result = ASN1_DER_ERROR;
2473
8
      goto cleanup;
2474
8
    }
2475
2476
5.23k
  *str = total;
2477
5.23k
  *str_len = total_size;
2478
2479
5.23k
  return ASN1_SUCCESS;
2480
858
cleanup:
2481
858
  free (out);
2482
858
  free (total);
2483
858
  return result;
2484
6.09k
}
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
0
{
2506
0
  return _asn1_decode_simple_ber (etype, der, _der_len, str, str_len, ber_len,
2507
0
          DECODE_FLAG_HAVE_TAG);
2508
0
}