Coverage Report

Created: 2024-11-25 06:31

/src/libtasn1/lib/decoding.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (C) 2002-2024 Free Software Foundation, Inc.
3
 *
4
 * This file is part of LIBTASN1.
5
 *
6
 * The LIBTASN1 library is free software; you can redistribute it
7
 * and/or modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
 * 02110-1301, USA
20
 */
21
22
23
/*****************************************************/
24
/* File: decoding.c                                  */
25
/* Description: Functions to manage DER decoding     */
26
/*****************************************************/
27
28
#include <int.h>
29
#include <parser_aux.h>
30
#include <gstr.h>
31
#include <structure.h>
32
#include <element.h>
33
#include <limits.h>
34
#include <intprops.h>
35
#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
3.84M
#define IS_ERR(len, flags) (len < -1 || ((flags & ASN1_DECODE_FLAG_STRICT_DER) && len < 0))
44
45
3.26M
#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
462k
#define DECODE_FLAG_HAVE_TAG 1
59
151k
#define DECODE_FLAG_CONSTRUCTED (1<<1)
60
204k
#define DECODE_FLAG_LEVEL1 (1<<2)
61
105k
#define DECODE_FLAG_LEVEL2 (1<<3)
62
6.64k
#define DECODE_FLAG_LEVEL3 (1<<4)
63
64
28.9M
#define DECR_LEN(l, s) do { \
65
28.9M
    l -= s; \
66
28.9M
    if (l < 0) { \
67
365
      warn(); \
68
365
      result = ASN1_DER_ERROR; \
69
365
      goto cleanup; \
70
365
    } \
