Coverage Report

Created: 2022-08-24 06:31

/src/libressl/crypto/asn1/tasn_dec.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: tasn_dec.c,v 1.78 2022/06/29 08:56:44 beck Exp $ */
2
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3
 * project 2000.
4
 */
5
/* ====================================================================
6
 * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 *
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19
 *
20
 * 3. All advertising materials mentioning features or use of this
21
 *    software must display the following acknowledgment:
22
 *    "This product includes software developed by the OpenSSL Project
23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24
 *
25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26
 *    endorse or promote products derived from this software without
27
 *    prior written permission. For written permission, please contact
28
 *    licensing@OpenSSL.org.
29
 *
30
 * 5. Products derived from this software may not be called "OpenSSL"
31
 *    nor may "OpenSSL" appear in their names without prior written
32
 *    permission of the OpenSSL Project.
33
 *
34
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36
 *    "This product includes software developed by the OpenSSL Project
37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38
 *
39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
51
 * ====================================================================
52
 *
53
 * This product includes cryptographic software written by Eric Young
54
 * (eay@cryptsoft.com).  This product includes software written by Tim
55
 * Hudson (tjh@cryptsoft.com).
56
 *
57
 */
58
59
#include <limits.h>
60
#include <stddef.h>
61
#include <string.h>
62
63
#include <openssl/asn1.h>
64
#include <openssl/asn1t.h>
65
#include <openssl/buffer.h>
66
#include <openssl/err.h>
67
#include <openssl/objects.h>
68
69
#include "asn1_locl.h"
70
#include "bytestring.h"
71
72
/*
73
 * Constructed types with a recursive definition (such as can be found in PKCS7)
74
 * could eventually exceed the stack given malicious input with excessive
75
 * recursion. Therefore we limit the stack depth.
76
 */
77
106k
#define ASN1_MAX_CONSTRUCTED_NEST 30
78
79
#ifndef ASN1_MAX_STRING_NEST
80
/*
81
 * This determines how many levels of recursion are permitted in ASN.1 string
82
 * types. If it is not limited stack overflows can occur. If set to zero no
83
 * recursion is allowed at all.
84
 */
85
1.27k
#define ASN1_MAX_STRING_NEST 5
86
#endif
87
88
static int asn1_template_d2i(ASN1_VALUE **pval, CBS *cbs,
89
    const ASN1_TEMPLATE *at, int optional, int depth);
90
91
static int
92
asn1_check_eoc(CBS *cbs)
93
109k
{
94
109k
  uint16_t eoc;
95
96
109k
  if (!CBS_peek_u16(cbs, &eoc))
97
2.97k
    return 0;
98
106k
  if (eoc != 0)
99
102k
    return 0;
100
101
3.94k
  return CBS_skip(cbs, 2);
102
106k
}
103
104
static int
105
asn1_check_tag(CBS *cbs, size_t *out_len, int *out_tag, uint8_t *out_class,
106
    int *out_indefinite, int *out_constructed, int expected_tag,
107
    int expected_class, int optional)
108
112k
{
109
112k
  int constructed, indefinite;
110
112k
  uint32_t tag_number;
111
112k
  uint8_t tag_class;
112
112k
  size_t length;
113
114
112k
  if (out_len != NULL)
115
112k
    *out_len = 0;
116
112k
  if (out_tag != NULL)
117
15.8k
    *out_tag = 0;
118
112k
  if (out_class != NULL)
119
15.8k
    *out_class = 0;
120
112k
  if (out_indefinite != NULL)
121
112k
    *out_indefinite = 0;
122
112k
  if (out_constructed != NULL)
123
97.4k
    *out_constructed = 0;
124
125
112k
  if (!asn1_get_identifier_cbs(cbs, 0, &tag_class, &constructed,
126
112k
      &tag_number)) {
127
43
    ASN1error(ASN1_R_BAD_OBJECT_HEADER);
128
43
    return 0;
129
43
  }
130
112k
  if (expected_tag >= 0) {
131
92.9k
    if (expected_tag != tag_number ||
132
92.9k
        expected_class != tag_class << 6) {
133
      /* Indicate missing type if this is OPTIONAL. */
134
9.57k
      if (optional)
135
8.84k
        return -1;
136
137
723
      ASN1error(ASN1_R_WRONG_TAG);
138
723
      return 0;
139
9.57k
    }
140
92.9k
  }
141
103k
  if (!asn1_get_length_cbs(cbs, 0, &indefinite, &length)) {
142
231
    ASN1error(ASN1_R_BAD_OBJECT_HEADER);
143
231
    return 0;
144
231
  }
145
146
  /* Indefinite length can only be used with constructed encoding. */
147
103k
  if (indefinite && !constructed) {
148
11
    ASN1error(ASN1_R_BAD_OBJECT_HEADER);
149
11
    return 0;
150
11
  }
151
152
103k
  if (!indefinite && CBS_len(cbs) < length) {
153
218
    ASN1error(ASN1_R_TOO_LONG);
154
218
    return 0;
155
218
  }
156
157
102k
  if (tag_number > INT_MAX) {
158
28
    ASN1error(ASN1_R_TOO_LONG);
159
28
    return 0;
160
28
  }
161
162
102k
  if (indefinite)
163
5.52k
    length = CBS_len(cbs);
164
165
102k
  if (out_len != NULL)
166
102k
    *out_len = length;
167
102k
  if (out_tag != NULL)
168
15.7k
    *out_tag = tag_number;
169
102k
  if (out_class != NULL)
170
15.7k
    *out_class = tag_class << 6;
171
102k
  if (out_indefinite != NULL)
172
102k
    *out_indefinite = indefinite;
173
102k
  if (out_constructed != NULL)
174
87.6k
    *out_constructed = constructed;
175
176
102k
  return 1;
177
102k
}
178
179
/* Collect the contents from a constructed ASN.1 object. */
180
static int
181
asn1_collect(CBB *cbb, CBS *cbs, int indefinite, int expected_tag,
182
    int expected_class, int depth)