71
28.9M
  } 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.86k
{
91
92
1.86k
  Estrcpy (ErrorDescription, ":: tag error near element '");
93
1.86k
  _asn1_hierarchical_name (node, ErrorDescription + strlen (ErrorDescription),
94
1.86k
         ASN1_MAX_ERROR_DESCRIPTION_SIZE - 40);
95
1.86k
  Estrcat (ErrorDescription, "'");
96
97
1.86k
}
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
12.1M
{
114
12.1M
  unsigned int ans;
115
12.1M
  int k, punt, sum;
116
117
12.1M
  *len = 0;
118
12.1M
  if (der_len <= 0)
119
3.83k
    return 0;
120
121
12.1M
  if (!(der[0] & 128))
122
10.1M
    {
123
      /* short form */
124
10.1M
      *len = 1;
125
10.1M
      ans = der[0];
126
10.1M
    }
127
2.00M
  else
128
2.00M
    {
129
      /* Long form */
130
2.00M
      k = der[0] & 0x7F;
131
2.00M
      punt = 1;
132
2.00M
      if (k)
133
1.88M
  {     /* definite length method */
134
1.88M
    ans = 0;
135
5.22M
    while (punt <= k && punt < der_len)
136
3.34M
      {
137
3.34M
        if (INT_MULTIPLY_OVERFLOW (ans, 256))
138
1.83k
    return -2;
139
3.34M
        ans *= 256;
140
141
3.34M
        if (INT_ADD_OVERFLOW (ans, ((unsigned) der[punt])))
142
0
    return -2;
143
3.34M
        ans += der[punt];
144
3.34M
        punt++;
145
3.34M
      }
146
1.88M
  }
147
121k
      else
148
121k
  {     /* indefinite length method */
149
121k
    *len = punt;
150
121k
    return -1;
151
121k
  }
152
153
1.88M
      *len = punt;
154
1.88M
    }
155
156
12.0M
  sum = ans;
157
12.0M
  if (ans >= INT_MAX || INT_ADD_OVERFLOW (sum, (*len)))
158
1.60k
    return -2;
159
12.0M
  sum += *len;
160
161
12.0M
  if (sum > der_len)
162
14.2k
    return -4;
163
164
11.9M
  return ans;
165
12.0M
}
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
12.2M
{
183
12.2M
  unsigned int ris;
184
12.2M
  int punt;
185
186
12.2M
  if (der == NULL || der_len < 2 || len == NULL)
187
2.07k
    return ASN1_DER_ERROR;
188
189
12.2M
  *cls = der[0] & 0xE0;
190
12.2M
  if ((der[0] & 0x1F) != 0x1F)
191
12.1M
    {
192
      /* short form */
193
12.1M
      *len = 1;
194
12.1M
      ris = der[0] & 0x1F;
195
12.1M
    }
196
100k
  else
197
100k
    {
198
      /* Long form */
199
100k
      punt = 1;
200
100k
      ris = 0;
201
235M
      while (punt < der_len && der[punt] & 128)
202
235M
  {
203
204
235M
    if (INT_MULTIPLY_OVERFLOW (ris, 128))
205
907
      return ASN1_DER_ERROR;
206
235M
    ris *= 128;
207
208
235M
    if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
209
0
      return ASN1_DER_ERROR;
210
235M
    ris += (der[punt] & 0x7F);
211
235M
    punt++;
212
235M
  }
213
214
99.3k
      if (punt >= der_len)
215
1.21k
  return ASN1_DER_ERROR;
216
217
98.1k
      if (INT_MULTIPLY_OVERFLOW (ris, 128))
218
1.49k
  return ASN1_DER_ERROR;
219
96.6k
      ris *= 128;
220
221
96.6k
      if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
222
0
  return ASN1_DER_ERROR;
223
96.6k
      ris += (der[punt] & 0x7F);
224
96.6k
      punt++;
225
226
96.6k
      *len = punt;
227
96.6k
    }
228
229
12.2M
  if (tag)
230
12.2M
    *tag = ris;
231
12.2M
  return ASN1_SUCCESS;
232
12.2M
}
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
87.9k
{
252
87.9k
  int ret;
253
87.9k
  long err;
254
255
87.9k
  ret = asn1_get_length_der (ber, ber_len, len);
256
257
87.9k
  if (ret == -1 && ber_len > 1)
258
2.61k
    {       /* indefinite length method */
259
2.61k
      err = _asn1_get_indefinite_length_string (ber + 1, ber_len - 1, &ret);
260
2.61k
      if (err != ASN1_SUCCESS)
261
652
  return -3;
262
2.61k
    }
263
264
87.3k
  return ret;
265
87.9k
}
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
1.53M
{
287
1.53M
  int len_len = 0;
288
289
1.53M
  if (der_len <= 0)
290
0
    return ASN1_GENERIC_ERROR;
291
292
1.53M
  *str_len = asn1_get_length_der (der, der_len, &len_len);
293
294
1.53M
  if (*str_len < 0)
295
0
    return ASN1_DER_ERROR;
296
297
1.53M
  *ret_len = *str_len + len_len;
298
1.53M
  if (str_size >= *str_len)
299
825k
    {
300
825k
      if (*str_len > 0 && str != NULL)
301
819k
  memcpy (str, der + len_len, *str_len);
302
825k
    }
303
712k
  else
304
712k
    {
305
712k
      return ASN1_MEM_ERROR;
306
712k
    }
307
308
825k
  return ASN1_SUCCESS;
309
1.53M
}
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
157k
{
332
157k
  int len_len, str_len;
333
157k
  unsigned i;
334
157k
  unsigned sign_count = 0;
335
157k
  unsigned dot_count = 0;
336
157k
  const unsigned char *p;
337
338
157k
  if (der_len <= 0 || str == NULL)
339
18
    return ASN1_DER_ERROR;
340
341
157k
  str_len = asn1_get_length_der (der, der_len, &len_len);
342
157k
  if (str_len <= 0 || str_size < str_len)
343
1.06k
    return ASN1_DER_ERROR;
344
345
  /* perform some sanity checks on the data */
346
156k
  if (str_len < 8)
347
44
    {
348
44
      warn ();
349
44
      return ASN1_TIME_ENCODING_ERROR;
350
44
    }
351
352
156k
  if ((flags & ASN1_DECODE_FLAG_STRICT_DER)
353
156k
      && !(flags & ASN1_DECODE_FLAG_ALLOW_INCORRECT_TIME))
354
156k
    {
355
156k
      p = &der[len_len];
356
2.12M
      for (i = 0; i < (unsigned) (str_len - 1); i++)
357
1.96M
  {
358
1.96M
    if (c_isdigit (p[i]) == 0)
359
2.32k
      {
360
2.32k
        if (type == ASN1_ETYPE_GENERALIZED_TIME)
361
2.25k
    {
362
      /* tolerate lax encodings */
363
2.25k
      if (p[i] == '.' && dot_count == 0)
364
2.05k
        {
365
2.05k
          dot_count++;
366
2.05k
          continue;
367
2.05k
        }
368
369
      /* This is not really valid DER, but there are
370
       * structures using that */
371
197
      if (!(flags & ASN1_DECODE_FLAG_STRICT_DER) &&
372
197
          (p[i] == '+' || p[i] == '-') && sign_count == 0)
373
0
        {
374
0
          sign_count++;
375
0
          continue;
376
0
        }
377
197
    }
378
379
270
        warn ();
380
270
        return ASN1_TIME_ENCODING_ERROR;
381
2.32k
      }
382
1.96M
  }
383
384
155k
      if (sign_count == 0 && p[str_len - 1] != 'Z')
385
98
  {
386
98
    warn ();
387
98
    return ASN1_TIME_ENCODING_ERROR;
388
98
  }
389
155k
    }
390
155k
  memcpy (str, der + len_len, str_len);
391
155k
  str[str_len] = 0;
392
155k
  *ret_len = str_len + len_len;
393
394
155k
  return ASN1_SUCCESS;
395
156k
}
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.27M
{
414
1.27M
  int len_len, len, k;
415
1.27M
  int leading, parsed;
416
1.27M
  char temp[LTOSTR_MAX_SIZE];
417
1.27M
  uint64_t val, val1, val0;
418
419
1.27M
  *ret_len = 0;
420
1.27M
  if (str && str_size > 0)
421
1.27M
    str[0] = 0;     /* no oid */
422
423
1.27M
  if (str == NULL || der_len <= 0)
424
46
    return ASN1_GENERIC_ERROR;
425
426
1.27M
  len = asn1_get_length_der (der, der_len, &len_len);
427
428
1.27M
  if (len <= 0 || len + len_len > der_len)
429
1.52k
    return ASN1_DER_ERROR;
430
431
  /* leading octet can never be 0x80 */
432
1.27M
  if (der[len_len] == 0x80)
433
53
    return ASN1_DER_ERROR;
434
435
1.27M
  val0 = 0;
436
437
1.39M
  for (k = 0; k < len; k++)
438
1.37M
    {
439
1.37M
      if (INT_LEFT_SHIFT_OVERFLOW (val0, 7))
440
152
  return ASN1_DER_ERROR;
441
442
1.37M
      val0 <<= 7;
443
1.37M
      val0 |= der[len_len + k] & 0x7F;
444
1.37M
      if (!(der[len_len + k] & 0x80))
445
1.26M
  break;
446
1.37M
    }
447
1.27M
  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.27M
  val = 0;
454
1.27M
  val1 = val0;
455
1.27M
  if (val1 > 39)
456
1.23M
    {
457
1.23M
      val = 1;
458
1.23M
      val1 = val0 - 40;
459
1.23M
      if (val1 > 39)
460
778k
  {
461
778k
    val = 2;
462
778k
    val1 = val0 - 80;
463
778k
  }
464
1.23M
    }
465
466
1.27M
  _asn1_str_cpy (str, str_size, _asn1_ltostr (val, temp));
467
1.27M
  _asn1_str_cat (str, str_size, ".");
468
1.27M
  _asn1_str_cat (str, str_size, _asn1_ltostr (val1, temp));
469
470
1.27M
  val = 0;
471
1.27M
  leading = 1;
472
19.2M
  for (k = parsed; k < len; k++)
473
18.0M
    {
474
      /* X.690 mandates that the leading byte must never be 0x80
475
       */
476
18.0M
      if (leading != 0 && der[len_len + k] == 0x80)
477
119
  return ASN1_DER_ERROR;
478
18.0M
      leading = 0;
479
480
      /* check for wrap around */
481
18.0M
      if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
482
311
  return ASN1_DER_ERROR;
483
484
18.0M
      val = val << 7;
485
18.0M
      val |= der[len_len + k] & 0x7F;
486
487
18.0M
      if (!(der[len_len + k] & 0x80))
488
16.6M
  {
489
16.6M
    _asn1_str_cat (str, str_size, ".");
490
16.6M
    _asn1_str_cat (str, str_size, _asn1_ltostr (val, temp));
491
16.6M
    val = 0;
492
16.6M
    leading = 1;
493
16.6M
  }
494
18.0M
    }
495
496
1.27M
  if (INT_ADD_OVERFLOW (len, len_len))
497
0
    return ASN1_DER_ERROR;
498
499
1.27M
  *ret_len = len + len_len;
500
501
1.27M
  return ASN1_SUCCESS;
502
1.27M
}
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
284k
{
522
284k
  int len_len = 0, len_byte;
523
524
284k
  if (der_len <= 0)
525
0
    return ASN1_GENERIC_ERROR;
526
527
284k
  len_byte = asn1_get_length_der (der, der_len, &len_len) - 1;
528
284k
  if (len_byte < 0)
529
1.53k
    return ASN1_DER_ERROR;
530
531
282k
  *ret_len = len_byte + len_len + 1;
532
282k
  *bit_len = len_byte * 8 - der[len_len];
533
534
282k
  if (*bit_len < 0)
535
824
    return ASN1_DER_ERROR;
536
537
281k
  if (str_size >= len_byte)
538
158k
    {
539
158k
      if (len_byte > 0 && str)
540
158k
  memcpy (str, der + len_len + 1, len_byte);
541
158k
    }
542
123k
  else
543
123k
    {
544
123k
      return ASN1_MEM_ERROR;
545
123k
    }
546
547
158k
  return ASN1_SUCCESS;
548
281k
}
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
9.15M
{
557
9.15M
  asn1_node p;
558
9.15M
  int counter, len2, len3, is_tag_implicit;
559
9.15M
  int result;
560
9.15M
  unsigned long tag, tag_implicit = 0;
561
9.15M
  unsigned char class, class2, class_implicit = 0;
562
563
9.15M
  if (der_len <= 0)
564
3.21k
    return ASN1_GENERIC_ERROR;
565
566
9.14M
  counter = is_tag_implicit = 0;
567
568
9.14M
  if (node->type & CONST_TAG)
569
871k
    {
570
871k
      p = node->down;
571
2.07M
      while (p)
572
1.24M
  {
573
1.24M
    if (type_field (p->type) == ASN1_ETYPE_TAG)
574
885k
      {
575
885k
        if (p->type & CONST_APPLICATION)
576
0
    class2 = ASN1_CLASS_APPLICATION;
577
885k
        else if (p->type & CONST_UNIVERSAL)
578
0
    class2 = ASN1_CLASS_UNIVERSAL;
579
885k
        else if (p->type & CONST_PRIVATE)
580
0
    class2 = ASN1_CLASS_PRIVATE;
581
885k
        else
582
885k
    class2 = ASN1_CLASS_CONTEXT_SPECIFIC;
583
584
885k
        if (p->type & CONST_EXPLICIT)
585
307k
    {
586
307k
      if (asn1_get_tag_der
587
307k
          (der + counter, der_len, &class, &len2,
588
307k
           &tag) != ASN1_SUCCESS)
589
915
        return ASN1_DER_ERROR;
590
591
306k
      DECR_LEN (der_len, len2);
592
306k
      counter += len2;
593
594
306k
      if (flags & ASN1_DECODE_FLAG_STRICT_DER)
595
266k
        len3 =
596
266k
          asn1_get_length_der (der + counter, der_len, &len2);
597
39.8k
      else
598
39.8k
        len3 =
599
39.8k
          asn1_get_length_ber (der + counter, der_len, &len2);
600
306k
      if (len3 < 0)
601
4.77k
        return ASN1_DER_ERROR;
602
603
301k
      DECR_LEN (der_len, len2);
604
301k
      counter += len2;
605
606
301k
      if (!is_tag_implicit)
607
301k
        {
608
301k
          if ((class != (class2 | ASN1_CLASS_STRUCTURED)) ||
609
301k
        (tag != strtoul ((char *) p->value, NULL, 10)))
610
41.0k
      return ASN1_TAG_ERROR;
611
301k
        }
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
260k
      is_tag_implicit = 0;
618
260k
    }
619
578k
        else
620
578k
    {   /* ASN1_TAG_IMPLICIT */
621
578k
      if (!is_tag_implicit)
622
578k
        {
623
578k
          if ((type_field (node->type) == ASN1_ETYPE_SEQUENCE) ||
624
578k
        (type_field (node->type) == ASN1_ETYPE_SEQUENCE_OF)
625
578k
        || (type_field (node->type) == ASN1_ETYPE_SET)
626
578k
        || (type_field (node->type) == ASN1_ETYPE_SET_OF))
627
76.3k
      class2 |= ASN1_CLASS_STRUCTURED;
628
578k
          class_implicit = class2;
629
578k
          tag_implicit = strtoul ((char *) p->value, NULL, 10);
630
578k
          is_tag_implicit = 1;
631
578k
        }
632
578k
    }
633
885k
      }
634
1.20M
    p = p->right;
635
1.20M
  }
636
871k
    }
637
638
9.10M
  if (is_tag_implicit)
639
578k
    {
640
578k
      if (asn1_get_tag_der
641
578k
    (der + counter, der_len, &class, &len2, &tag) != ASN1_SUCCESS)
642
2.12k
  return ASN1_DER_ERROR;
643
644
576k
      DECR_LEN (der_len, len2);
645
646
576k
      if ((class != class_implicit) || (tag != tag_implicit))
647
382k
  {
648
382k
    if (type_field (node->type) == ASN1_ETYPE_OCTET_STRING)
649
12.9k
      {
650
12.9k
        class_implicit |= ASN1_CLASS_STRUCTURED;
651
12.9k
        if ((class != class_implicit) || (tag != tag_implicit))
652
12.6k
    return ASN1_TAG_ERROR;
653
12.9k
      }
654
369k
    else
655
369k
      return ASN1_TAG_ERROR;
656
382k
  }
657
576k
    }
658
8.52M
  else
659
8.52M
    {
660
8.52M
      unsigned type = type_field (node->type);
661
8.52M
      if (type == ASN1_ETYPE_TAG)
662
4.49k
  {
663
4.49k
    *tag_len = 0;
664
4.49k
    if (inner_tag_len)
665
4.49k
      *inner_tag_len = 0;
666
4.49k
    return ASN1_SUCCESS;
667
4.49k
  }
668
669
8.51M
      if (asn1_get_tag_der
670
8.51M
    (der + counter, der_len, &class, &len2, &tag) != ASN1_SUCCESS)
671
1.82k
  return ASN1_DER_ERROR;
672
673
8.51M
      DECR_LEN (der_len, len2);
674
675
8.51M
      switch (type)
676
8.51M
  {
677
0
  case ASN1_ETYPE_NULL:
678
507k
  case ASN1_ETYPE_BOOLEAN:
679
1.38M
  case ASN1_ETYPE_INTEGER:
680
1.38M
  case ASN1_ETYPE_ENUMERATED:
681
2.72M
  case ASN1_ETYPE_OBJECT_ID:
682
2.72M
  case ASN1_ETYPE_GENERALSTRING:
683
2.72M
  case ASN1_ETYPE_NUMERIC_STRING:
684
2.72M
  case ASN1_ETYPE_IA5_STRING:
685
2.78M
  case ASN1_ETYPE_TELETEX_STRING:
686
2.89M
  case ASN1_ETYPE_PRINTABLE_STRING:
687
2.92M
  case ASN1_ETYPE_UNIVERSAL_STRING:
688
2.96M
  case ASN1_ETYPE_BMP_STRING:
689
2.98M
  case ASN1_ETYPE_UTF8_STRING:
690
2.98M
  case ASN1_ETYPE_VISIBLE_STRING:
691
3.18M
  case ASN1_ETYPE_BIT_STRING:
692
5.29M
  case ASN1_ETYPE_SEQUENCE:
693
5.93M
  case ASN1_ETYPE_SEQUENCE_OF:
694
5.93M
  case ASN1_ETYPE_SET:
695
6.43M
  case ASN1_ETYPE_SET_OF:
696
6.60M
  case ASN1_ETYPE_GENERALIZED_TIME:
697
6.95M
  case ASN1_ETYPE_UTC_TIME:
698
6.95M
    if ((class != _asn1_tags[type].class)
699
6.95M
        || (tag != _asn1_tags[type].tag))
700
607k
      return ASN1_DER_ERROR;
701
6.34M
    break;
702
703
6.34M
  case ASN1_ETYPE_OCTET_STRING:
704
    /* OCTET STRING is handled differently to allow
705
     * BER encodings (structured class). */
706
638k
    if (((class != ASN1_CLASS_UNIVERSAL)
707
638k
         && (class != (ASN1_CLASS_UNIVERSAL | ASN1_CLASS_STRUCTURED)))
708
638k
        || (tag != ASN1_TAG_OCTET_STRING))
709
25.1k
      return ASN1_DER_ERROR;
710
613k
    break;
711
921k
  case ASN1_ETYPE_ANY:
712
921k
    counter -= len2;
713
921k
    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
8.51M
  }
721
8.51M
    }
722
723
8.07M
  counter += len2;
724
8.07M
  *tag_len = counter;
725
8.07M
  if (inner_tag_len)
726
7.61M
    *inner_tag_len = len2;
727
8.07M
  return ASN1_SUCCESS;
728
729
0
cleanup:
730
0
  return result;
731
9.10M
}
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
9.15M
{
738
9.15M
  asn1_node p;
739
9.15M
  int ris = ASN1_DER_ERROR;
740
741
9.15M
  if (type_field (node->type) == ASN1_ETYPE_CHOICE)
742
462k
    {
743
462k
      p = node->down;
744
462k
      while (p)
745
462k
  {
746
462k
    ris =
747
462k
      _asn1_extract_tag_der (p, der, der_len, ret_len, inner_len,
748
462k
           flags);
749
462k
    if (ris == ASN1_SUCCESS)
750
462k
      break;
751
0
    p = p->right;
752
0
  }
753
754
462k
      *ret_len = 0;
755
462k
      return ris;
756
462k
    }
757
8.68M
  else
758
8.68M
    return _asn1_extract_tag_der (node, der, der_len, ret_len, inner_len,
759
8.68M
          flags);
760
9.15M
}
761
762
static int
763
_asn1_delete_not_used (asn1_node node)
764
506k
{
765
506k
  asn1_node p, p2;
766
767
506k
  if (node == NULL)
768
0
    return ASN1_ELEMENT_NOT_FOUND;
769
770
506k
  p = node;
771
13.0M
  while (p)
772
12.5M
    {
773
12.5M
      if (p->type & CONST_NOT_USED)
774
137k
  {
775
137k
    p2 = NULL;
776
137k
    if (p != node)
777
137k
      {
778
137k
        p2 = _asn1_find_left (p);
779
137k
        if (!p2)
780
1.40k
    p2 = _asn1_find_up (p);
781
137k
      }
782
137k
    asn1_delete_structure (&p);
783
137k
    p = p2;
784
137k
  }
785
786
12.5M
      if (!p)
787
0
  break;     /* reach node */
788
789
12.5M
      if (p->down)
790
5.70M
  {
791
5.70M
    p = p->down;
792
5.70M
  }
793
6.87M
      else
794
6.87M
  {
795
6.87M
    if (p == node)
796
54.0k
      p = NULL;
797
6.82M
    else if (p->right)
798
2.95M
      p = p->right;
799
3.86M
    else
800
3.86M
      {
801
5.69M
        while (1)
802
5.69M
    {
803
5.69M
      p = _asn1_find_up (p);
804
5.69M
      if (p == node)
805
452k
        {
806
452k
          p = NULL;
807
452k
          break;
808
452k
        }
809
5.24M
      if (p->right)
810
3.41M
        {
811
3.41M
          p = p->right;
812
3.41M
          break;
813
3.41M
        }
814
5.24M
    }
815
3.86M
      }
816
6.87M
  }
817
12.5M
    }
818
506k
  return ASN1_SUCCESS;
819
506k
}
820
821
static int
822
_asn1_get_indefinite_length_string (const unsigned char *der,
823
            int der_len, int *len)
824
3.90k
{
825
3.90k
  int len2, len3, counter, indefinite;
826
3.90k
  int result;
827
3.90k
  unsigned long tag;
828
3.90k
  unsigned char class;
829
830
3.90k
  counter = indefinite = 0;
831
832
1.61M
  while (1)
833
1.61M
    {
834
1.61M
      if (HAVE_TWO (der_len) && (der[counter] == 0)
835
1.61M
    && (der[counter + 1] == 0))
836
4.40k
  {
837
4.40k
    counter += 2;
838
4.40k
    DECR_LEN (der_len, 2);
839
840
4.40k
    indefinite--;
841
4.40k
    if (indefinite <= 0)
842
3.20k
      break;
843
1.19k
    else
844
1.19k
      continue;
845
4.40k
  }
846
847
1.61M
      if (asn1_get_tag_der
848
1.61M
    (der + counter, der_len, &class, &len2, &tag) != ASN1_SUCCESS)
849
273
  return ASN1_DER_ERROR;
850
851
1.61M
      DECR_LEN (der_len, len2);
852
1.61M
      counter += len2;
853
854
1.61M
      len2 = asn1_get_length_der (der + counter, der_len, &len3);
855
1.61M
      if (len2 < -1)
856
430
  return ASN1_DER_ERROR;
857
858
1.61M
      if (len2 == -1)
859
100k
  {
860
100k
    indefinite++;
861
100k
    counter += 1;
862
100k
    DECR_LEN (der_len, 1);
863
100k
  }
864
1.51M
      else
865
1.51M
  {
866
1.51M
    counter += len2 + len3;
867
1.51M
    DECR_LEN (der_len, len2 + len3);
868
1.51M
  }
869
1.61M
    }
870
871
3.20k
  *len = counter;
872
3.20k
  return ASN1_SUCCESS;
873
874
0
cleanup:
875
0
  return result;
876
3.90k
}
877
878
static void
879
delete_unneeded_choice_fields (asn1_node p)
880
462k
{
881
462k
  asn1_node p2;
882
883
944k
  while (p->right)
884
482k
    {
885
482k
      p2 = p->right;
886
482k
      asn1_delete_structure (&p2);
887
482k
    }
888
462k
}
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
647k
{
921
647k
  asn1_node node, p, p2, p3;
922
647k
  char temp[128];
923
647k
  int counter, len2, len3, len4, move, ris, tlen;
924
647k
  struct node_tail_cache_st tcache = { NULL, NULL };
925
647k
  unsigned char class;
926
647k
  unsigned long tag;
927
647k
  int tag_len;
928
647k
  int indefinite, result, total_len = *max_ider_len, ider_len = *max_ider_len;
929
647k
  int inner_tag_len;
930
647k
  unsigned char *ptmp;
931
647k
  const unsigned char *ptag;
932
647k
  const unsigned char *der = ider;
933
934
647k
  node = *element;
935
936
647k
  if (errorDescription != NULL)
937
84.3k
    errorDescription[0] = 0;
938
939
647k
  if (node == NULL)
940
0
    return ASN1_ELEMENT_NOT_FOUND;
941
942
647k
  if (node->type & CONST_OPTION)
943
0
    {
944
0
      result = ASN1_GENERIC_ERROR;
945
0
      warn ();
946
0
      goto cleanup;
947
0
    }
948
949
647k
  counter = 0;
950
647k
  move = DOWN;
951
647k
  p = node;
952
12.6M
  while (1)
953
12.6M
    {
954
12.6M
      tag_len = 0;
955
12.6M
      inner_tag_len = 0;
956
12.6M
      ris = ASN1_SUCCESS;
957
12.6M
      if (move != UP)
958
8.43M
  {
959
8.43M
    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
8.43M
    p->start = counter;
1015
8.43M
    p->end = total_len - 1;
1016
1017
8.43M
    if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1018
1.25M
      {
1019
1.25M
        p2 = _asn1_find_up (p);
1020
1.25M
        len2 = p2->tmp_ival;
1021
1.25M
        if (counter == len2)
1022
188k
    {
1023
188k
      if (p->right)
1024
9.50k
        {
1025
9.50k
          p2 = p->right;
1026
9.50k
          move = RIGHT;
1027
9.50k
        }
1028
178k
      else
1029
178k
        move = UP;
1030
1031
188k
      if (p->type & CONST_OPTION)
1032
187k
        asn1_delete_structure (&p);
1033
1034
188k
      p = p2;
1035
188k
      continue;
1036
188k
    }
1037
1.25M
      }
1038
1039
8.24M
    if (type_field (p->type) == ASN1_ETYPE_CHOICE)
1040
472k
      {
1041
928k
        while (p->down)
1042
918k
    {
1043
918k
      ris =
1044
918k
        extract_tag_der_recursive (p->down, der + counter,
1045
918k
                 ider_len, &len2, NULL, flags);
1046
1047
918k
      if (ris == ASN1_SUCCESS)
1048
462k
        {
1049
462k
          delete_unneeded_choice_fields (p->down);
1050
462k
          break;
1051
462k
        }
1052
455k
      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
455k
      else
1059
455k
        {
1060
455k
          p2 = p->down;
1061
455k
          asn1_delete_structure (&p2);
1062
455k
        }
1063
918k
    }
1064
1065
472k
        if (p->down == NULL)
1066
10.3k
    {
1067
10.3k
      if (!(p->type & CONST_OPTION))
1068
7.33k
        {
1069
7.33k
          result = ASN1_DER_ERROR;
1070
7.33k
          warn ();
1071
7.33k
          goto cleanup;
1072
7.33k
        }
1073
10.3k
    }
1074
462k
        else if (type_field (p->type) != ASN1_ETYPE_CHOICE)
1075
0
    p = p->down;
1076
1077
465k
        p->start = counter;
1078
465k
      }
1079
1080
8.23M
    if ((p->type & CONST_OPTION) || (p->type & CONST_DEFAULT))
1081
1.06M
      {
1082
1.06M
        p2 = _asn1_find_up (p);
1083
1.06M
        len2 = p2->tmp_ival;
1084
1085
1.06M
        if ((len2 != -1) && (counter > len2))
1086
2.35k
    ris = ASN1_TAG_ERROR;
1087
1.06M
      }
1088
1089
8.23M
    if (ris == ASN1_SUCCESS)
1090
8.23M
      ris =
1091
8.23M
        extract_tag_der_recursive (p, der + counter, ider_len,
1092
8.23M
           &tag_len, &inner_tag_len, flags);
1093
1094
8.23M
    if (ris != ASN1_SUCCESS)
1095
618k
      {
1096
618k
        if (p->type & CONST_OPTION)
1097
152k
    {
1098
152k
      p->type |= CONST_NOT_USED;
1099
152k
      move = RIGHT;
1100
152k
    }
1101
465k
        else if (p->type & CONST_DEFAULT)
1102
354k
    {
1103
354k
      _asn1_set_value (p, NULL, 0);
1104
354k
      move = RIGHT;
1105
354k
    }
1106
111k
        else
1107
111k
    {
1108
111k
      if (errorDescription != NULL)
1109
1.86k
        _asn1_error_description_tag_error (p, errorDescription);
1110
1111
111k
      result = ASN1_TAG_ERROR;
1112
111k
      warn ();
1113
111k
      goto cleanup;
1114
111k
    }
1115
618k
      }
1116
7.62M
    else
1117
7.62M
      {
1118
7.62M
        DECR_LEN (ider_len, tag_len);
1119
7.62M
        counter += tag_len;
1120
7.62M
      }
1121
8.23M
  }
1122
1123
12.2M
      if (ris == ASN1_SUCCESS)
1124
11.7M
  {
1125
11.7M
    switch (type_field (p->type))
1126
11.7M
      {
1127
1.59k
      case ASN1_ETYPE_NULL:
1128
1.59k
        DECR_LEN (ider_len, 1);
1129
1.59k
        if (der[counter])
1130
1
    {
1131
1
      result = ASN1_DER_ERROR;
1132
1
      warn ();
1133
1
      goto cleanup;
1134
1
    }
1135
1.59k
        counter++;
1136
1.59k
        move = RIGHT;
1137
1.59k
        break;
1138
160k
      case ASN1_ETYPE_BOOLEAN:
1139
160k
        DECR_LEN (ider_len, 2);
1140
1141
160k
        if (der[counter++] != 1)
1142
62
    {
1143
62
      result = ASN1_DER_ERROR;
1144
62
      warn ();
1145
62
      goto cleanup;
1146
62
    }
1147
160k
        if (der[counter++] == 0)
1148
8.97k
    _asn1_set_value (p, "F", 1);
1149
151k
        else
1150
151k
    _asn1_set_value (p, "T", 1);
1151
160k
        move = RIGHT;
1152
160k
        break;
1153
801k
      case ASN1_ETYPE_INTEGER:
1154
805k
      case ASN1_ETYPE_ENUMERATED:
1155
805k
        len2 = asn1_get_length_der (der + counter, ider_len, &len3);
1156
805k
        if (len2 < 0)
1157
1.64k
    {
1158
1.64k
      result = ASN1_DER_ERROR;
1159
1.64k
      warn ();
1160
1.64k
      goto cleanup;
1161
1.64k
    }
1162
1163
803k
        DECR_LEN (ider_len, len3 + len2);
1164
1165
803k
        _asn1_set_value (p, der + counter, len3 + len2);
1166
803k
        counter += len3 + len2;
1167
803k
        move = RIGHT;
1168
803k
        break;
1169
1.27M
      case ASN1_ETYPE_OBJECT_ID:
1170
1.27M
        result =
1171
1.27M
    asn1_get_object_id_der (der + counter, ider_len, &len2,
1172
1.27M
          temp, sizeof (temp));
1173
1.27M
        if (result != ASN1_SUCCESS)
1174
2.20k
    {
1175
2.20k
      warn ();
1176
2.20k
      goto cleanup;
1177
2.20k
    }
1178
1179
1.27M
        DECR_LEN (ider_len, len2);
1180
1181
1.27M
        tlen = strlen (temp);
1182
1.27M
        if (tlen > 0)
1183
1.27M
    _asn1_set_value (p, temp, tlen + 1);
1184
1185
1.27M
        counter += len2;
1186
1.27M
        move = RIGHT;
1187
1.27M
        break;
1188
59.0k
      case ASN1_ETYPE_GENERALIZED_TIME:
1189
157k
      case ASN1_ETYPE_UTC_TIME:
1190
157k
        result =
1191
157k
    _asn1_get_time_der (type_field (p->type), der + counter,
1192
157k
            ider_len, &len2, temp, sizeof (temp) - 1,
1193
157k
            flags);
1194
157k
        if (result != ASN1_SUCCESS)
1195
1.49k
    {
1196
1.49k
      warn ();
1197
1.49k
      goto cleanup;
1198
1.49k
    }
1199
1200
155k
        DECR_LEN (ider_len, len2);
1201
1202
155k
        tlen = strlen (temp);
1203
155k
        if (tlen > 0)
1204
155k
    _asn1_set_value (p, temp, tlen);
1205
1206
155k
        counter += len2;
1207
155k
        move = RIGHT;
1208
155k
        break;
1209
607k
      case ASN1_ETYPE_OCTET_STRING:
1210
607k
        if (counter < inner_tag_len)
1211
0
    {
1212
0
      result = ASN1_DER_ERROR;
1213
0
      warn ();
1214
0
      goto cleanup;
1215
0
    }
1216
1217
607k
        ptag = der + counter - inner_tag_len;
1218
607k
        if ((flags & ASN1_DECODE_FLAG_STRICT_DER)
1219
607k
      || !(ptag[0] & ASN1_CLASS_STRUCTURED))
1220
606k
    {
1221
606k
      if (ptag[0] & ASN1_CLASS_STRUCTURED)
1222
141
        {
1223
141
          result = ASN1_DER_ERROR;
1224
141
          warn ();
1225
141
          goto cleanup;
1226
141
        }
1227
1228
606k
      len2 = asn1_get_length_der (der + counter, ider_len, &len3);
1229
606k
      if (len2 < 0)
1230
1.35k
        {
1231
1.35k
          result = ASN1_DER_ERROR;
1232
1.35k
          warn ();
1233
1.35k
          goto cleanup;
1234
1.35k
        }
1235
1236
604k
      DECR_LEN (ider_len, len3 + len2);
1237
1238
604k
      _asn1_set_value (p, der + counter, len3 + len2);
1239
604k
      counter += len3 + len2;
1240
604k
    }
1241
1.33k
        else
1242
1.33k
    {
1243
1.33k
      unsigned dflags = 0, vlen, ber_len;
1244
1245
1.33k
      if (ptag[0] & ASN1_CLASS_STRUCTURED)
1246
1.33k
        dflags |= DECODE_FLAG_CONSTRUCTED;
1247
1248
1.33k
      result =
1249
1.33k
        _asn1_decode_simple_ber (type_field (p->type),
1250
1.33k
               der + counter, ider_len, &ptmp,
1251
1.33k
               &vlen, &ber_len, dflags);
1252
1.33k
      if (result != ASN1_SUCCESS)
1253
636
        {
1254
636
          warn ();
1255
636
          goto cleanup;
1256
636
        }
1257
1258
695
      DECR_LEN (ider_len, ber_len);
1259
1260
695
      _asn1_set_value_lv (p, ptmp, vlen);
1261
1262
695
      counter += ber_len;
1263
695
      free (ptmp);
1264
695
    }
1265
605k
        move = RIGHT;
1266
605k
        break;
1267
0
      case ASN1_ETYPE_GENERALSTRING:
1268
0
      case ASN1_ETYPE_NUMERIC_STRING:
1269
39.9k
      case ASN1_ETYPE_IA5_STRING:
1270
43.4k
      case ASN1_ETYPE_TELETEX_STRING:
1271
72.2k
      case ASN1_ETYPE_PRINTABLE_STRING:
1272
76.3k
      case ASN1_ETYPE_UNIVERSAL_STRING:
1273
86.9k
      case ASN1_ETYPE_BMP_STRING:
1274
88.7k
      case ASN1_ETYPE_UTF8_STRING:
1275
88.7k
      case ASN1_ETYPE_VISIBLE_STRING:
1276
295k
      case ASN1_ETYPE_BIT_STRING:
1277
295k
        len2 = asn1_get_length_der (der + counter, ider_len, &len3);
1278
295k
        if (len2 < 0)
1279
1.76k
    {
1280
1.76k
      result = ASN1_DER_ERROR;
1281
1.76k
      warn ();
1282
1.76k
      goto cleanup;
1283
1.76k
    }
1284
1285
293k
        DECR_LEN (ider_len, len3 + len2);
1286
1287
293k
        _asn1_set_value (p, der + counter, len3 + len2);
1288
293k
        counter += len3 + len2;
1289
293k
        move = RIGHT;
1290
293k
        break;
1291
3.84M
      case ASN1_ETYPE_SEQUENCE:
1292
3.84M
      case ASN1_ETYPE_SET:
1293
3.84M
        if (move == UP)
1294
1.74M
    {
1295
1.74M
      len2 = p->tmp_ival;
1296
1.74M
      p->tmp_ival = 0;
1297
1.74M
      if (len2 == -1)
1298
11.4k
        {   /* indefinite length method */
1299
11.4k
          DECR_LEN (ider_len, 2);
1300
11.3k
          if ((der[counter]) || der[counter + 1])
1301
192
      {
1302
192
        result = ASN1_DER_ERROR;
1303
192
        warn ();
1304
192
        goto cleanup;
1305
192
      }
1306
11.1k
          counter += 2;
1307
11.1k
        }
1308
1.73M
      else
1309
1.73M
        {   /* definite length method */
1310
1.73M
          if (len2 != counter)
1311
6.64k
      {
1312
6.64k
        result = ASN1_DER_ERROR;
1313
6.64k
        warn ();
1314
6.64k
        goto cleanup;
1315
6.64k
      }
1316
1.73M
        }
1317
1.74M
      move = RIGHT;
1318
1.74M
    }
1319
2.09M
        else
1320
2.09M
    {   /* move==DOWN || move==RIGHT */
1321
2.09M
      len3 = asn1_get_length_der (der + counter, ider_len, &len2);
1322
2.09M
      if (IS_ERR (len3, flags))
1323
2.24k
        {
1324
2.24k
          result = ASN1_DER_ERROR;
1325
2.24k
          warn ();
1326
2.24k
          goto cleanup;
1327
2.24k
        }
1328
1329
2.09M
      DECR_LEN (ider_len, len2);
1330
2.09M
      counter += len2;
1331
1332
2.09M
      if (len3 > 0)
1333
1.87M
        {
1334
1.87M
          p->tmp_ival = counter + len3;
1335
1.87M
          move = DOWN;
1336
1.87M
        }
1337
219k
      else if (len3 == 0)
1338
204k
        {
1339
204k
          p2 = p->down;
1340
747k
          while (p2)
1341
542k
      {
1342
542k
        if (type_field (p2->type) != ASN1_ETYPE_TAG)
1343
542k
          {
1344
542k
            p3 = p2->right;
1345
542k
            asn1_delete_structure (&p2);
1346
542k
            p2 = p3;
1347
542k
          }
1348
519
        else
1349
519
          p2 = p2->right;
1350
542k
      }
1351
204k
          move = RIGHT;
1352
204k
        }
1353
15.2k
      else
1354
15.2k
        {   /* indefinite length method */
1355
15.2k
          p->tmp_ival = -1;
1356
15.2k
          move = DOWN;
1357
15.2k
        }
1358
2.09M
    }
1359
3.83M
        break;
1360
3.83M
      case ASN1_ETYPE_SEQUENCE_OF:
1361
2.79M
      case ASN1_ETYPE_SET_OF:
1362
2.79M
        if (move == UP)
1363
1.96M
    {
1364
1.96M
      len2 = p->tmp_ival;
1365
1.96M
      if (len2 == -1)
1366
17.2k
        {   /* indefinite length method */
1367
17.2k
          if (!HAVE_TWO (ider_len)
1368
17.2k
        || ((der[counter]) || der[counter + 1]))
1369
16.5k
      {
1370
16.5k
        result = _asn1_append_sequence_set (p, &tcache);
1371
16.5k
        if (result != 0)
1372
0
          {
1373
0
            warn ();
1374
0
            goto cleanup;
1375
0
          }
1376
16.5k
        p = tcache.tail;
1377
16.5k
        move = RIGHT;
1378
16.5k
        continue;
1379
16.5k
      }
1380
1381
717
          p->tmp_ival = 0;
1382
717
          tcache.tail = NULL; /* finished decoding this structure */
1383
717
          tcache.head = NULL;
1384
717
          DECR_LEN (ider_len, 2);
1385
717
          counter += 2;
1386
717
        }
1387
1.94M
      else
1388
1.94M
        {   /* definite length method */
1389
1.94M
          if (len2 > counter)
1390
1.25M
      {
1391
1.25M
        result = _asn1_append_sequence_set (p, &tcache);
1392
1.25M
        if (result != 0)
1393
0
          {
1394
0
            warn ();
1395
0
            goto cleanup;
1396
0
          }
1397
1.25M
        p = tcache.tail;
1398
1.25M
        move = RIGHT;
1399
1.25M
        continue;
1400
1.25M
      }
1401
1402
692k
          p->tmp_ival = 0;
1403
692k
          tcache.tail = NULL; /* finished decoding this structure */
1404
692k
          tcache.head = NULL;
1405
1406
692k
          if (len2 != counter)
1407
589
      {
1408
589
        result = ASN1_DER_ERROR;
1409
589
        warn ();
1410
589
        goto cleanup;
1411
589
      }
1412
692k
        }
1413
1.96M
    }
1414
829k
        else
1415
829k
    {   /* move==DOWN || move==RIGHT */
1416
829k
      len3 = asn1_get_length_der (der + counter, ider_len, &len2);
1417
829k
      if (IS_ERR (len3, flags))
1418
1.38k
        {
1419
1.38k
          result = ASN1_DER_ERROR;
1420
1.38k
          warn ();
1421
1.38k
          goto cleanup;
1422
1.38k
        }
1423
1424
827k
      DECR_LEN (ider_len, len2);
1425
827k
      counter += len2;
1426
827k
      if (len3)
1427
702k
        {
1428
702k
          if (len3 > 0)
1429
701k
      { /* definite length method */
1430
701k
        p->tmp_ival = counter + len3;
1431
701k
      }
1432
1.02k
          else
1433
1.02k
      { /* indefinite length method */
1434
1.02k
        p->tmp_ival = -1;
1435
1.02k
      }
1436
1437
702k
          p2 = p->down;
1438
702k
          if (p2 == NULL)
1439
0
      {
1440
0
        result = ASN1_DER_ERROR;
1441
0
        warn ();
1442
0
        goto cleanup;
1443
0
      }
1444
1445
783k
          while ((type_field (p2->type) == ASN1_ETYPE_TAG)
1446
783k
           || (type_field (p2->type) == ASN1_ETYPE_SIZE))
1447
80.8k
      p2 = p2->right;
1448
702k
          if (p2->right == NULL)
1449
702k
      {
1450
702k
        result = _asn1_append_sequence_set (p, &tcache);
1451
702k
        if (result != 0)
1452
0
          {
1453
0
            warn ();
1454
0
            goto cleanup;
1455
0
          }
1456
702k
      }
1457
702k
          p = p2;
1458
702k
        }
1459
827k
    }
1460
1.52M
        move = RIGHT;
1461
1.52M
        break;
1462
923k
      case ASN1_ETYPE_ANY:
1463
        /* Check indefinite length method in an EXPLICIT TAG */
1464
1465
923k
        if (!(flags & ASN1_DECODE_FLAG_STRICT_DER)
1466
923k
      && (p->type & CONST_TAG) && tag_len == 2
1467
923k
      && (der[counter - 1] == 0x80))
1468
1.56k
    indefinite = 1;
1469
921k
        else
1470
921k
    indefinite = 0;
1471
1472
923k
        if (asn1_get_tag_der
1473
923k
      (der + counter, ider_len, &class, &len2,
1474
923k
       &tag) != ASN1_SUCCESS)
1475
177
    {
1476
177
      result = ASN1_DER_ERROR;
1477
177
      warn ();
1478
177
      goto cleanup;
1479
177
    }
1480
1481
922k
        DECR_LEN (ider_len, len2);
1482
1483
922k
        len4 =
1484
922k
    asn1_get_length_der (der + counter + len2, ider_len, &len3);
1485
922k
        if (IS_ERR (len4, flags))
1486
1.63k
    {
1487
1.63k
      result = ASN1_DER_ERROR;
1488
1.63k
      warn ();
1489
1.63k
      goto cleanup;
1490
1.63k
    }
1491
921k
        if (len4 != -1)  /* definite */
1492
920k
    {
1493
920k
      len2 += len4;
1494
1495
920k
      DECR_LEN (ider_len, len4 + len3);
1496
920k
      _asn1_set_value_lv (p, der + counter, len2 + len3);
1497
920k
      counter += len2 + len3;
1498
920k
    }
1499
1.29k
        else    /* == -1 */
1500
1.29k
    {   /* indefinite length */
1501
1.29k
      ider_len += len2; /* undo DECR_LEN */
1502
1503
1.29k
      if (counter == 0)
1504
0
        {
1505
0
          result = ASN1_DER_ERROR;
1506
0
          warn ();
1507
0
          goto cleanup;
1508
0
        }
1509
1510
1.29k
      result =
1511
1.29k
        _asn1_get_indefinite_length_string (der + counter,
1512
1.29k
              ider_len, &len2);
1513
1.29k
      if (result != ASN1_SUCCESS)
1514
51
        {
1515
51
          warn ();
1516
51
          goto cleanup;
1517
51
        }
1518
1519
1.23k
      DECR_LEN (ider_len, len2);
1520
1.23k
      _asn1_set_value_lv (p, der + counter, len2);
1521
1.23k
      counter += len2;
1522
1523
1.23k
    }
1524
1525
        /* Check if a couple of 0x00 are present due to an EXPLICIT TAG with
1526
           an indefinite length method. */
1527
921k
        if (indefinite)
1528
1.56k
    {
1529
1.56k
      DECR_LEN (ider_len, 2);
1530
1.52k
      if (!der[counter] && !der[counter + 1])
1531
1.45k
        {
1532
1.45k
          counter += 2;
1533
1.45k
        }
1534
66
      else
1535
66
        {
1536
66
          result = ASN1_DER_ERROR;
1537
66
          warn ();
1538
66
          goto cleanup;
1539
66
        }
1540
1.52k
    }
1541
1542
921k
        move = RIGHT;
1543
921k
        break;
1544
923k
      default:
1545
923k
        move = (move == UP) ? RIGHT : DOWN;
1546
923k
        break;
1547
11.7M
      }
1548
11.7M
  }
1549
1550
11.0M
      if (p)
1551
11.0M
  {
1552
11.0M
    p->end = counter - 1;
1553
11.0M
  }
1554
1555
11.0M
      if (p == node && move != DOWN)
1556
506k
  break;
1557
1558
10.4M
      if (move == DOWN)
1559
2.35M
  {
1560
2.35M
    if (p->down)
1561
2.35M
      p = p->down;
1562
4.49k
    else
1563
4.49k
      move = RIGHT;
1564
2.35M
  }
1565
10.4M
      if ((move == RIGHT) && !(p->type & CONST_SET))
1566
8.14M
  {
1567
8.14M
    if (p->right)
1568
4.15M
      p = p->right;
1569
3.99M
    else
1570
3.99M
      move = UP;
1571
8.14M
  }
1572
10.4M
      if (move == UP)
1573
3.99M
  p = _asn1_find_up (p);
1574
10.4M
    }
1575
1576
506k
  _asn1_delete_not_used (*element);
1577
1578
506k
  if ((ider_len < 0) ||
1579
506k
      (!(flags & ASN1_DECODE_FLAG_ALLOW_PADDING) && (ider_len != 0)))
1580
880
    {
1581
880
      warn ();
1582
880
      result = ASN1_DER_ERROR;
1583
880
      goto cleanup;
1584
880
    }
1585
1586
505k
  *max_ider_len = total_len - ider_len;
1587
1588
505k
  return ASN1_SUCCESS;
1589
1590
141k
cleanup:
1591
141k
  asn1_delete_structure (element);
1592
141k
  return result;
1593
506k
}
1594
1595
1596
/**
1597
 * asn1_der_decoding:
1598
 * @element: pointer to an ASN1 structure.
1599
 * @ider: vector that contains the DER encoding.
1600
 * @ider_len: number of bytes of *@ider: @ider[0]..@ider[len-1].
1601
 * @errorDescription: null-terminated string contains details when an
1602
 *   error occurred.
1603
 *
1604
 * Fill the structure *@element with values of a DER encoding
1605
 * string. The structure must just be created with function
1606
 * asn1_create_element().
1607
 *
1608
 * Note that the *@element variable is provided as a pointer for
1609
 * historical reasons.
1610
 *
1611
 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1612
 *   if @ELEMENT is %NULL, and %ASN1_TAG_ERROR or
1613
 *   %ASN1_DER_ERROR if the der encoding doesn't match the structure
1614
 *   name (*@ELEMENT deleted).
1615
 **/