183
1.27k
{
184
1.27k
  int constructed;
185
1.27k
  size_t length;
186
1.27k
  CBS content;
187
1.27k
  int need_eoc;
188
189
1.27k
  if (depth > ASN1_MAX_STRING_NEST) {
190
6
    ASN1error(ASN1_R_NESTED_ASN1_STRING);
191
6
    return 0;
192
6
  }
193
194
1.26k
  need_eoc = indefinite;
195
196
4.27k
  while (CBS_len(cbs) > 0) {
197
3.89k
    if (asn1_check_eoc(cbs)) {
198
647
      if (!need_eoc) {
199
8
        ASN1error(ASN1_R_UNEXPECTED_EOC);
200
8
        return 0;
201
8
      }
202
639
      return 1;
203
647
    }
204
3.24k
    if (!asn1_check_tag(cbs, &length, NULL, NULL, &indefinite,
205
3.24k
        &constructed, expected_tag, expected_class, 0)) {
206
68
      ASN1error(ERR_R_NESTED_ASN1_ERROR);
207
68
      return 0;
208
68
    }
209
210
3.17k
    if (constructed) {
211
678
      if (!asn1_collect(cbb, cbs, indefinite, expected_tag,
212
678
          expected_class, depth + 1))
213
173
        return 0;
214
505
      continue;
215
678
    }
216
217
2.50k
    if (!CBS_get_bytes(cbs, &content, length)) {
218
0
      ASN1error(ERR_R_NESTED_ASN1_ERROR);
219
0
      return 0;
220
0
    }
221
2.50k
    if (!CBB_add_bytes(cbb, CBS_data(&content), CBS_len(&content)))
222
0
      return 0;
223
2.50k
  }
224
225
381
  if (need_eoc) {
226
14
    ASN1error(ASN1_R_MISSING_EOC);
227
14
    return 0;
228
14
  }
229
230
367
  return 1;
231
381
}
232
233
/* Find the end of an ASN.1 object. */
234
static int
235
asn1_find_end(CBS *cbs, size_t length, int indefinite)
236
647
{
237
647
  size_t eoc_count;
238
239
647
  if (!indefinite) {
240
503
    if (!CBS_skip(cbs, length)) {
241
0
      ASN1error(ERR_R_NESTED_ASN1_ERROR);
242
0
      return 0;
243
0
    }
244
503
    return 1;
245
503
  }
246
247
144
  eoc_count = 1;
248
249
1.34k
  while (CBS_len(cbs) > 0) {
250
1.33k
    if (asn1_check_eoc(cbs)) {
251
451
      if (--eoc_count == 0)
252
119
        break;
253
332
      continue;
254
451
    }
255
888
    if (!asn1_check_tag(cbs, &length, NULL, NULL,
256
888
        &indefinite, NULL, -1, 0, 0)) {
257
19
      ASN1error(ERR_R_NESTED_ASN1_ERROR);
258
19
      return 0;
259
19
    }
260
869
    if (indefinite) {
261
382
      eoc_count++;
262
382
      continue;
263
382
    }
264
487
    if (!CBS_skip(cbs, length))
265
0
      return 0;
266
487
  }
267
268
125
  if (eoc_count > 0) {
269
6
    ASN1error(ASN1_R_MISSING_EOC);
270
6
    return 0;
271
6
  }
272
273
119
  return 1;
274
125
}
275
276
static int
277
asn1_c2i_primitive(ASN1_VALUE **pval, CBS *content, int utype, const ASN1_ITEM *it)
278
48.8k
{
279
48.8k
  ASN1_STRING *stmp;
280
48.8k
  ASN1_INTEGER **tint;
281
48.8k
  ASN1_BOOLEAN *tbool;
282
48.8k
  uint8_t u8val;
283
48.8k
  int ret = 0;
284
285
48.8k
  if (it->funcs != NULL)
286
0
    return 0;
287
288
48.8k
  if (CBS_len(content) > INT_MAX)
289
0
    return 0;
290
291
48.8k
  switch (utype) {
292
20.1k
  case V_ASN1_OBJECT:
293
20.1k
    if (!c2i_ASN1_OBJECT_cbs((ASN1_OBJECT **)pval, content))
294
38
      goto err;
295
20.0k
    break;
296
297
20.0k
  case V_ASN1_NULL:
298
1.72k
    if (CBS_len(content) != 0) {
299
1
      ASN1error(ASN1_R_NULL_IS_WRONG_LENGTH);
300
1
      goto err;
301
1
    }
302
1.72k
    *pval = (ASN1_VALUE *)1;
303
1.72k
    break;
304
305
1.13k
  case V_ASN1_BOOLEAN:
306
1.13k
    tbool = (ASN1_BOOLEAN *)pval;
307
1.13k
    if (CBS_len(content) != 1) {
308
8
      ASN1error(ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
309
8
      goto err;
310
8
    }
311
1.13k
    if (!CBS_get_u8(content, &u8val))
312
0
      goto err;
313
1.13k
    *tbool = u8val;
314
1.13k
    break;
315
316
3.09k
  case V_ASN1_BIT_STRING:
317
3.09k
    if (!c2i_ASN1_BIT_STRING_cbs((ASN1_BIT_STRING **)pval, content))
318
46
      goto err;
319
3.04k
    break;
320
321
4.12k
  case V_ASN1_INTEGER:
322
5.13k
  case V_ASN1_ENUMERATED:
323
5.13k
    tint = (ASN1_INTEGER **)pval;
324
5.13k
    if (!c2i_ASN1_INTEGER_cbs(tint, content))
325
11
      goto err;
326
    /* Fixup type to match the expected form */
327
5.12k
    (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
328
5.12k
    break;
329
330
5.42k
  case V_ASN1_OCTET_STRING:
331
5.44k
  case V_ASN1_NUMERICSTRING:
332
11.0k
  case V_ASN1_PRINTABLESTRING:
333
11.1k
  case V_ASN1_T61STRING:
334
11.1k
  case V_ASN1_VIDEOTEXSTRING:
335
11.3k
  case V_ASN1_IA5STRING:
336
14.2k
  case V_ASN1_UTCTIME:
337
14.2k
  case V_ASN1_GENERALIZEDTIME:
338
14.2k
  case V_ASN1_GRAPHICSTRING:
339
14.2k
  case V_ASN1_VISIBLESTRING:
340
14.2k
  case V_ASN1_GENERALSTRING:
341
14.4k
  case V_ASN1_UNIVERSALSTRING:
342
14.4k
  case V_ASN1_BMPSTRING:
343
16.5k
  case V_ASN1_UTF8STRING:
344
17.0k
  case V_ASN1_OTHER:
345
17.0k
  case V_ASN1_SET:
346
17.2k
  case V_ASN1_SEQUENCE:
347
17.6k
  default:
348
17.6k
    if (utype == V_ASN1_BMPSTRING && (CBS_len(content) & 1)) {
349
1
      ASN1error(ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
350
1
      goto err;
351
1
    }
352
17.6k
    if (utype == V_ASN1_UNIVERSALSTRING && (CBS_len(content) & 3)) {
353
1
      ASN1error(ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
354
1
      goto err;
355
1
    }
356
17.6k
    if (utype == V_ASN1_UTCTIME || utype == V_ASN1_GENERALIZEDTIME) {
357
2.85k
      if (!asn1_time_parse_cbs(content,
358
2.85k
          utype == V_ASN1_GENERALIZEDTIME, NULL))  {
359
77
        ASN1error(ASN1_R_INVALID_TIME_FORMAT);
360
77
        goto err;
361
77
      }
362
2.85k
    }
363
    /* All based on ASN1_STRING and handled the same way. */
364
17.5k
    if (*pval == NULL) {
365
1.40k
      if ((stmp = ASN1_STRING_type_new(utype)) == NULL) {
366
0
        ASN1error(ERR_R_MALLOC_FAILURE);
367
0
        goto err;
368
0
      }
369
1.40k
      *pval = (ASN1_VALUE *)stmp;
370
16.1k
    } else {
371
16.1k
      stmp = (ASN1_STRING *)*pval;
372
16.1k
      stmp->type = utype;
373
16.1k
    }
374
17.5k
    if (!ASN1_STRING_set(stmp, CBS_data(content), CBS_len(content))) {
375
0
      ASN1_STRING_free(stmp);
376
0
      *pval = NULL;
377
0
      goto err;
378
0
    }
379
17.5k
    break;
380
48.8k
  }
381
382
48.6k
  ret = 1;
383
384
48.8k
 err:
385
48.8k
  return ret;
386
48.6k
}
387
388
static int
389
asn1_c2i_any(ASN1_VALUE **pval, CBS *content, int utype, const ASN1_ITEM *it)
390
3.31k
{
391
3.31k
  ASN1_TYPE *atype;
392
393
3.31k
  if (it->utype != V_ASN1_ANY || it->funcs != NULL)
394
0
    return 0;
395
396
3.31k
  if (*pval != NULL) {
397
0
    ASN1_TYPE_free((ASN1_TYPE *)*pval);
398
0
    *pval = NULL;
399
0
  }
400
401
3.31k
  if ((atype = ASN1_TYPE_new()) == NULL)
402
0
    return 0;
403
404
3.31k
  if (!asn1_c2i_primitive(&atype->value.asn1_value, content, utype, it)) {
405
48
    ASN1_TYPE_free(atype);
406
48
    return 0;
407
48
  }
408
3.26k
  atype->type = utype;
409
410
  /* Fix up value for ASN.1 NULL. */
411
3.26k
  if (atype->type == V_ASN1_NULL)
412
1.72k
    atype->value.ptr = NULL;
413
414
3.26k
  *pval = (ASN1_VALUE *)atype;
415
416
3.26k
  return 1;
417
3.31k
}
418
419
static int
420
asn1_c2i(ASN1_VALUE **pval, CBS *content, int utype, const ASN1_ITEM *it)
421
52.0k
{
422
52.0k
  if (CBS_len(content) > INT_MAX)
423
0
    return 0;
424
425
52.0k
  if (it->funcs != NULL) {
426
3.13k
    const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
427
3.13k
    char free_content = 0;
428
429
3.13k
    if (pf->prim_c2i == NULL)
430
0
      return 0;
431
432
3.13k
    return pf->prim_c2i(pval, CBS_data(content), CBS_len(content),
433
3.13k
        utype, &free_content, it);
434
3.13k
  }
435
436
48.8k
  if (it->utype == V_ASN1_ANY)
437
3.31k
    return asn1_c2i_any(pval, content, utype, it);
438
439
45.5k
  return asn1_c2i_primitive(pval, content, utype, it);
440
48.8k
}
441
442
/*
443
 * Decode ASN.1 content into a primitive type. There are three possible forms -
444
 * a SEQUENCE/SET/OTHER that is stored verbatim (including the ASN.1 tag and
445
 * length octets), constructed objects and non-constructed objects. In the
446
 * first two cases indefinite length is permitted, which we may need to handle.
447
 * When this function is called the *cbs should reference the start of the
448
 * ASN.1 object (i.e. the tag/length header), while *cbs_object should
449
 * reference the start of the object contents (i.e. after the tag/length
450
 * header. Additionally, the *cbs_object offset should be relative to the
451
 * ASN.1 object being parsed. On success the *cbs will point at the octet
452
 * after the object.
453
 */
454
static int
455
asn1_d2i_primitive_content(ASN1_VALUE **pval, CBS *cbs, CBS *cbs_object,
456
    int utype, int constructed, int indefinite, size_t length,
457
    const ASN1_ITEM *it)
458
52.1k
{
459
52.1k
  CBS cbs_content, cbs_initial;
460
52.1k
  uint8_t *data = NULL;
461
52.1k
  size_t data_len = 0;
462
52.1k
  CBB cbb;
463
52.1k
  int ret = 0;
464
465
52.1k
  memset(&cbb, 0, sizeof(cbb));
466
467
52.1k
  CBS_dup(cbs, &cbs_initial);
468
52.1k
  CBS_init(&cbs_content, NULL, 0);
469
470
  /* XXX - check primitive vs constructed based on utype. */
471
472
  /* SEQUENCE and SET must be constructed. */
473
52.1k
  if ((utype == V_ASN1_SEQUENCE || utype == V_ASN1_SET) && !constructed) {
474
4
    ASN1error(ASN1_R_TYPE_NOT_CONSTRUCTED);
475
4
    goto err;
476
4
  }
477
478
  /* SEQUENCE, SET and "OTHER" are left in encoded form. */
479
52.1k
  if (utype == V_ASN1_SEQUENCE || utype == V_ASN1_SET ||
480
52.1k
      utype == V_ASN1_OTHER) {
481
647
    if (!asn1_find_end(cbs_object, length, indefinite))
482
25
      goto err;
483
622
    if (!CBS_get_bytes(&cbs_initial, &cbs_content,
484
622
        CBS_offset(cbs_object)))
485
0
      goto err;
486
51.4k
  } else if (constructed) {
487
    /*
488
     * Should really check the internal tags are correct but
489
     * some things may get this wrong. The relevant specs
490
     * say that constructed string types should be OCTET STRINGs
491
     * internally irrespective of the type. So instead just check
492
     * for UNIVERSAL class and ignore the tag.
493
     */
494
597
    if (!CBB_init(&cbb, 0))
495
0
      goto err;
496
597
    if (!asn1_collect(&cbb, cbs_object, indefinite, -1,
497
597
        V_ASN1_UNIVERSAL, 0))
498
96
      goto err;
499
501
    if (!CBB_finish(&cbb, &data, &data_len))
500
0
      goto err;
501
502
501
    CBS_init(&cbs_content, data, data_len);
503
50.8k
  } else {
504
50.8k
    if (!CBS_get_bytes(cbs_object, &cbs_content, length))
505
0
      goto err;
506
50.8k
  }
507
508
52.0k
  if (!asn1_c2i(pval, &cbs_content, utype, it))
509
193
    goto err;
510
511
51.8k
  if (!CBS_skip(cbs, CBS_offset(cbs_object)))
512
0
    goto err;
513
514
51.8k
  ret = 1;
515
516
52.1k
 err:
517
52.1k
  CBB_cleanup(&cbb);
518
52.1k
  freezero(data, data_len);
519
520
52.1k
  return ret;
521
51.8k
}
522
523
static int
524
asn1_d2i_any(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
525
    int tag_number, int tag_class, int optional)
526
3.40k
{
527
3.40k
  int constructed, indefinite;
528
3.40k
  uint8_t object_class;
529
3.40k
  int object_type;
530
3.40k
  CBS cbs_object;
531
3.40k
  size_t length;
532
533
3.40k
  CBS_init(&cbs_object, CBS_data(cbs), CBS_len(cbs));
534
535
3.40k
  if (it->utype != V_ASN1_ANY)
536
0
    return 0;
537
538
3.40k
  if (tag_number >= 0) {
539
0
    ASN1error(ASN1_R_ILLEGAL_TAGGED_ANY);
540
0
    return 0;
541
0
  }
542
3.40k
  if (optional) {
543
0
    ASN1error(ASN1_R_ILLEGAL_OPTIONAL_ANY);
544
0
    return 0;
545
0
  }
546
547
  /* Determine type from ASN.1 tag. */
548
3.40k
  if (asn1_check_tag(&cbs_object, &length, &object_type, &object_class,
549
3.40k
      &indefinite, &constructed, -1, 0, 0) != 1) {
550
39
    ASN1error(ERR_R_NESTED_ASN1_ERROR);
551
39
    return 0;
552
39
  }
553
3.36k
  if (object_class != V_ASN1_UNIVERSAL)
554
439
    object_type = V_ASN1_OTHER;
555
556
3.36k
  return asn1_d2i_primitive_content(pval, cbs, &cbs_object, object_type,
557
3.36k
      constructed, indefinite, length, it);
558
3.40k
}
559
560
static int
561
asn1_d2i_mstring(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
562
    int tag_number, int tag_class, int optional)
563
12.3k
{
564
12.3k
  int constructed, indefinite;
565
12.3k
  uint8_t object_class;
566
12.3k
  int object_tag;
567
12.3k
  CBS cbs_object;
568
12.3k
  size_t length;
569
570
12.3k
  CBS_init(&cbs_object, CBS_data(cbs), CBS_len(cbs));
571
572
  /*
573
   * It never makes sense for multi-strings to have implicit tagging, so
574
   * if tag_number != -1, then this looks like an error in the template.
575
   */
576
12.3k
  if (tag_number != -1) {
577
0
    ASN1error(ASN1_R_BAD_TEMPLATE);
578
0
    return 0;
579
0
  }
580
581
12.3k
  if (asn1_check_tag(&cbs_object, &length, &object_tag, &object_class,
582
12.3k
      &indefinite, &constructed, -1, 0, 1) != 1) {
583
12
    ASN1error(ERR_R_NESTED_ASN1_ERROR);
584
12
    return 0;
585
12
  }
586
587
  /* Class must be UNIVERSAL. */
588
12.3k
  if (object_class != V_ASN1_UNIVERSAL) {
589
5
    if (optional)
590
0
      return -1;
591
5
    ASN1error(ASN1_R_MSTRING_NOT_UNIVERSAL);
592
5
    return 0;
593
5
  }
594
  /* Check tag matches bit map. */
595
12.3k
  if ((ASN1_tag2bit(object_tag) & it->utype) == 0) {
596
12
    if (optional)
597
0
      return -1;
598
12
    ASN1error(ASN1_R_MSTRING_WRONG_TAG);
599
12
    return 0;
600
12
  }
601
602
12.3k
  return asn1_d2i_primitive_content(pval, cbs, &cbs_object,
603
12.3k
      object_tag, constructed, indefinite, length, it);
604
12.3k
}
605
606
static int
607
asn1_d2i_primitive(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
608
    int tag_number, int tag_class, int optional)
609
47.9k
{
610
47.9k
  CBS cbs_object;
611
47.9k
  int constructed, indefinite;
612
47.9k
  int utype = it->utype;
613
47.9k
  size_t length;
614
47.9k
  int ret;
615
616
47.9k
  CBS_init(&cbs_object, CBS_data(cbs), CBS_len(cbs));
617
618
47.9k
  if (it->itype == ASN1_ITYPE_MSTRING)
619
0
    return 0;
620
621
47.9k
  if (it->utype == V_ASN1_ANY)
622
3.40k
    return asn1_d2i_any(pval, cbs, it, tag_number, tag_class, optional);
623
624
44.5k
  if (tag_number == -1) {
625
40.4k
    tag_number = it->utype;
626
40.4k
    tag_class = V_ASN1_UNIVERSAL;
627
40.4k
  }
628
629
44.5k
  ret = asn1_check_tag(&cbs_object, &length, NULL, NULL, &indefinite,
630
44.5k
      &constructed, tag_number, tag_class, optional);
631
44.5k
  if (ret == -1)
632
7.75k
    return -1;
633
36.8k
  if (ret != 1) {
634
422
    ASN1error(ERR_R_NESTED_ASN1_ERROR);
635
422
    return 0;
636
422
  }
637
638
36.3k
  return asn1_d2i_primitive_content(pval, cbs, &cbs_object, utype,
639
36.3k
      constructed, indefinite, length, it);
640
36.8k
}
641
642
static int
643
asn1_item_d2i_choice(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
644
    int tag_number, int tag_class, int optional, int depth)
645
377
{
646
377
  const ASN1_TEMPLATE *at, *errat = NULL;
647
377
  const ASN1_AUX *aux;
648
377
  ASN1_aux_cb *asn1_cb = NULL;
649
377
  ASN1_VALUE *achoice = NULL;
650
377
  ASN1_VALUE **pchptr;
651
377
  int i, ret;
652
653
377
  if ((aux = it->funcs) != NULL)
654
90
    asn1_cb = aux->asn1_cb;
655
656
377
  if (it->itype != ASN1_ITYPE_CHOICE)
657
0
    goto err;
658
659
  /*
660
   * It never makes sense for CHOICE types to have implicit tagging, so
661
   * if tag_number != -1, then this looks like an error in the template.
662
   */
663
377
  if (tag_number != -1) {
664
0
    ASN1error(ASN1_R_BAD_TEMPLATE);
665
0
    goto err;
666
0
  }
667
668
377
  if (*pval != NULL) {
669
15
    ASN1_item_ex_free(pval, it);
670
15
    *pval = NULL;
671
15
  }
672
673
377
  if (!ASN1_item_ex_new(&achoice, it)) {
674
0
    ASN1error(ERR_R_NESTED_ASN1_ERROR);
675
0
    goto err;
676
0
  }
677
678
377
  if (asn1_cb != NULL && !asn1_cb(ASN1_OP_D2I_PRE, &achoice, it, NULL)) {
679
0
    ASN1error(ASN1_R_AUX_ERROR);
680
0
    goto err;
681
0
  }
682
683
  /* Try each possible CHOICE in turn. */
684
1.91k
  for (i = 0; i < it->tcount; i++) {
685
1.83k
    at = &it->templates[i];
686
687
1.83k
    pchptr = asn1_get_field_ptr(&achoice, at);
688
689
    /* Mark field as OPTIONAL so its absence can be identified. */
690
1.83k
    ret = asn1_template_d2i(pchptr, cbs, at, 1, depth);
691
1.83k
    if (ret == -1)
692
1.53k
      continue;
693
294
    if (ret != 1) {
694
111
      ASN1error(ERR_R_NESTED_ASN1_ERROR);
695
111
      errat = at;
696
111
      goto err;
697
111
    }
698
699
    /* We've successfully decoded an ASN.1 object. */
700
183
    asn1_set_choice_selector(&achoice, i, it);
701
183
    break;
702
294
  }
703
704
  /* Did we fall off the end without reading anything? */
705
266
  if (i == it->tcount) {
706
83
    if (optional) {
707
0
      ASN1_item_ex_free(&achoice, it);
708
0
      return -1;
709
0
    }
710
83
    ASN1error(ASN1_R_NO_MATCHING_CHOICE_TYPE);
711
83
    goto err;
712
83
  }
713
714
183
  if (asn1_cb != NULL && !asn1_cb(ASN1_OP_D2I_POST, &achoice, it, NULL)) {
715
0
    ASN1error(ASN1_R_AUX_ERROR);
716
0
    goto err;
717
0
  }
718
719
183
  *pval = achoice;
720
183
  achoice = NULL;
721
722
183
  return 1;
723
724
194
 err:
725
194
  ASN1_item_ex_free(&achoice, it);
726
727
194
  if (errat != NULL)
728
111
    ERR_asprintf_error_data("Field=%s, Type=%s", errat->field_name,
729
111
        it->sname);
730
83
  else
731
83
    ERR_asprintf_error_data("Type=%s", it->sname);
732
733
194
  return 0;
734
183
}
735
736
static int
737
asn1_item_d2i_sequence(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
738
    int tag_number, int tag_class, int optional, int depth)
739
29.9k
{
740
29.9k
  CBS cbs_seq, cbs_seq_content, cbs_object;
741
29.9k
  int constructed, indefinite, optional_field;
742
29.9k
  const ASN1_TEMPLATE *errat = NULL;
743
29.9k
  const ASN1_TEMPLATE *seqat, *at;
744
29.9k
  ASN1_aux_cb *asn1_cb = NULL;
745
29.9k
  const ASN1_AUX *aux;
746
29.9k
  ASN1_VALUE *aseq = NULL;
747
29.9k
  ASN1_VALUE **pseqval;
748
29.9k
  int eoc_needed, i;
749
29.9k
  size_t length;
750
29.9k
  int ret = 0;
751
752
29.9k
  CBS_init(&cbs_seq, CBS_data(cbs), CBS_len(cbs));
753
754
29.9k
  if ((aux = it->funcs) != NULL)
755
7.28k
    asn1_cb = aux->asn1_cb;
756
757
29.9k
  if (it->itype != ASN1_ITYPE_NDEF_SEQUENCE &&
758
29.9k
      it->itype != ASN1_ITYPE_SEQUENCE)
759
0
    goto err;
760
761
29.9k
  if (*pval != NULL) {
762
10.0k
    ASN1_item_ex_free(pval, it);
763
10.0k
    *pval = NULL;
764
10.0k
  }
765
766
  /* If no IMPLICIT tagging use UNIVERSAL/SEQUENCE. */
767
29.9k
  if (tag_number == -1) {
768
29.5k
    tag_class = V_ASN1_UNIVERSAL;
769
29.5k
    tag_number = V_ASN1_SEQUENCE;
770
29.5k
  }
771
772
  /* Read ASN.1 SEQUENCE header. */
773
29.9k
  ret = asn1_check_tag(&cbs_seq, &length, NULL, NULL, &indefinite,
774
29.9k
      &constructed, tag_number, tag_class, optional);
775
29.9k
  if (ret == -1)
776
432
    return -1;
777
29.5k
  if (ret != 1) {
778
491
    ASN1error(ERR_R_NESTED_ASN1_ERROR);
779
491
    goto err;
780
491
  }
781
782
29.0k
  if (!constructed) {
783
15
    ASN1error(ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
784
15
    goto err;
785
15
  }
786
787
29.0k
  if (indefinite) {
788
1.87k
    eoc_needed = 1;
789
1.87k
    CBS_init(&cbs_seq_content, CBS_data(&cbs_seq), CBS_len(&cbs_seq));
790
27.1k
  } else {
791
27.1k
    eoc_needed = 0;
792
27.1k
    if (!CBS_get_bytes(&cbs_seq, &cbs_seq_content, length))
793
0
      goto err;
794
27.1k
  }
795
796
29.0k
  if (!ASN1_item_ex_new(&aseq, it)) {
797
0
    ASN1error(ERR_R_NESTED_ASN1_ERROR);
798
0
    goto err;
799
0
  }
800
801
29.0k
  if (asn1_cb != NULL && !asn1_cb(ASN1_OP_D2I_PRE, &aseq, it, NULL)) {
802
0
    ASN1error(ASN1_R_AUX_ERROR);
803
0
    goto err;
804
0
  }
805
806
100k
  for (i = 0; i < it->tcount; i++) {
807
76.6k
    at = &it->templates[i];
808
809
76.6k
    if (asn1_check_eoc(&cbs_seq_content)) {
810
170
      if (!indefinite) {
811
61
        ASN1error(ASN1_R_UNEXPECTED_EOC);
812
61
        goto err;
813
61
      }
814
109
      eoc_needed = 0;
815
109
      break;
816
170
    }
817
76.4k
    if (CBS_len(&cbs_seq_content) == 0)
818
2.86k
      break;
819
820
73.6k
    if ((seqat = asn1_do_adb(&aseq, at, 1)) == NULL)
821
0
      goto err;
822
823
73.6k
    pseqval = asn1_get_field_ptr(&aseq, seqat);
824
825
    /*
826
     * This was originally implemented to "increase efficiency",
827
     * however it currently needs to remain since it papers over
828
     * the use of ASN.1 ANY with OPTIONAL in SEQUENCEs (which
829
     * asn1_d2i_primitive() currently rejects).
830
     */
831
73.6k
    optional_field = (seqat->flags & ASN1_TFLG_OPTIONAL) != 0;
832
73.6k
    if (i == it->tcount - 1)
833
24.1k
      optional_field = 0;
834
835
73.6k
    ret = asn1_template_d2i(pseqval, &cbs_seq_content,
836
73.6k
        seqat, optional_field, depth);
837
73.6k
    if (ret == -1) {
838
      /* Absent OPTIONAL component. */
839
7.31k
      ASN1_template_free(pseqval, seqat);
840
7.31k
      continue;
841
7.31k
    }
842
66.3k
    if (ret != 1) {
843
2.22k
      errat = seqat;
844
2.22k
      goto err;
845
2.22k
    }
846
66.3k
  }
847
848
26.7k
  if (eoc_needed && !asn1_check_eoc(&cbs_seq_content)) {
849
101
    ASN1error(ASN1_R_MISSING_EOC);
850
101
    goto err;
851
101
  }
852
853
26.6k
  if (indefinite) {
854
450
    if (!CBS_skip(&cbs_seq, CBS_offset(&cbs_seq_content)))
855
0
      goto err;
856
26.1k
  } else if (CBS_len(&cbs_seq_content) != 0) {
857
14
    ASN1error(ASN1_R_SEQUENCE_LENGTH_MISMATCH);
858
14
    goto err;
859
14
  }
860
861
  /*
862
   * There is no more data in the ASN.1 SEQUENCE, however we may not have
863
   * populated all fields - check that any remaining are OPTIONAL.
864
   */
865
29.7k
  for (; i < it->tcount; i++) {
866
3.26k
    at = &it->templates[i];
867
868
3.26k
    if ((seqat = asn1_do_adb(&aseq, at, 1)) == NULL)
869
0
      goto err;
870
871
3.26k
    if ((seqat->flags & ASN1_TFLG_OPTIONAL) == 0) {
872
107
      ASN1error(ASN1_R_FIELD_MISSING);
873
107
      errat = seqat;
874
107
      goto err;
875
107
    }
876
877
    /* XXX - this is probably unnecessary with earlier free. */
878
3.15k
    pseqval = asn1_get_field_ptr(&aseq, seqat);
879
3.15k
    ASN1_template_free(pseqval, seqat);
880
3.15k
  }
881
882
26.5k
  if (!CBS_get_bytes(cbs, &cbs_object, CBS_offset(&cbs_seq)))
883
0
    goto err;
884
885
26.5k
  if (!asn1_enc_save(&aseq, &cbs_object, it)) {
886
0
    ASN1error(ERR_R_MALLOC_FAILURE);
887
0
    goto err;
888
0
  }
889
890
26.5k
  if (asn1_cb != NULL && !asn1_cb(ASN1_OP_D2I_POST, &aseq, it, NULL)) {
891
0
    ASN1error(ASN1_R_AUX_ERROR);
892
0
    goto err;
893
0
  }
894
895
26.5k
  *pval = aseq;
896
26.5k
  aseq = NULL;
897
898
26.5k
  return 1;
899
900
3.01k
 err:
901
3.01k
  ASN1_item_ex_free(&aseq, it);
902
903
3.01k
  if (errat != NULL)
904
2.33k
    ERR_asprintf_error_data("Field=%s, Type=%s", errat->field_name,
905
2.33k
        it->sname);
906
682
  else
907
682
    ERR_asprintf_error_data("Type=%s", it->sname);
908
909
3.01k
  return 0;
910
26.5k
}
911
912
static int
913
asn1_item_d2i_extern(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
914
    int tag_number, int tag_class, int optional)
915
3.25k
{
916
3.25k
  const ASN1_EXTERN_FUNCS *ef = it->funcs;
917
3.25k
  const unsigned char *p = NULL;
918
3.25k
  ASN1_TLC ctx = { 0 };
919
3.25k
  int ret = 0;
920
921
3.25k
  if (CBS_len(cbs) > LONG_MAX)
922
0
    return 0;
923
924
3.25k
  p = CBS_data(cbs);
925
926
3.25k
  if ((ret = ef->asn1_ex_d2i(pval, &p, (long)CBS_len(cbs), it,
927
3.25k
      tag_number, tag_class, optional, &ctx)) == 1) {
928
3.05k
    if (!CBS_skip(cbs, p - CBS_data(cbs)))
929
0
      goto err;
930
3.05k
  }
931
3.25k
  return ret;
932
933
0
 err:
934
0
  ASN1_item_ex_free(pval, it);
935
936
0
  ERR_asprintf_error_data("Type=%s", it->sname);
937
938
0
  return 0;
939
3.25k
}
940
941
static int
942
asn1_item_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_ITEM *it,
943
    int tag_number, int tag_class, int optional, int depth)
944
106k
{
945
106k
  if (pval == NULL)
946
0
    return 0;
947
948
106k
  if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
949
0
    ASN1error(ASN1_R_NESTED_TOO_DEEP);
950
0
    goto err;
951
0
  }
952
953
106k
  switch (it->itype) {
954
60.8k
  case ASN1_ITYPE_PRIMITIVE:
955
60.8k
    if (it->templates != NULL) {
956
      /*
957
       * Tagging or OPTIONAL is currently illegal on an item
958
       * template because the flags can't get passed down.
959
       * In practice this isn't a problem: we include the
960
       * relevant flags from the item template in the
961
       * template itself.
962
       */
963
12.9k
      if (tag_number != -1 || optional) {
964
0
        ASN1error(ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
965
0
        goto err;
966
0
      }
967
12.9k
      return asn1_template_d2i(pval, cbs, it->templates,
968
12.9k
          optional, depth);
969
12.9k
    }
970
47.9k
    return asn1_d2i_primitive(pval, cbs, it, tag_number, tag_class,
971
47.9k
        optional);
972
973
12.3k
  case ASN1_ITYPE_MSTRING:
974
12.3k
    return asn1_d2i_mstring(pval, cbs, it, tag_number, tag_class,
975
12.3k
        optional);
976
977
3.25k
  case ASN1_ITYPE_EXTERN:
978
3.25k
    return asn1_item_d2i_extern(pval, cbs, it, tag_number,
979
3.25k
        tag_class, optional);
980
981
377
  case ASN1_ITYPE_CHOICE:
982
377
    return asn1_item_d2i_choice(pval, cbs, it, tag_number,
983
377
        tag_class, optional, depth);
984
985
2
  case ASN1_ITYPE_NDEF_SEQUENCE:
986
29.9k
  case ASN1_ITYPE_SEQUENCE:
987
29.9k
    return asn1_item_d2i_sequence(pval, cbs, it, tag_number,
988
29.9k
        tag_class, optional, depth);
989
990
0
  default:
991
0
    return 0;
992
106k
  }
993
994
0
 err:
995
0
  ASN1_item_ex_free(pval, it);
996
997
0
  ERR_asprintf_error_data("Type=%s", it->sname);
998
999
0
  return 0;
1000
106k
}
1001
1002
static void
1003
14.9k
asn1_template_stack_of_free(STACK_OF(ASN1_VALUE) *avals, const ASN1_TEMPLATE *at) {
1004
14.9k
  ASN1_VALUE *aval;
1005
1006
14.9k
  if (avals == NULL)
1007
14.5k
    return;
1008
1009
2.38k
  while (sk_ASN1_VALUE_num(avals) > 0) {
1010
1.92k
    aval = sk_ASN1_VALUE_pop(avals);
1011
1.92k
    ASN1_item_ex_free(&aval, at->item);
1012
1.92k
  }
1013
457
  sk_ASN1_VALUE_free(avals);
1014
457
}
1015
1016
static int
1017
asn1_template_stack_of_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *at,
1018
    int optional, int depth)
1019
14.5k
{
1020
14.5k
  CBS cbs_object, cbs_object_content;
1021
14.5k
  STACK_OF(ASN1_VALUE) *avals = NULL;
1022
14.5k
  ASN1_VALUE *aval = NULL;
1023
14.5k
  int tag_number, tag_class;
1024
14.5k
  int eoc_needed;
1025
14.5k
  int indefinite;
1026
14.5k
  size_t length;
1027
14.5k
  int ret;
1028
1029
14.5k
  CBS_init(&cbs_object, CBS_data(cbs), CBS_len(cbs));
1030
1031
14.5k
  if (pval == NULL)
1032
0
    return 0;
1033
1034
14.5k
  asn1_template_stack_of_free((STACK_OF(ASN1_VALUE) *)*pval, at);
1035
14.5k
  *pval = NULL;
1036
1037
14.5k
  tag_number = at->tag;
1038
14.5k
  tag_class = at->flags & ASN1_TFLG_TAG_CLASS;
1039
1040
  /* Determine the inner tag value for SET OF or SEQUENCE OF. */
1041
14.5k
  if ((at->flags & ASN1_TFLG_IMPTAG) == 0) {
1042
14.2k
    tag_number = V_ASN1_SEQUENCE;
1043
14.2k
    tag_class = V_ASN1_UNIVERSAL;
1044
14.2k
    if ((at->flags & ASN1_TFLG_SET_OF) != 0)
1045
9.15k
      tag_number = V_ASN1_SET;
1046
14.2k
  }
1047
1048
14.5k
  ret = asn1_check_tag(&cbs_object, &length, NULL, NULL, &indefinite,
1049
14.5k
      NULL, tag_number, tag_class, optional);
1050
14.5k
  if (ret == -1)
1051
109
    return -1;
1052
14.4k
  if (ret != 1) {
1053
168
    ASN1error(ERR_R_NESTED_ASN1_ERROR);
1054
168
    return 0;
1055
168
  }
1056
1057
14.2k
  if (indefinite) {
1058
2.43k
    eoc_needed = 1;
1059
2.43k
    CBS_init(&cbs_object_content, CBS_data(&cbs_object),
1060
2.43k
        CBS_len(&cbs_object));
1061
11.8k
  } else {
1062
11.8k
    eoc_needed = 0;
1063
11.8k
    if (!CBS_get_bytes(&cbs_object, &cbs_object_content,
1064
11.8k
        length))
1065
0
      goto err;
1066
11.8k
  }
1067
1068
14.2k
  if ((avals = sk_ASN1_VALUE_new_null()) == NULL) {
1069
0
    ASN1error(ERR_R_MALLOC_FAILURE);
1070
0
    goto err;
1071
0
  }
1072
1073
  /* Read as many items as possible. */
1074
38.1k
  while (CBS_len(&cbs_object_content) > 0) {
1075
26.6k
    if (asn1_check_eoc(&cbs_object_content)) {
1076
2.33k
      if (!eoc_needed) {
1077
14
        ASN1error(ASN1_R_UNEXPECTED_EOC);
1078
14
        goto err;
1079
14
      }
1080
2.32k
      eoc_needed = 0;
1081
2.32k
      break;
1082
2.33k
    }
1083
24.3k
    if (!asn1_item_d2i(&aval, &cbs_object_content, at->item,
1084
24.3k
        -1, 0, 0, depth)) {
1085
439
      ASN1error(ERR_R_NESTED_ASN1_ERROR);
1086
439
      goto err;
1087
439
    }
1088
23.8k
    if (!sk_ASN1_VALUE_push(avals, aval)) {
1089
0
      ASN1error(ERR_R_MALLOC_FAILURE);
1090
0
      goto err;
1091
0
    }
1092
23.8k
    aval = NULL;
1093
23.8k
  }
1094
13.7k
  if (eoc_needed) {
1095
4
    ASN1error(ASN1_R_MISSING_EOC);
1096
4
    goto err;
1097
4
  }
1098
1099
13.7k
  if (indefinite) {
1100
2.32k
    if (!CBS_skip(&cbs_object, CBS_offset(&cbs_object_content)))
1101
0
      goto err;
1102
2.32k
  }
1103
1104
13.7k
  if (!CBS_skip(cbs, CBS_offset(&cbs_object)))
1105
0
    goto err;
1106
1107
13.7k
  *pval = (ASN1_VALUE *)avals;
1108
13.7k
  avals = NULL;
1109
1110
13.7k
  return 1;
1111
1112
457
 err:
1113
457
  asn1_template_stack_of_free(avals, at);
1114
457
  ASN1_item_ex_free(&aval, at->item);
1115
1116
457
  return 0;
1117
13.7k
}
1118
1119
static int
1120
asn1_template_noexp_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *at,
1121
    int optional, int depth)
1122
87.7k
{
1123
87.7k
  int tag_number, tag_class;
1124
87.7k
  int ret;
1125
1126
87.7k
  if (pval == NULL)
1127
0
    return 0;
1128
1129
87.7k
  if ((at->flags & ASN1_TFLG_SK_MASK) != 0)
1130
14.5k
    return asn1_template_stack_of_d2i(pval, cbs, at, optional, depth);
1131
1132
73.2k
  tag_number = -1;
1133
73.2k
  tag_class = V_ASN1_UNIVERSAL;
1134
1135
  /* See if we need to use IMPLICIT tagging. */
1136
73.2k
  if ((at->flags & ASN1_TFLG_IMPTAG) != 0) {
1137
4.61k
    tag_number = at->tag;
1138
4.61k
    tag_class = at->flags & ASN1_TFLG_TAG_CLASS;
1139
4.61k
  }
1140
1141
73.2k
  ret = asn1_item_d2i(pval, cbs, at->item, tag_number, tag_class,
1142
73.2k
      optional, depth);
1143
73.2k
  if (ret == -1)
1144
8.18k
    return -1;
1145
65.0k
  if (ret != 1) {
1146
2.18k
    ASN1error(ERR_R_NESTED_ASN1_ERROR);
1147
2.18k
    goto err;
1148
2.18k
  }
1149
1150
62.8k
  return 1;
1151
1152
2.18k
 err:
1153
  /* XXX - The called function should have freed already. */
1154
2.18k
  ASN1_template_free(pval, at);
1155
2.18k
  return 0;
1156
65.0k
}
1157
1158
static int
1159
asn1_template_d2i(ASN1_VALUE **pval, CBS *cbs, const ASN1_TEMPLATE *at,
1160
    int optional, int depth)
1161
88.3k
{
1162
88.3k
  CBS cbs_exp, cbs_exp_content;
1163
88.3k
  int constructed, indefinite;
1164
88.3k
  size_t length;
1165
88.3k
  int ret;
1166
1167
88.3k
  if (pval == NULL)
1168
0
    return 0;
1169
1170
  /* Check if EXPLICIT tag is expected. */
1171
88.3k
  if ((at->flags & ASN1_TFLG_EXPTAG) == 0)
1172
84.4k
    return asn1_template_noexp_d2i(pval, cbs, at, optional, depth);
1173
1174
3.89k
  CBS_init(&cbs_exp, CBS_data(cbs), CBS_len(cbs));
1175
1176
  /* Read ASN.1 header for EXPLICIT tagged object. */
1177
3.89k
  ret = asn1_check_tag(&cbs_exp, &length, NULL, NULL, &indefinite,
1178
3.89k
      &constructed, at->tag, at->flags & ASN1_TFLG_TAG_CLASS, optional);
1179
3.89k
  if (ret == -1)
1180
555
    return -1;
1181
3.34k
  if (ret != 1) {
1182
35
    ASN1error(ERR_R_NESTED_ASN1_ERROR);
1183
35
    return 0;
1184
35
  }
1185
1186
3.30k
  if (!constructed) {
1187
1
    ASN1error(ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
1188
1
    return 0;
1189
1
  }
1190
1191
3.30k
  if (indefinite) {
1192
6
    CBS_init(&cbs_exp_content, CBS_data(&cbs_exp), CBS_len(&cbs_exp));
1193
3.29k
  } else {
1194
3.29k
    if (!CBS_get_bytes(&cbs_exp, &cbs_exp_content, length))
1195
0
      goto err;
1196
3.29k
  }
1197
1198
3.30k
  if ((ret = asn1_template_noexp_d2i(pval, &cbs_exp_content, at, 0,
1199
3.30k
      depth)) != 1) {
1200
55
    ASN1error(ERR_R_NESTED_ASN1_ERROR);
1201
55
    return 0;
1202
55
  }
1203
1204
3.25k
  if (indefinite) {
1205
6
    if (!asn1_check_eoc(&cbs_exp_content)) {
1206
5
      ASN1error(ASN1_R_MISSING_EOC);
1207
5
      goto err;
1208
5
    }
1209
1
    if (!CBS_skip(&cbs_exp, CBS_offset(&cbs_exp_content)))
1210
0
      goto err;
1211
3.24k
  } else if (CBS_len(&cbs_exp_content) != 0) {
1212
6
    ASN1error(ASN1_R_SEQUENCE_LENGTH_MISMATCH);
1213
6
    goto err;
1214
6
  }
1215
1216
3.23k
  if (!CBS_skip(cbs, CBS_offset(&cbs_exp)))
1217
0
    goto err;
1218
1219
3.23k
  return 1;
1220
1221
11
 err:
1222
11
  ASN1_template_free(pval, at);
1223
11
  return 0;
1224
3.23k
}
1225
1226
ASN1_VALUE *
1227
ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long inlen,
1228
    const ASN1_ITEM *it)
1229
6.02k
{
1230
6.02k
  ASN1_VALUE *ptmpval = NULL;
1231
1232
6.02k
  if (pval == NULL)
1233
5.85k
    pval = &ptmpval;
1234
6.02k
  if (ASN1_item_ex_d2i(pval, in, inlen, it, -1, 0, 0, NULL) <= 0)
1235
2.00k
    return NULL;
1236
1237
4.01k
  return *pval;
1238
6.02k
}
1239
1240
int
1241
ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long inlen,
1242
    const ASN1_ITEM *it, int tag_number, int tag_class, char optional,
1243
    ASN1_TLC *ctx)
1244
9.28k
{
1245
9.28k
  CBS cbs;
1246
9.28k
  int ret;
1247
1248
9.28k
  if (inlen < 0)
1249
0
    return 0;
1250
1251
9.28k
  CBS_init(&cbs, *in, inlen);
1252
9.28k
  if ((ret = asn1_item_d2i(pval, &cbs, it, tag_number, tag_class,
1253
9.28k
      (int)optional, 0)) == 1)
1254
7.16k
    *in = CBS_data(&cbs);
1255
1256
9.28k
  return ret;
1257
9.28k
}
1258
1259
int
1260
ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
1261
    const ASN1_TEMPLATE *at)
1262
0
{
1263
0
  CBS cbs;
1264
0
  int ret;
1265
1266
0
  if (len < 0)
1267
0
    return 0;
1268
1269
0
  CBS_init(&cbs, *in, len);
1270
0
  if ((ret = asn1_template_d2i(pval, &cbs, at, 0, 0)) == 1)
1271
0
    *in = CBS_data(&cbs);
1272
1273
0
  return ret;
1274
0
}