1616
int
1617
asn1_der_decoding (asn1_node *element, const void *ider, int ider_len,
1618
       char *errorDescription)
1619
132k
{
1620
132k
  return asn1_der_decoding2 (element, ider, &ider_len, 0, errorDescription);
1621
132k
}
1622
1623
/**
1624
 * asn1_der_decoding_element:
1625
 * @structure: pointer to an ASN1 structure
1626
 * @elementName: name of the element to fill
1627
 * @ider: vector that contains the DER encoding of the whole structure.
1628
 * @len: number of bytes of *der: der[0]..der[len-1]
1629
 * @errorDescription: null-terminated string contains details when an
1630
 *   error occurred.
1631
 *
1632
 * Fill the element named @ELEMENTNAME with values of a DER encoding
1633
 * string.  The structure must just be created with function
1634
 * asn1_create_element().  The DER vector must contain the encoding
1635
 * string of the whole @STRUCTURE.  If an error occurs during the
1636
 * decoding procedure, the *@STRUCTURE is deleted and set equal to
1637
 * %NULL.
1638
 *
1639
 * This function is deprecated and may just be an alias to asn1_der_decoding
1640
 * in future versions. Use asn1_der_decoding() instead.
1641
 *
1642
 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1643
 *   if ELEMENT is %NULL or @elementName == NULL, and
1644
 *   %ASN1_TAG_ERROR or %ASN1_DER_ERROR if the der encoding doesn't
1645
 *   match the structure @structure (*ELEMENT deleted).
1646
 **/
1647
int
1648
asn1_der_decoding_element (asn1_node *structure, const char *elementName,
1649
         const void *ider, int len, char *errorDescription)
1650
0
{
1651
0
  return asn1_der_decoding (structure, ider, len, errorDescription);
1652
0
}
1653
1654
/**
1655
 * asn1_der_decoding_startEnd:
1656
 * @element: pointer to an ASN1 element
1657
 * @ider: vector that contains the DER encoding.
1658
 * @ider_len: number of bytes of *@ider: @ider[0]..@ider[len-1]
1659
 * @name_element: an element of NAME structure.
1660
 * @start: the position of the first byte of NAME_ELEMENT decoding
1661
 *   (@ider[*start])
1662
 * @end: the position of the last byte of NAME_ELEMENT decoding
1663
 *  (@ider[*end])
1664
 *
1665
 * Find the start and end point of an element in a DER encoding
1666
 * string. I mean that if you have a der encoding and you have already
1667
 * used the function asn1_der_decoding() to fill a structure, it may
1668
 * happen that you want to find the piece of string concerning an
1669
 * element of the structure.
1670
 *
1671
 * One example is the sequence "tbsCertificate" inside an X509
1672
 * certificate.
1673
 *
1674
 * Note that since libtasn1 3.7 the @ider and @ider_len parameters
1675
 * can be omitted, if the element is already decoded using asn1_der_decoding().
1676
 *
1677
 * Returns: %ASN1_SUCCESS if DER encoding OK, %ASN1_ELEMENT_NOT_FOUND
1678
 *   if ELEMENT is %asn1_node EMPTY or @name_element is not a valid
1679
 *   element, %ASN1_TAG_ERROR or %ASN1_DER_ERROR if the der encoding
1680
 *   doesn't match the structure ELEMENT.
1681
 **/
1682
int
1683
asn1_der_decoding_startEnd (asn1_node element, const void *ider, int ider_len,
1684
          const char *name_element, int *start, int *end)
1685
225k
{
1686
225k
  asn1_node node, node_to_find;
1687
225k
  int result = ASN1_DER_ERROR;
1688
1689
225k
  node = element;
1690
1691
225k
  if (node == NULL)
1692
0
    return ASN1_ELEMENT_NOT_FOUND;
1693
1694
225k
  node_to_find = asn1_find_node (node, name_element);
1695
1696
225k
  if (node_to_find == NULL)
1697
521
    return ASN1_ELEMENT_NOT_FOUND;
1698
1699
224k
  *start = node_to_find->start;
1700
224k
  *end = node_to_find->end;
1701
1702
224k
  if (*start == 0 && *end == 0)
1703
0
    {
1704
0
      if (ider == NULL || ider_len == 0)
1705
0
  return ASN1_GENERIC_ERROR;
1706
1707
      /* it seems asn1_der_decoding() wasn't called before. Do it now */
1708
0
      result = asn1_der_decoding (&node, ider, ider_len, NULL);
1709
0
      if (result != ASN1_SUCCESS)
1710
0
  {
1711
0
    warn ();
1712
0
    return result;
1713
0
  }
1714
1715
0
      node_to_find = asn1_find_node (node, name_element);
1716
0
      if (node_to_find == NULL)
1717
0
  return ASN1_ELEMENT_NOT_FOUND;
1718
1719
0
      *start = node_to_find->start;
1720
0
      *end = node_to_find->end;
1721
0
    }
1722
1723
224k
  if (*end < *start)
1724
0
    return ASN1_GENERIC_ERROR;
1725
1726
224k
  return ASN1_SUCCESS;
1727
224k
}
1728
1729
/**
1730
 * asn1_expand_any_defined_by:
1731
 * @definitions: ASN1 definitions
1732
 * @element: pointer to an ASN1 structure
1733
 *
1734
 * Expands every "ANY DEFINED BY" element of a structure created from
1735
 * a DER decoding process (asn1_der_decoding function). The element
1736
 * ANY must be defined by an OBJECT IDENTIFIER. The type used to
1737
 * expand the element ANY is the first one following the definition of
1738
 * the actual value of the OBJECT IDENTIFIER.
1739
 *
1740
 * Returns: %ASN1_SUCCESS if Substitution OK, %ASN1_ERROR_TYPE_ANY if
1741
 *   some "ANY DEFINED BY" element couldn't be expanded due to a
1742
 *   problem in OBJECT_ID -> TYPE association, or other error codes
1743
 *   depending on DER decoding.
1744
 **/
1745
int
1746
asn1_expand_any_defined_by (asn1_node_const definitions, asn1_node *element)
1747
0
{
1748
0
  char name[2 * ASN1_MAX_NAME_SIZE + 2], value[ASN1_MAX_NAME_SIZE];
1749
0
  int retCode = ASN1_SUCCESS, result;
1750
0
  int len, len2, len3;
1751
0
  asn1_node_const p2;
1752
0
  asn1_node p, p3, aux = NULL;
1753
0
  char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
1754
0
  const char *definitionsName;
1755
1756
0
  if ((definitions == NULL) || (*element == NULL))
1757
0
    return ASN1_ELEMENT_NOT_FOUND;
1758
1759
0
  definitionsName = definitions->name;
1760
1761
0
  p = *element;
1762
0
  while (p)
1763
0
    {
1764
1765
0
      switch (type_field (p->type))
1766
0
  {
1767
0
  case ASN1_ETYPE_ANY:
1768
0
    if ((p->type & CONST_DEFINED_BY) && (p->value))
1769
0
      {
1770
        /* search the "DEF_BY" element */
1771
0
        p2 = p->down;
1772
0
        while ((p2) && (type_field (p2->type) != ASN1_ETYPE_CONSTANT))
1773
0
    p2 = p2->right;
1774
1775
0
        if (!p2)
1776
0
    {
1777
0
      retCode = ASN1_ERROR_TYPE_ANY;
1778
0
      break;
1779
0
    }
1780
1781
0
        p3 = _asn1_find_up (p);
1782
1783
0
        if (!p3)
1784
0
    {
1785
0
      retCode = ASN1_ERROR_TYPE_ANY;
1786
0
      break;
1787
0
    }
1788
1789
0
        p3 = p3->down;
1790
0
        while (p3)
1791
0
    {
1792
0
      if (!(strcmp (p3->name, p2->name)))
1793
0
        break;
1794
0
      p3 = p3->right;
1795
0
    }
1796
1797
0
        if ((!p3) || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID) ||
1798
0
      (p3->value == NULL))
1799
0
    {
1800
1801
0
      p3 = _asn1_find_up (p);
1802
0
      p3 = _asn1_find_up (p3);
1803
1804
0
      if (!p3)
1805
0
        {
1806
0
          retCode = ASN1_ERROR_TYPE_ANY;
1807
0
          break;
1808
0
        }
1809
1810
0
      p3 = p3->down;
1811
1812
0
      while (p3)
1813
0
        {
1814
0
          if (!(strcmp (p3->name, p2->name)))
1815
0
      break;
1816
0
          p3 = p3->right;
1817
0
        }
1818
1819
0
      if ((!p3) || (type_field (p3->type) != ASN1_ETYPE_OBJECT_ID)
1820
0
          || (p3->value == NULL))
1821
0
        {
1822
0
          retCode = ASN1_ERROR_TYPE_ANY;
1823
0
          break;
1824
0
        }
1825
0
    }
1826
1827
        /* search the OBJECT_ID into definitions */
1828
0
        p2 = definitions->down;
1829
0
        while (p2)
1830
0
    {
1831
0
      if ((type_field (p2->type) == ASN1_ETYPE_OBJECT_ID) &&
1832
0
          (p2->type & CONST_ASSIGN))
1833
0
        {
1834
0
          snprintf (name, sizeof (name), "%s.%s", definitionsName,
1835
0
        p2->name);
1836
1837
0
          len = ASN1_MAX_NAME_SIZE;
1838
0
          result =
1839
0
      asn1_read_value (definitions, name, value, &len);
1840
1841
0
          if ((result == ASN1_SUCCESS)
1842
0
        && (!_asn1_strcmp (p3->value, value)))
1843
0
      {
1844
0
        p2 = p2->right; /* pointer to the structure to
1845
               use for expansion */
1846
0
        while ((p2) && (p2->type & CONST_ASSIGN))
1847
0
          p2 = p2->right;
1848
1849
0
        if (p2)
1850
0
          {
1851
0
            snprintf (name, sizeof (name), "%s.%s",
1852
0
          definitionsName, p2->name);
1853
1854
0
            result =
1855
0
        asn1_create_element (definitions, name, &aux);
1856
0
            if (result == ASN1_SUCCESS)
1857
0
        {
1858
0
          _asn1_cpy_name (aux, p);
1859
0
          len2 =
1860
0
            asn1_get_length_der (p->value,
1861
0
               p->value_len, &len3);
1862
0
          if (len2 < 0)
1863
0
            return ASN1_DER_ERROR;
1864
1865
0
          result =
1866
0
            asn1_der_decoding (&aux, p->value + len3,
1867
0
                   len2,
1868
0
                   errorDescription);
1869
0
          if (result == ASN1_SUCCESS)
1870
0
            {
1871
1872
0
              _asn1_set_right (aux, p->right);
1873
0
              _asn1_set_right (p, aux);
1874
1875
0
              result = asn1_delete_structure (&p);
1876
0
              if (result == ASN1_SUCCESS)
1877
0
          {
1878
0
            p = aux;
1879
0
            aux = NULL;
1880
0
            break;
1881
0
          }
1882
0
              else
1883
0
          { /* error with asn1_delete_structure */
1884
0
            asn1_delete_structure (&aux);
1885
0
            retCode = result;
1886
0
            break;
1887
0
          }
1888
0
            }
1889
0
          else
1890
0
            { /* error with asn1_der_decoding */
1891
0
              retCode = result;
1892
0
              break;
1893
0
            }
1894
0
        }
1895
0
            else
1896
0
        { /* error with asn1_create_element */
1897
0
          retCode = result;
1898
0
          break;
1899
0
        }
1900
0
          }
1901
0
        else
1902
0
          { /* error with the pointer to the structure to expand */
1903
0
            retCode = ASN1_ERROR_TYPE_ANY;
1904
0
            break;
1905
0
          }
1906
0
      }
1907
0
        }
1908
0
      p2 = p2->right;
1909
0
    }    /* end while */
1910
1911
0
        if (!p2)
1912
0
    {
1913
0
      retCode = ASN1_ERROR_TYPE_ANY;
1914
0
      break;
1915
0
    }
1916
1917
0
      }
1918
0
    break;
1919
0
  default:
1920
0
    break;
1921
0
  }
1922
1923
1924
0
      if (p->down)
1925
0
  {
1926
0
    p = p->down;
1927
0
  }
1928
0
      else if (p == *element)
1929
0
  {
1930
0
    p = NULL;
1931
0
    break;
1932
0
  }
1933
0
      else if (p->right)
1934
0
  p = p->right;
1935
0
      else
1936
0
  {
1937
0
    while (1)
1938
0
      {
1939
0
        p = _asn1_find_up (p);
1940
0
        if (p == *element)
1941
0
    {
1942
0
      p = NULL;
1943
0
      break;
1944
0
    }
1945
0
        if (p->right)
1946
0
    {
1947
0
      p = p->right;
1948
0
      break;
1949
0
    }
1950
0
      }
1951
0
  }
1952
0
    }
1953
1954
0
  return retCode;
1955
0
}
1956
1957
/**
1958
 * asn1_expand_octet_string:
1959
 * @definitions: ASN1 definitions
1960
 * @element: pointer to an ASN1 structure
1961
 * @octetName: name of the OCTET STRING field to expand.
1962
 * @objectName: name of the OBJECT IDENTIFIER field to use to define
1963
 *    the type for expansion.
1964
 *
1965
 * Expands an "OCTET STRING" element of a structure created from a DER
1966
 * decoding process (the asn1_der_decoding() function).  The type used
1967
 * for expansion is the first one following the definition of the
1968
 * actual value of the OBJECT IDENTIFIER indicated by OBJECTNAME.
1969
 *
1970
 * Returns: %ASN1_SUCCESS if substitution OK, %ASN1_ELEMENT_NOT_FOUND
1971
 *   if @objectName or @octetName are not correct,
1972
 *   %ASN1_VALUE_NOT_VALID if it wasn't possible to find the type to
1973
 *   use for expansion, or other errors depending on DER decoding.
1974
 **/
1975
int
1976
asn1_expand_octet_string (asn1_node_const definitions, asn1_node *element,
1977
        const char *octetName, const char *objectName)
1978
0
{
1979
0
  char name[2 * ASN1_MAX_NAME_SIZE + 1], value[ASN1_MAX_NAME_SIZE];
1980
0
  int retCode = ASN1_SUCCESS, result;
1981
0
  int len, len2, len3;
1982
0
  asn1_node_const p2;
1983
0
  asn1_node aux = NULL;
1984
0
  asn1_node octetNode = NULL, objectNode = NULL;
1985
0
  char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
1986
1987
0
  if ((definitions == NULL) || (*element == NULL))
1988
0
    return ASN1_ELEMENT_NOT_FOUND;
1989
1990
0
  octetNode = asn1_find_node (*element, octetName);
1991
0
  if (octetNode == NULL)
1992
0
    return ASN1_ELEMENT_NOT_FOUND;
1993
0
  if (type_field (octetNode->type) != ASN1_ETYPE_OCTET_STRING)
1994
0
    return ASN1_ELEMENT_NOT_FOUND;
1995
0
  if (octetNode->value == NULL)
1996
0
    return ASN1_VALUE_NOT_FOUND;
1997
1998
0
  objectNode = asn1_find_node (*element, objectName);
1999
0
  if (objectNode == NULL)
2000
0
    return ASN1_ELEMENT_NOT_FOUND;
2001
2002
0
  if (type_field (objectNode->type) != ASN1_ETYPE_OBJECT_ID)
2003
0
    return ASN1_ELEMENT_NOT_FOUND;
2004
2005
0
  if (objectNode->value == NULL)
2006
0
    return ASN1_VALUE_NOT_FOUND;
2007
2008
2009
  /* search the OBJECT_ID into definitions */
2010
0
  p2 = definitions->down;
2011
0
  while (p2)
2012
0
    {
2013
0
      if ((type_field (p2->type) == ASN1_ETYPE_OBJECT_ID) &&
2014
0
    (p2->type & CONST_ASSIGN))
2015
0
  {
2016
0
    strcpy (name, definitions->name);
2017
0
    strcat (name, ".");
2018
0
    strcat (name, p2->name);
2019
2020
0
    len = sizeof (value);
2021
0
    result = asn1_read_value (definitions, name, value, &len);
2022
2023
0
    if ((result == ASN1_SUCCESS)
2024
0
        && (!_asn1_strcmp (objectNode->value, value)))
2025
0
      {
2026
2027
0
        p2 = p2->right; /* pointer to the structure to
2028
           use for expansion */
2029
0
        while ((p2) && (p2->type & CONST_ASSIGN))
2030
0
    p2 = p2->right;
2031
2032
0
        if (p2)
2033
0
    {
2034
0
      strcpy (name, definitions->name);
2035
0
      strcat (name, ".");
2036
0
      strcat (name, p2->name);
2037
2038
0
      result = asn1_create_element (definitions, name, &aux);
2039
0
      if (result == ASN1_SUCCESS)
2040
0
        {
2041
0
          _asn1_cpy_name (aux, octetNode);
2042
0
          len2 =
2043
0
      asn1_get_length_der (octetNode->value,
2044
0
               octetNode->value_len, &len3);
2045
0
          if (len2 < 0)
2046
0
      return ASN1_DER_ERROR;
2047
2048
0
          result =
2049
0
      asn1_der_decoding (&aux, octetNode->value + len3,
2050
0
             len2, errorDescription);
2051
0
          if (result == ASN1_SUCCESS)
2052
0
      {
2053
2054
0
        _asn1_set_right (aux, octetNode->right);
2055
0
        _asn1_set_right (octetNode, aux);
2056
2057
0
        result = asn1_delete_structure (&octetNode);
2058
0
        if (result == ASN1_SUCCESS)
2059
0
          {
2060
0
            aux = NULL;
2061
0
            break;
2062
0
          }
2063
0
        else
2064
0
          { /* error with asn1_delete_structure */
2065
0
            asn1_delete_structure (&aux);
2066
0
            retCode = result;
2067
0
            break;
2068
0
          }
2069
0
      }
2070
0
          else
2071
0
      { /* error with asn1_der_decoding */
2072
0
        retCode = result;
2073
0
        break;
2074
0
      }
2075
0
        }
2076
0
      else
2077
0
        {   /* error with asn1_create_element */
2078
0
          retCode = result;
2079
0
          break;
2080
0
        }
2081
0
    }
2082
0
        else
2083
0
    {   /* error with the pointer to the structure to expand */
2084
0
      retCode = ASN1_VALUE_NOT_VALID;
2085
0
      break;
2086
0
    }
2087
0
      }
2088
0
  }
2089
2090
0
      p2 = p2->right;
2091
2092
0
    }
2093
2094
0
  if (!p2)
2095
0
    retCode = ASN1_VALUE_NOT_VALID;
2096
2097
0
  return retCode;
2098
0
}
2099
2100
/*-
2101
 * _asn1_decode_simple_der:
2102
 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2103
 * @der: the encoded string
2104
 * @_der_len: the bytes of the encoded string
2105
 * @str: a pointer to the data
2106
 * @str_len: the length of the data
2107
 * @dflags: DECODE_FLAG_*
2108
 *
2109
 * Decodes a simple DER encoded type (e.g. a string, which is not constructed).
2110
 * The output is a pointer inside the @der.
2111
 *
2112
 * Returns: %ASN1_SUCCESS if successful or an error value.
2113
 -*/
2114
static int
2115
_asn1_decode_simple_der (unsigned int etype, const unsigned char *der,
2116
       unsigned int _der_len, const unsigned char **str,
2117
       unsigned int *str_len, unsigned dflags)
2118
152k
{
2119
152k
  int tag_len, len_len;
2120
152k
  const unsigned char *p;
2121
152k
  int der_len = _der_len;
2122
152k
  unsigned char class;
2123
152k
  unsigned long tag;
2124
152k
  long ret;
2125
2126
152k
  if (der == NULL || der_len == 0)
2127
0
    return ASN1_VALUE_NOT_VALID;
2128
2129
152k
  if (ETYPE_OK (etype) == 0 || ETYPE_IS_STRING (etype) == 0)
2130
0
    return ASN1_VALUE_NOT_VALID;
2131
2132
  /* doesn't handle constructed classes */
2133
152k
  class = ETYPE_CLASS (etype);
2134
152k
  if (class != ASN1_CLASS_UNIVERSAL)
2135
0
    return ASN1_VALUE_NOT_VALID;
2136
2137
152k
  p = der;
2138
2139
152k
  if (dflags & DECODE_FLAG_HAVE_TAG)
2140
152k
    {
2141
152k
      ret = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag);
2142
152k
      if (ret != ASN1_SUCCESS)
2143
0
  return ret;
2144
2145
152k
      if (class != ETYPE_CLASS (etype) || tag != ETYPE_TAG (etype))
2146
2.69k
  {
2147
2.69k
    warn ();
2148
2.69k
    return ASN1_DER_ERROR;
2149
2.69k
  }
2150
2151
150k
      p += tag_len;
2152
150k
      der_len -= tag_len;
2153
150k
      if (der_len <= 0)
2154
17
  return ASN1_DER_ERROR;
2155
150k
    }
2156
2157
150k
  ret = asn1_get_length_der (p, der_len, &len_len);
2158
150k
  if (ret < 0)
2159
241
    return ASN1_DER_ERROR;
2160
2161
149k
  p += len_len;
2162
149k
  der_len -= len_len;
2163
149k
  if (der_len <= 0)
2164
53
    return ASN1_DER_ERROR;
2165
2166
149k
  *str_len = ret;
2167
149k
  *str = p;
2168
2169
149k
  return ASN1_SUCCESS;
2170
149k
}
2171
2172
/**
2173
 * asn1_decode_simple_der:
2174
 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2175
 * @der: the encoded string
2176
 * @_der_len: the bytes of the encoded string
2177
 * @str: a pointer to the data
2178
 * @str_len: the length of the data
2179
 *
2180
 * Decodes a simple DER encoded type (e.g. a string, which is not constructed).
2181
 * The output is a pointer inside the @der.
2182
 *
2183
 * Returns: %ASN1_SUCCESS if successful or an error value.
2184
 **/
2185
int
2186
asn1_decode_simple_der (unsigned int etype, const unsigned char *der,
2187
      unsigned int _der_len, const unsigned char **str,
2188
      unsigned int *str_len)
2189
7.85k
{
2190
7.85k
  return _asn1_decode_simple_der (etype, der, _der_len, str, str_len,
2191
7.85k
          DECODE_FLAG_HAVE_TAG);
2192
7.85k
}
2193
2194
static int
2195
append (uint8_t **dst, unsigned *dst_size, const unsigned char *src,
2196
  unsigned src_size)
2197
249k
{
2198
249k
  if (src_size == 0)
2199
8.09k
    return ASN1_SUCCESS;
2200
2201
241k
  *dst = _asn1_realloc (*dst, *dst_size + src_size);
2202
241k
  if (*dst == NULL)
2203
0
    return ASN1_MEM_ALLOC_ERROR;
2204
241k
  memcpy (*dst + *dst_size, src, src_size);
2205
241k
  *dst_size += src_size;
2206
241k
  return ASN1_SUCCESS;
2207
241k
}
2208
2209
/*-
2210
 * _asn1_decode_simple_ber:
2211
 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2212
 * @der: the encoded string
2213
 * @_der_len: the bytes of the encoded string
2214
 * @str: a pointer to the data
2215
 * @str_len: the length of the data
2216
 * @ber_len: the total length occupied by BER (may be %NULL)
2217
 * @have_tag: whether a DER tag is included
2218
 *
2219
 * Decodes a BER encoded type. The output is an allocated value
2220
 * of the data. This decodes BER STRINGS only. Other types are
2221
 * decoded as DER.
2222
 *
2223
 * Returns: %ASN1_SUCCESS if successful or an error value.
2224
 -*/
2225
static int
2226
_asn1_decode_simple_ber (unsigned int etype, const unsigned char *der,
2227
       unsigned int _der_len, unsigned char **str,
2228
       unsigned int *str_len, unsigned int *ber_len,
2229
       unsigned dflags)
2230
151k
{
2231
151k
  int tag_len, len_len;
2232
151k
  const unsigned char *p;
2233
151k
  int der_len = _der_len;
2234
151k
  uint8_t *total = NULL;
2235
151k
  unsigned total_size = 0;
2236
151k
  unsigned char class;
2237
151k
  unsigned long tag;
2238
151k
  unsigned char *out = NULL;
2239
151k
  const unsigned char *cout = NULL;
2240
151k
  unsigned out_len;
2241
151k
  long result;
2242
2243
151k
  if (ber_len)
2244
107k
    *ber_len = 0;
2245
2246
151k
  if (der == NULL || der_len == 0)
2247
22
    {
2248
22
      warn ();
2249
22
      return ASN1_VALUE_NOT_VALID;
2250
22
    }
2251
2252
151k
  if (ETYPE_OK (etype) == 0)
2253
0
    {
2254
0
      warn ();
2255
0
      return ASN1_VALUE_NOT_VALID;
2256
0
    }
2257
2258
  /* doesn't handle constructed + definite classes */
2259
151k
  class = ETYPE_CLASS (etype);
2260
151k
  if (class != ASN1_CLASS_UNIVERSAL)
2261
0
    {
2262
0
      warn ();
2263
0
      return ASN1_VALUE_NOT_VALID;
2264
0
    }
2265
2266
151k
  p = der;
2267
2268
151k
  if (dflags & DECODE_FLAG_HAVE_TAG)
2269
150k
    {
2270
150k
      result = asn1_get_tag_der (p, der_len, &class, &tag_len, &tag);
2271
150k
      if (result != ASN1_SUCCESS)
2272
98
  {
2273
98
    warn ();
2274
98
    return result;
2275
98
  }
2276
2277
149k
      if (tag != ETYPE_TAG (etype))
2278
778
  {
2279
778
    warn ();
2280
778
    return ASN1_DER_ERROR;
2281
778
  }
2282
2283
149k
      p += tag_len;
2284
2285
149k
      DECR_LEN (der_len, tag_len);
2286
2287
149k
      if (ber_len)
2288
105k
  *ber_len += tag_len;
2289
149k
    }
2290
2291
  /* indefinite constructed */
2292
150k
  if ((((dflags & DECODE_FLAG_CONSTRUCTED) || class == ASN1_CLASS_STRUCTURED)
2293
150k
       && ETYPE_IS_STRING (etype)) && !(dflags & DECODE_FLAG_LEVEL3))
2294
5.28k
    {
2295
5.28k
      if (der_len == 0)
2296
7
  {
2297
7
    warn ();
2298
7
    result = ASN1_DER_ERROR;
2299
7
    goto cleanup;
2300
7
  }
2301
2302
5.27k
      if (der_len > 0 && p[0] == 0x80)  /* indefinite */
2303
1.54k
  {
2304
1.54k
    len_len = 1;
2305
1.54k
    DECR_LEN (der_len, len_len);
2306
1.54k
    p += len_len;
2307
2308
1.54k
    if (ber_len)
2309
818
      *ber_len += len_len;
2310
2311
    /* decode the available octet strings */
2312
1.54k
    do
2313
102k
      {
2314
102k
        unsigned tmp_len;
2315
102k
        unsigned flags = DECODE_FLAG_HAVE_TAG;
2316
2317
102k
        if (dflags & DECODE_FLAG_LEVEL1)
2318
4.57k
    flags |= DECODE_FLAG_LEVEL2;
2319
97.9k
        else if (dflags & DECODE_FLAG_LEVEL2)
2320
874
    flags |= DECODE_FLAG_LEVEL3;
2321
97.1k
        else
2322
97.1k
    flags |= DECODE_FLAG_LEVEL1;
2323
2324
102k
        result =
2325
102k
    _asn1_decode_simple_ber (etype, p, der_len, &out, &out_len,
2326
102k
           &tmp_len, flags);
2327
102k
        if (result != ASN1_SUCCESS)
2328
793
    {
2329
793
      warn ();
2330
793
      goto cleanup;
2331
793
    }
2332
2333
101k
        p += tmp_len;
2334
101k
        DECR_LEN (der_len, tmp_len);
2335
2336
101k
        if (ber_len)
2337
92.4k
    *ber_len += tmp_len;
2338
2339
101k
        DECR_LEN (der_len, 2); /* we need the EOC */
2340
2341
101k
        result = append (&total, &total_size, out, out_len);
2342
101k
        if (result != ASN1_SUCCESS)
2343
0
    {
2344
0
      warn ();
2345
0
      goto cleanup;
2346
0
    }
2347
2348
101k
        free (out);
2349
101k
        out = NULL;
2350
2351
101k
        if (p[0] == 0 && p[1] == 0)  /* EOC */
2352
656
    {
2353
656
      if (ber_len)
2354
360
        *ber_len += 2;
2355
656
      break;
2356
656
    }
2357
2358
        /* no EOC */
2359
101k
        der_len += 2;
2360
2361
101k
        if (der_len == 2)
2362
49
    {
2363
49
      warn ();
2364
49
      result = ASN1_DER_ERROR;
2365
49
      goto cleanup;
2366
49
    }
2367
101k
      }
2368
101k
    while (1);
2369
1.54k
  }
2370
3.73k
      else      /* constructed */
2371
3.73k
  {
2372
3.73k
    long const_len;
2373
2374
3.73k
    result = asn1_get_length_ber (p, der_len, &len_len);
2375
3.73k
    if (result < 0)
2376
226
      {
2377
226
        warn ();
2378
226
        result = ASN1_DER_ERROR;
2379
226
        goto cleanup;
2380
226
      }
2381
2382
3.50k
    DECR_LEN (der_len, len_len);
2383
3.50k
    p += len_len;
2384
2385
3.50k
    const_len = result;
2386
2387
3.50k
    if (ber_len)
2388
3.31k
      *ber_len += len_len;
2389
2390
    /* decode the available octet strings */
2391
6.16k
    while (const_len > 0)
2392
3.24k
      {
2393
3.24k
        unsigned tmp_len;
2394
3.24k
        unsigned flags = DECODE_FLAG_HAVE_TAG;
2395
2396
3.24k
        if (dflags & DECODE_FLAG_LEVEL1)
2397
1.22k
    flags |= DECODE_FLAG_LEVEL2;
2398
2.02k
        else if (dflags & DECODE_FLAG_LEVEL2)
2399
475
    flags |= DECODE_FLAG_LEVEL3;
2400
1.54k
        else
2401
1.54k
    flags |= DECODE_FLAG_LEVEL1;
2402
2403
3.24k
        result =
2404
3.24k
    _asn1_decode_simple_ber (etype, p, der_len, &out, &out_len,
2405
3.24k
           &tmp_len, flags);
2406
3.24k
        if (result != ASN1_SUCCESS)
2407
486
    {
2408
486
      warn ();
2409
486
      goto cleanup;
2410
486
    }
2411
2412
2.75k
        p += tmp_len;
2413
2.75k
        DECR_LEN (der_len, tmp_len);
2414
2.75k
        DECR_LEN (const_len, tmp_len);
2415
2416
2.66k
        if (ber_len)
2417
2.04k
    *ber_len += tmp_len;
2418
2419
2.66k
        result = append (&total, &total_size, out, out_len);
2420
2.66k
        if (result != ASN1_SUCCESS)
2421
0
    {
2422
0
      warn ();
2423
0
      goto cleanup;
2424
0
    }
2425
2426
2.66k
        free (out);
2427
2.66k
        out = NULL;
2428
2.66k
      }
2429
3.50k
  }
2430
5.27k
    }
2431
145k
  else if (class == ETYPE_CLASS (etype))
2432
145k
    {
2433
145k
      if (ber_len)
2434
102k
  {
2435
102k
    result = asn1_get_length_der (p, der_len, &len_len);
2436
102k
    if (result < 0)
2437
218
      {
2438
218
        warn ();
2439
218
        result = ASN1_DER_ERROR;
2440
218
        goto cleanup;
2441
218
      }
2442
102k
    *ber_len += result + len_len;
2443
102k
  }
2444
2445
      /* non-string values are decoded as DER */
2446
144k
      result =
2447
144k
  _asn1_decode_simple_der (etype, der, _der_len, &cout, &out_len,
2448
144k
         dflags);
2449
144k
      if (result != ASN1_SUCCESS)
2450
186
  {
2451
186
    warn ();
2452
186
    goto cleanup;
2453
186
  }
2454
2455
144k
      result = append (&total, &total_size, cout, out_len);
2456
144k
      if (result != ASN1_SUCCESS)
2457
0
  {
2458
0
    warn ();
2459
0
    goto cleanup;
2460
0
  }
2461
144k
    }
2462
79
  else
2463
79
    {
2464
79
      warn ();
2465
79
      result = ASN1_DER_ERROR;
2466
79
      goto cleanup;
2467
79
    }
2468
2469
148k
  *str = total;
2470
148k
  *str_len = total_size;
2471
2472
148k
  return ASN1_SUCCESS;
2473
2.18k
cleanup:
2474
2.18k
  free (out);
2475
2.18k
  free (total);
2476
2.18k
  return result;
2477
150k
}
2478
2479
/**
2480
 * asn1_decode_simple_ber:
2481
 * @etype: The type of the string to be encoded (ASN1_ETYPE_)
2482
 * @der: the encoded string
2483
 * @_der_len: the bytes of the encoded string
2484
 * @str: a pointer to the data
2485
 * @str_len: the length of the data
2486
 * @ber_len: the total length occupied by BER (may be %NULL)
2487
 *
2488
 * Decodes a BER encoded type. The output is an allocated value
2489
 * of the data. This decodes BER STRINGS only. Other types are
2490
 * decoded as DER.
2491
 *
2492
 * Returns: %ASN1_SUCCESS if successful or an error value.
2493
 **/
2494
int
2495
asn1_decode_simple_ber (unsigned int etype, const unsigned char *der,
2496
      unsigned int _der_len, unsigned char **str,
2497
      unsigned int *str_len, unsigned int *ber_len)
2498
44.2k
{
2499
44.2k
  return _asn1_decode_simple_ber (etype, der, _der_len, str, str_len, ber_len,
2500
44.2k
          DECODE_FLAG_HAVE_TAG);
2501
44.2k
}