Coverage Report

Created: 2023-06-29 07:25

/src/boringssl/crypto/asn1/tasn_dec.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
 * All rights reserved.
3
 *
4
 * This package is an SSL implementation written
5
 * by Eric Young (eay@cryptsoft.com).
6
 * The implementation was written so as to conform with Netscapes SSL.
7
 *
8
 * This library is free for commercial and non-commercial use as long as
9
 * the following conditions are aheared to.  The following conditions
10
 * apply to all code found in this distribution, be it the RC4, RSA,
11
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12
 * included with this distribution is covered by the same copyright terms
13
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
 *
15
 * Copyright remains Eric Young's, and as such any Copyright notices in
16
 * the code are not to be removed.
17
 * If this package is used in a product, Eric Young should be given attribution
18
 * as the author of the parts of the library used.
19
 * This can be in the form of a textual message at program startup or
20
 * in documentation (online or textual) provided with the package.
21
 *
22
 * Redistribution and use in source and binary forms, with or without
23
 * modification, are permitted provided that the following conditions
24
 * are met:
25
 * 1. Redistributions of source code must retain the copyright
26
 *    notice, this list of conditions and the following disclaimer.
27
 * 2. Redistributions in binary form must reproduce the above copyright
28
 *    notice, this list of conditions and the following disclaimer in the
29
 *    documentation and/or other materials provided with the distribution.
30
 * 3. All advertising materials mentioning features or use of this software
31
 *    must display the following acknowledgement:
32
 *    "This product includes cryptographic software written by
33
 *     Eric Young (eay@cryptsoft.com)"
34
 *    The word 'cryptographic' can be left out if the rouines from the library
35
 *    being used are not cryptographic related :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37
 *    the apps directory (application code) you must include an acknowledgement:
38
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 * The licence and distribution terms for any publically available version or
53
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54
 * copied and put under another distribution licence
55
 * [including the GNU Public Licence.] */
56
57
#include <openssl/asn1.h>
58
#include <openssl/asn1t.h>
59
#include <openssl/bytestring.h>
60
#include <openssl/err.h>
61
#include <openssl/mem.h>
62
#include <openssl/pool.h>
63
64
#include <assert.h>
65
#include <limits.h>
66
#include <string.h>
67
68
#include "../bytestring/internal.h"
69
#include "../internal.h"
70
#include "internal.h"
71
72
// Constructed types with a recursive definition (such as can be found in PKCS7)
73
// could eventually exceed the stack given malicious input with excessive
74
// recursion. Therefore we limit the stack depth. This is the maximum number of
75
// recursive invocations of asn1_item_embed_d2i().
76
8.73M
#define ASN1_MAX_CONSTRUCTED_NEST 30
77
78
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
79
                           char *cst, const unsigned char **in, long len,
80
                           int exptag, int expclass, char opt);
81
82
static int asn1_template_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
83
                                long len, const ASN1_TEMPLATE *tt, char opt,
84
                                CRYPTO_BUFFER *buf, int depth);
85
static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
86
                                   long len, const ASN1_TEMPLATE *tt, char opt,
87
                                   CRYPTO_BUFFER *buf, int depth);
88
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len,
89
                       int utype, const ASN1_ITEM *it);
90
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in,
91
                                 long len, const ASN1_ITEM *it, int tag,
92
                                 int aclass, char opt);
93
static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
94
                            long len, const ASN1_ITEM *it, int tag, int aclass,
95
                            char opt, CRYPTO_BUFFER *buf, int depth);
96
97
// Table to convert tags to bit values, used for MSTRING type
98
static const unsigned long tag2bit[31] = {
99
    0,  // (reserved)
100
    0,  // BOOLEAN
101
    0,  // INTEGER
102
    B_ASN1_BIT_STRING,
103
    B_ASN1_OCTET_STRING,
104
    0,               // NULL
105
    0,               // OBJECT IDENTIFIER
106
    B_ASN1_UNKNOWN,  // ObjectDescriptor
107
    B_ASN1_UNKNOWN,  // EXTERNAL
108
    B_ASN1_UNKNOWN,  // REAL
109
    B_ASN1_UNKNOWN,  // ENUMERATED
110
    B_ASN1_UNKNOWN,  // EMBEDDED PDV
111
    B_ASN1_UTF8STRING,
112
    B_ASN1_UNKNOWN,  // RELATIVE-OID
113
    B_ASN1_UNKNOWN,  // TIME
114
    B_ASN1_UNKNOWN,  // (reserved)
115
    B_ASN1_SEQUENCE,
116
    0,  // SET
117
    B_ASN1_NUMERICSTRING,
118
    B_ASN1_PRINTABLESTRING,
119
    B_ASN1_T61STRING,
120
    B_ASN1_VIDEOTEXSTRING,
121
    B_ASN1_IA5STRING,
122
    B_ASN1_UTCTIME,
123
    B_ASN1_GENERALIZEDTIME,
124
    B_ASN1_GRAPHICSTRING,
125
    B_ASN1_ISO64STRING,
126
    B_ASN1_GENERALSTRING,
127
    B_ASN1_UNIVERSALSTRING,
128
    B_ASN1_UNKNOWN,  // CHARACTER STRING
129
    B_ASN1_BMPSTRING,
130
};
131
132
2.16M
unsigned long ASN1_tag2bit(int tag) {
133
2.16M
  if (tag < 0 || tag > 30) {
134
27.8k
    return 0;
135
27.8k
  }
136
2.13M
  return tag2bit[tag];
137
2.16M
}
138
139
305k
static int is_supported_universal_type(int tag, int aclass) {
140
305k
  if (aclass != V_ASN1_UNIVERSAL) {
141
15.7k
    return 0;
142
15.7k
  }
143
289k
  return tag == V_ASN1_OBJECT || tag == V_ASN1_NULL || tag == V_ASN1_BOOLEAN ||
144
289k
         tag == V_ASN1_BIT_STRING || tag == V_ASN1_INTEGER ||
145
289k
         tag == V_ASN1_ENUMERATED || tag == V_ASN1_OCTET_STRING ||
146
289k
         tag == V_ASN1_NUMERICSTRING || tag == V_ASN1_PRINTABLESTRING ||
147
289k
         tag == V_ASN1_T61STRING || tag == V_ASN1_VIDEOTEXSTRING ||
148
289k
         tag == V_ASN1_IA5STRING || tag == V_ASN1_UTCTIME ||
149
289k
         tag == V_ASN1_GENERALIZEDTIME || tag == V_ASN1_GRAPHICSTRING ||
150
289k
         tag == V_ASN1_VISIBLESTRING || tag == V_ASN1_GENERALSTRING ||
151
289k
         tag == V_ASN1_UNIVERSALSTRING || tag == V_ASN1_BMPSTRING ||
152
289k
         tag == V_ASN1_UTF8STRING || tag == V_ASN1_SET ||
153
289k
         tag == V_ASN1_SEQUENCE;
154
305k
}
155
156
// Macro to initialize and invalidate the cache
157
158
// Decode an ASN1 item, this currently behaves just like a standard 'd2i'
159
// function. 'in' points to a buffer to read the data from, in future we
160
// will have more advanced versions that can input data a piece at a time and
161
// this will simply be a special case.
162
163
ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
164
462k
                          const ASN1_ITEM *it) {
165
462k
  ASN1_VALUE *ret = NULL;
166
462k
  if (asn1_item_ex_d2i(&ret, in, len, it, /*tag=*/-1, /*aclass=*/0, /*opt=*/0,
167
                       /*buf=*/NULL, /*depth=*/0) <= 0) {
168
    // Clean up, in case the caller left a partial object.
169
    //
170
    // TODO(davidben): I don't think it can leave one, but the codepaths below
171
    // are a bit inconsistent. Revisit this when rewriting this function.
172
9.65k
    ASN1_item_ex_free(&ret, it);
173
9.65k
  }
174
175
  // If the caller supplied an output pointer, free the old one and replace it
176
  // with |ret|. This differs from OpenSSL slightly in that we don't support
177
  // object reuse. We run this on both success and failure. On failure, even
178
  // with object reuse, OpenSSL destroys the previous object.
179
462k
  if (pval != NULL) {
180
0
    ASN1_item_ex_free(pval, it);
181
0
    *pval = ret;
182
0
  }
183
462k
  return ret;
184
462k
}
185
186
// Decode an item, taking care of IMPLICIT tagging, if any. If 'opt' set and
187
// tag mismatch return -1 to handle OPTIONAL
188
//
189
// TODO(davidben): Historically, all functions in this file had to account for
190
// |*pval| containing an arbitrary existing value. This is no longer the case
191
// because |ASN1_item_d2i| now always starts from NULL. As part of rewriting
192
// this function, take the simplified assumptions into account. Though we must
193
// still account for the internal calls to |ASN1_item_ex_new|.
194
195
static int asn1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
196
                            long len, const ASN1_ITEM *it, int tag, int aclass,
197
8.73M
                            char opt, CRYPTO_BUFFER *buf, int depth) {
198
8.73M
  const ASN1_TEMPLATE *tt, *errtt = NULL;
199
8.73M
  const unsigned char *p = NULL, *q;
200
8.73M
  unsigned char oclass;
201
8.73M
  char cst, isopt;
202
8.73M
  int i;
203
8.73M
  int otag;
204
8.73M
  int ret = 0;
205
8.73M
  ASN1_VALUE **pchptr;
206
8.73M
  if (!pval) {
207
0
    return 0;
208
0
  }
209
210
8.73M
  if (buf != NULL) {
211
3.57M
    assert(CRYPTO_BUFFER_data(buf) <= *in &&
212
3.57M
           *in + len <= CRYPTO_BUFFER_data(buf) + CRYPTO_BUFFER_len(buf));
213
3.57M
  }
214
215
  // Bound |len| to comfortably fit in an int. Lengths in this module often
216
  // switch between int and long without overflow checks.
217
8.73M
  if (len > INT_MAX / 2) {
218
0
    len = INT_MAX / 2;
219
0
  }
220
221
8.73M
  if (++depth > ASN1_MAX_CONSTRUCTED_NEST) {
222
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_TOO_DEEP);
223
0
    goto err;
224
0
  }
225
226
8.73M
  switch (it->itype) {
227
4.86M
    case ASN1_ITYPE_PRIMITIVE:
228
4.86M
      if (it->templates) {
229
        // tagging or OPTIONAL is currently illegal on an item template
230
        // because the flags can't get passed down. In practice this
231
        // isn't a problem: we include the relevant flags from the item
232
        // template in the template itself.
233
999k
        if ((tag != -1) || opt) {
234
0
          OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
235
0
          goto err;
236
0
        }
237
999k
        return asn1_template_ex_d2i(pval, in, len, it->templates, opt, buf,
238
999k
                                    depth);
239
999k
      }
240
3.86M
      return asn1_d2i_ex_primitive(pval, in, len, it, tag, aclass, opt);
241
0
      break;
242
243
1.19M
    case ASN1_ITYPE_MSTRING:
244
      // It never makes sense for multi-strings to have implicit tagging, so
245
      // if tag != -1, then this looks like an error in the template.
246
1.19M
      if (tag != -1) {
247
0
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
248
0
        goto err;
249
0
      }
250
251
1.19M
      p = *in;
252
      // Just read in tag and class
253
1.19M
      ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, &p, len, -1, 0, 1);
254
1.19M
      if (!ret) {
255
151
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
256
151
        goto err;
257
151
      }
258
259
      // Must be UNIVERSAL class
260
1.19M
      if (oclass != V_ASN1_UNIVERSAL) {
261
        // If OPTIONAL, assume this is OK
262
37
        if (opt) {
263
0
          return -1;
264
0
        }
265
37
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_MSTRING_NOT_UNIVERSAL);
266
37
        goto err;
267
37
      }
268
      // Check tag matches bit map
269
1.19M
      if (!(ASN1_tag2bit(otag) & it->utype)) {
270
        // If OPTIONAL, assume this is OK
271
86
        if (opt) {
272
0
          return -1;
273
0
        }
274
86
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_MSTRING_WRONG_TAG);
275
86
        goto err;
276
86
      }
277
1.19M
      return asn1_d2i_ex_primitive(pval, in, len, it, otag, 0, 0);
278
279
253k
    case ASN1_ITYPE_EXTERN: {
280
      // We don't support implicit tagging with external types.
281
253k
      if (tag != -1) {
282
0
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
283
0
        goto err;
284
0
      }
285
253k
      const ASN1_EXTERN_FUNCS *ef = it->funcs;
286
253k
      return ef->asn1_ex_d2i(pval, in, len, it, opt, NULL);
287
253k
    }
288
289
115k
    case ASN1_ITYPE_CHOICE: {
290
      // It never makes sense for CHOICE types to have implicit tagging, so if
291
      // tag != -1, then this looks like an error in the template.
292
115k
      if (tag != -1) {
293
0
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE);
294
0
        goto err;
295
0
      }
296
297
115k
      const ASN1_AUX *aux = it->funcs;
298
115k
      ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
299
115k
      if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) {
300
0
        goto auxerr;
301
0
      }
302
303
115k
      if (*pval) {
304
        // Free up and zero CHOICE value if initialised
305
997
        i = asn1_get_choice_selector(pval, it);
306
997
        if ((i >= 0) && (i < it->tcount)) {
307
0
          tt = it->templates + i;
308
0
          pchptr = asn1_get_field_ptr(pval, tt);
309
0
          ASN1_template_free(pchptr, tt);
310
0
          asn1_set_choice_selector(pval, -1, it);
311
0
        }
312
114k
      } else if (!ASN1_item_ex_new(pval, it)) {
313
0
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
314
0
        goto err;
315
0
      }
316
      // CHOICE type, try each possibility in turn
317
115k
      p = *in;
318
703k
      for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
319
703k
        pchptr = asn1_get_field_ptr(pval, tt);
320
        // We mark field as OPTIONAL so its absence can be recognised.
321
703k
        ret = asn1_template_ex_d2i(pchptr, &p, len, tt, 1, buf, depth);
322
        // If field not present, try the next one
323
703k
        if (ret == -1) {
324
587k
          continue;
325
587k
        }
326
        // If positive return, read OK, break loop
327
115k
        if (ret > 0) {
328
114k
          break;
329
114k
        }
330
        // Otherwise must be an ASN1 parsing error
331
872
        errtt = tt;
332
872
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
333
872
        goto err;
334
115k
      }
335
336
      // Did we fall off the end without reading anything?
337
115k
      if (i == it->tcount) {
338
        // If OPTIONAL, this is OK
339
133
        if (opt) {
340
          // Free and zero it
341
0
          ASN1_item_ex_free(pval, it);
342
0
          return -1;
343
0
        }
344
133
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE);
345
133
        goto err;
346
133
      }
347
348
114k
      asn1_set_choice_selector(pval, i, it);
349
114k
      if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) {
350
0
        goto auxerr;
351
0
      }
352
114k
      *in = p;
353
114k
      return 1;
354
114k
    }
355
356
2.31M
    case ASN1_ITYPE_SEQUENCE: {
357
2.31M
      p = *in;
358
359
      // If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL
360
2.31M
      if (tag == -1) {
361
2.10M
        tag = V_ASN1_SEQUENCE;
362
2.10M
        aclass = V_ASN1_UNIVERSAL;
363
2.10M
      }
364
      // Get SEQUENCE length and update len, p
365
2.31M
      ret = asn1_check_tlen(&len, NULL, NULL, &cst, &p, len, tag, aclass, opt);
366
2.31M
      if (!ret) {
367
3.39k
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
368
3.39k
        goto err;
369
2.30M
      } else if (ret == -1) {
370
200k
        return -1;
371
200k
      }
372
2.10M
      if (!cst) {
373
93
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
374
93
        goto err;
375
93
      }
376
377
2.10M
      if (!*pval && !ASN1_item_ex_new(pval, it)) {
378
0
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
379
0
        goto err;
380
0
      }
381
382
2.10M
      const ASN1_AUX *aux = it->funcs;
383
2.10M
      ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL;
384
2.10M
      if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) {
385
0
        goto auxerr;
386
0
      }
387
388
      // Free up and zero any ADB found
389
7.77M
      for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
390
5.67M
        if (tt->flags & ASN1_TFLG_ADB_MASK) {
391
890
          const ASN1_TEMPLATE *seqtt;
392
890
          ASN1_VALUE **pseqval;
393
890
          seqtt = asn1_do_adb(pval, tt, 0);
394
890
          if (seqtt == NULL) {
395
890
            continue;
396
890
          }
397
0
          pseqval = asn1_get_field_ptr(pval, seqtt);
398
0
          ASN1_template_free(pseqval, seqtt);
399
0
        }
400
5.67M
      }
401
402
      // Get each field entry
403
7.60M
      for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
404
5.60M
        const ASN1_TEMPLATE *seqtt;
405
5.60M
        ASN1_VALUE **pseqval;
406
5.60M
        seqtt = asn1_do_adb(pval, tt, 1);
407
5.60M
        if (seqtt == NULL) {
408
0
          goto err;
409
0
        }
410
5.60M
        pseqval = asn1_get_field_ptr(pval, seqtt);
411
        // Have we ran out of data?
412
5.60M
        if (!len) {
413
97.3k
          break;
414
97.3k
        }
415
5.50M
        q = p;
416
        // This determines the OPTIONAL flag value. The field cannot be
417
        // omitted if it is the last of a SEQUENCE and there is still
418
        // data to be read. This isn't strictly necessary but it
419
        // increases efficiency in some cases.
420
5.50M
        if (i == (it->tcount - 1)) {
421
2.00M
          isopt = 0;
422
3.50M
        } else {
423
3.50M
          isopt = (seqtt->flags & ASN1_TFLG_OPTIONAL) != 0;
424
3.50M
        }
425
        // attempt to read in field, allowing each to be OPTIONAL
426
427
5.50M
        ret = asn1_template_ex_d2i(pseqval, &p, len, seqtt, isopt, buf, depth);
428
5.50M
        if (!ret) {
429
13.9k
          errtt = seqtt;
430
13.9k
          goto err;
431
5.49M
        } else if (ret == -1) {
432
          // OPTIONAL component absent. Free and zero the field.
433
573k
          ASN1_template_free(pseqval, seqtt);
434
573k
          continue;
435
573k
        }
436
        // Update length
437
4.92M
        len -= p - q;
438
4.92M
      }
439
440
      // Check all data read
441
2.09M
      if (len) {
442
626
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_SEQUENCE_LENGTH_MISMATCH);
443
626
        goto err;
444
626
      }
445
446
      // If we get here we've got no more data in the SEQUENCE, however we
447
      // may not have read all fields so check all remaining are OPTIONAL
448
      // and clear any that are.
449
2.19M
      for (; i < it->tcount; tt++, i++) {
450
108k
        const ASN1_TEMPLATE *seqtt;
451
108k
        seqtt = asn1_do_adb(pval, tt, 1);
452
108k
        if (seqtt == NULL) {
453
0
          goto err;
454
0
        }
455
108k
        if (seqtt->flags & ASN1_TFLG_OPTIONAL) {
456
106k
          ASN1_VALUE **pseqval;
457
106k
          pseqval = asn1_get_field_ptr(pval, seqtt);
458
106k
          ASN1_template_free(pseqval, seqtt);
459
106k
        } else {
460
1.56k
          errtt = seqtt;
461
1.56k
          OPENSSL_PUT_ERROR(ASN1, ASN1_R_FIELD_MISSING);
462
1.56k
          goto err;
463
1.56k
        }
464
108k
      }
465
      // Save encoding
466
2.09M
      if (!asn1_enc_save(pval, *in, p - *in, it, buf)) {
467
0
        goto auxerr;
468
0
      }
469
2.09M
      if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) {
470
0
        goto auxerr;
471
0
      }
472
2.09M
      *in = p;
473
2.09M
      return 1;
474
2.09M
    }
475
476
0
    default:
477
0
      return 0;
478
8.73M
  }
479
0
auxerr:
480
0
  OPENSSL_PUT_ERROR(ASN1, ASN1_R_AUX_ERROR);
481
20.8k
err:
482
20.8k
  ASN1_item_ex_free(pval, it);
483
20.8k
  if (errtt) {
484
16.3k
    ERR_add_error_data(4, "Field=", errtt->field_name, ", Type=", it->sname);
485
16.3k
  } else {
486
4.51k
    ERR_add_error_data(2, "Type=", it->sname);
487
4.51k
  }
488
20.8k
  return 0;
489
0
}
490
491
int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
492
                     const ASN1_ITEM *it, int tag, int aclass, char opt,
493
385k
                     CRYPTO_BUFFER *buf) {
494
385k
  return asn1_item_ex_d2i(pval, in, len, it, tag, aclass, opt, buf,
495
385k
                          /*depth=*/0);
496
385k
}
497
498
// Templates are handled with two separate functions. One handles any
499
// EXPLICIT tag and the other handles the rest.
500
501
static int asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in,
502
                                long inlen, const ASN1_TEMPLATE *tt, char opt,
503
7.21M
                                CRYPTO_BUFFER *buf, int depth) {
504
7.21M
  int aclass;
505
7.21M
  int ret;
506
7.21M
  long len;
507
7.21M
  const unsigned char *p, *q;
508
7.21M
  if (!val) {
509
0
    return 0;
510
0
  }
511
7.21M
  uint32_t flags = tt->flags;
512
7.21M
  aclass = flags & ASN1_TFLG_TAG_CLASS;
513
514
7.21M
  p = *in;
515
516
  // Check if EXPLICIT tag expected
517
7.21M
  if (flags & ASN1_TFLG_EXPTAG) {
518
342k
    char cst;
519
    // Need to work out amount of data available to the inner content and
520
    // where it starts: so read in EXPLICIT header to get the info.
521
342k
    ret = asn1_check_tlen(&len, NULL, NULL, &cst, &p, inlen, tt->tag, aclass,
522
342k
                          opt);
523
342k
    q = p;
524
342k
    if (!ret) {
525
488
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
526
488
      return 0;
527
341k
    } else if (ret == -1) {
528
95.0k
      return -1;
529
95.0k
    }
530
246k
    if (!cst) {
531
29
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
532
29
      return 0;
533
29
    }
534
    // We've found the field so it can't be OPTIONAL now
535
246k
    ret = asn1_template_noexp_d2i(val, &p, len, tt, /*opt=*/0, buf, depth);
536
246k
    if (!ret) {
537
1.08k
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
538
1.08k
      return 0;
539
1.08k
    }
540
    // We read the field in OK so update length
541
245k
    len -= p - q;
542
    // Check for trailing data.
543
245k
    if (len) {
544
125
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_EXPLICIT_LENGTH_MISMATCH);
545
125
      goto err;
546
125
    }
547
6.86M
  } else {
548
6.86M
    return asn1_template_noexp_d2i(val, in, inlen, tt, opt, buf, depth);
549
6.86M
  }
550
551
245k
  *in = p;
552
245k
  return 1;
553
554
125
err:
555
125
  ASN1_template_free(val, tt);
556
125
  return 0;
557
7.21M
}
558
559
static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
560
                                   long len, const ASN1_TEMPLATE *tt, char opt,
561
7.11M
                                   CRYPTO_BUFFER *buf, int depth) {
562
7.11M
  int aclass;
563
7.11M
  int ret;
564
7.11M
  const unsigned char *p;
565
7.11M
  if (!val) {
566
0
    return 0;
567
0
  }
568
7.11M
  uint32_t flags = tt->flags;
569
7.11M
  aclass = flags & ASN1_TFLG_TAG_CLASS;
570
571
7.11M
  p = *in;
572
573
7.11M
  if (flags & ASN1_TFLG_SK_MASK) {
574
    // SET OF, SEQUENCE OF
575
1.11M
    int sktag, skaclass;
576
    // First work out expected inner tag value
577
1.11M
    if (flags & ASN1_TFLG_IMPTAG) {
578
1.69k
      sktag = tt->tag;
579
1.69k
      skaclass = aclass;
580
1.11M
    } else {
581
1.11M
      skaclass = V_ASN1_UNIVERSAL;
582
1.11M
      if (flags & ASN1_TFLG_SET_OF) {
583
739k
        sktag = V_ASN1_SET;
584
739k
      } else {
585
378k
        sktag = V_ASN1_SEQUENCE;
586
378k
      }
587
1.11M
    }
588
    // Get the tag
589
1.11M
    ret =
590
1.11M
        asn1_check_tlen(&len, NULL, NULL, NULL, &p, len, sktag, skaclass, opt);
591
1.11M
    if (!ret) {
592
3.13k
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
593
3.13k
      return 0;
594
1.11M
    } else if (ret == -1) {
595
343
      return -1;
596
343
    }
597
1.11M
    if (!*val) {
598
1.11M
      *val = (ASN1_VALUE *)sk_ASN1_VALUE_new_null();
599
1.11M
    } else {
600
      // We've got a valid STACK: free up any items present
601
7
      STACK_OF(ASN1_VALUE) *sktmp = (STACK_OF(ASN1_VALUE) *)*val;
602
7
      ASN1_VALUE *vtmp;
603
7
      while (sk_ASN1_VALUE_num(sktmp) > 0) {
604
0
        vtmp = sk_ASN1_VALUE_pop(sktmp);
605
0
        ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
606
0
      }
607
7
    }
608
609
1.11M
    if (!*val) {
610
0
      goto err;
611
0
    }
612
613
    // Read as many items as we can
614
3.00M
    while (len > 0) {
615
1.89M
      ASN1_VALUE *skfield;
616
1.89M
      const unsigned char *q = p;
617
1.89M
      skfield = NULL;
618
1.89M
      if (!asn1_item_ex_d2i(&skfield, &p, len, ASN1_ITEM_ptr(tt->item),
619
1.89M
                            /*tag=*/-1, /*aclass=*/0, /*opt=*/0, buf, depth)) {
620
4.66k
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
621
4.66k
        goto err;
622
4.66k
      }
623
1.88M
      len -= p - q;
624
1.88M
      if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, skfield)) {
625
0
        ASN1_item_ex_free(&skfield, ASN1_ITEM_ptr(tt->item));
626
0
        goto err;
627
0
      }
628
1.88M
    }
629
5.99M
  } else if (flags & ASN1_TFLG_IMPTAG) {
630
    // IMPLICIT tagging
631
856k
    ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), tt->tag,
632
856k
                           aclass, opt, buf, depth);
633
856k
    if (!ret) {
634
1.17k
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
635
1.17k
      goto err;
636
854k
    } else if (ret == -1) {
637
738k
      return -1;
638
738k
    }
639
5.14M
  } else {
640
    // Nothing special
641
5.14M
    ret = asn1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), /*tag=*/-1,
642
5.14M
                           /*aclass=*/0, opt, buf, depth);
643
5.14M
    if (!ret) {
644
11.7k
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
645
11.7k
      goto err;
646
5.12M
    } else if (ret == -1) {
647
327k
      return -1;
648
327k
    }
649
5.14M
  }
650
651
6.02M
  *in = p;
652
6.02M
  return 1;
653
654
17.6k
err:
655
17.6k
  ASN1_template_free(val, tt);
656
17.6k
  return 0;
657
7.11M
}
658
659
static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in,
660
                                 long inlen, const ASN1_ITEM *it, int tag,
661
5.05M
                                 int aclass, char opt) {
662
5.05M
  int ret = 0, utype;
663
5.05M
  long plen;
664
5.05M
  char cst;
665
5.05M
  const unsigned char *p;
666
5.05M
  const unsigned char *cont = NULL;
667
5.05M
  long len;
668
5.05M
  if (!pval) {
669
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_NULL);
670
0
    return 0;  // Should never happen
671
0
  }
672
673
5.05M
  if (it->itype == ASN1_ITYPE_MSTRING) {
674
1.19M
    utype = tag;
675
1.19M
    tag = -1;
676
3.86M
  } else {
677
3.86M
    utype = it->utype;
678
3.86M
  }
679
680
5.05M
  if (utype == V_ASN1_ANY) {
681
    // If type is ANY need to figure out type from tag
682
305k
    unsigned char oclass;
683
305k
    if (tag >= 0) {
684
0
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_TAGGED_ANY);
685
0
      return 0;
686
0
    }
687
305k
    if (opt) {
688
0
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OPTIONAL_ANY);
689
0
      return 0;
690
0
    }
691
305k
    p = *in;
692
305k
    ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, &p, inlen, -1, 0, 0);
693
305k
    if (!ret) {
694
129
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
695
129
      return 0;
696
129
    }
697
305k
    if (!is_supported_universal_type(utype, oclass)) {
698
19.6k
      utype = V_ASN1_OTHER;
699
19.6k
    }
700
305k
  }
701
5.05M
  if (tag == -1) {
702
4.40M
    tag = utype;
703
4.40M
    aclass = V_ASN1_UNIVERSAL;
704
4.40M
  }
705
5.05M
  p = *in;
706
  // Check header
707
5.05M
  ret = asn1_check_tlen(&plen, NULL, NULL, &cst, &p, inlen, tag, aclass, opt);
708
5.05M
  if (!ret) {
709
3.36k
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_NESTED_ASN1_ERROR);
710
3.36k
    return 0;
711
5.05M
  } else if (ret == -1) {
712
865k
    return -1;
713
865k
  }
714
4.18M
  ret = 0;
715
  // SEQUENCE, SET and "OTHER" are left in encoded form
716
4.18M
  if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
717
4.18M
      (utype == V_ASN1_OTHER)) {
718
    // SEQUENCE and SET must be constructed
719
57.9k
    if (utype != V_ASN1_OTHER && !cst) {
720
70
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_CONSTRUCTED);
721
70
      return 0;
722
70
    }
723
724
57.9k
    cont = *in;
725
57.9k
    len = p - cont + plen;
726
57.9k
    p += plen;
727
4.12M
  } else if (cst) {
728
    // This parser historically supported BER constructed strings. We no
729
    // longer do and will gradually tighten this parser into a DER
730
    // parser. BER types should use |CBS_asn1_ber_to_der|.
731
255
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_TYPE_NOT_PRIMITIVE);
732
255
    return 0;
733
4.12M
  } else {
734
4.12M
    cont = p;
735
4.12M
    len = plen;
736
4.12M
    p += plen;
737
4.12M
  }
738
739
  // We now have content length and type: translate into a structure
740
4.18M
  if (!asn1_ex_c2i(pval, cont, len, utype, it)) {
741
5.37k
    goto err;
742
5.37k
  }
743
744
4.18M
  *in = p;
745
4.18M
  ret = 1;
746
4.18M
err:
747
4.18M
  return ret;
748
4.18M
}
749
750
// Translate ASN1 content octets into a structure
751
752
static int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, long len,
753
4.18M
                       int utype, const ASN1_ITEM *it) {
754
4.18M
  ASN1_VALUE **opval = NULL;
755
4.18M
  ASN1_STRING *stmp;
756
4.18M
  ASN1_TYPE *typ = NULL;
757
4.18M
  int ret = 0;
758
4.18M
  ASN1_INTEGER **tint;
759
760
  // Historically, |it->funcs| for primitive types contained an
761
  // |ASN1_PRIMITIVE_FUNCS| table of callbacks.
762
4.18M
  assert(it->funcs == NULL);
763
764
  // If ANY type clear type and set pointer to internal value
765
4.18M
  if (it->utype == V_ASN1_ANY) {
766
305k
    if (!*pval) {
767
303k
      typ = ASN1_TYPE_new();
768
303k
      if (typ == NULL) {
769
0
        goto err;
770
0
      }
771
303k
      *pval = (ASN1_VALUE *)typ;
772
303k
    } else {
773
2.11k
      typ = (ASN1_TYPE *)*pval;
774
2.11k
    }
775
776
305k
    if (utype != typ->type) {
777
305k
      ASN1_TYPE_set(typ, utype, NULL);
778
305k
    }
779
305k
    opval = pval;
780
305k
    pval = &typ->value.asn1_value;
781
305k
  }
782
4.18M
  switch (utype) {
783
1.75M
    case V_ASN1_OBJECT:
784
1.75M
      if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) {
785
209
        goto err;
786
209
      }
787
1.75M
      break;
788
789
1.75M
    case V_ASN1_NULL:
790
222k
      if (len) {
791
71
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_NULL_IS_WRONG_LENGTH);
792
71
        goto err;
793
71
      }
794
221k
      *pval = (ASN1_VALUE *)1;
795
221k
      break;
796
797
69.6k
    case V_ASN1_BOOLEAN:
798
69.6k
      if (len != 1) {
799
111
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
800
111
        goto err;
801
69.5k
      } else {
802
69.5k
        ASN1_BOOLEAN *tbool;
803
69.5k
        tbool = (ASN1_BOOLEAN *)pval;
804
69.5k
        *tbool = *cont;
805
69.5k
      }
806
69.5k
      break;
807
808
140k
    case V_ASN1_BIT_STRING:
809
140k
      if (!c2i_ASN1_BIT_STRING((ASN1_BIT_STRING **)pval, &cont, len)) {
810
749
        goto err;
811
749
      }
812
140k
      break;
813
814
266k
    case V_ASN1_INTEGER:
815
320k
    case V_ASN1_ENUMERATED:
816
320k
      tint = (ASN1_INTEGER **)pval;
817
320k
      if (!c2i_ASN1_INTEGER(tint, &cont, len)) {
818
180
        goto err;
819
180
      }
820
      // Fixup type to match the expected form
821
320k
      (*tint)->type = utype | ((*tint)->type & V_ASN1_NEG);
822
320k
      break;
823
824
404k
    case V_ASN1_OCTET_STRING:
825
406k
    case V_ASN1_NUMERICSTRING:
826
656k
    case V_ASN1_PRINTABLESTRING:
827
663k
    case V_ASN1_T61STRING:
828
663k
    case V_ASN1_VIDEOTEXSTRING:
829
766k
    case V_ASN1_IA5STRING:
830
1.01M
    case V_ASN1_UTCTIME:
831
1.01M
    case V_ASN1_GENERALIZEDTIME:
832
1.01M
    case V_ASN1_GRAPHICSTRING:
833
1.01M
    case V_ASN1_VISIBLESTRING:
834
1.01M
    case V_ASN1_GENERALSTRING:
835
1.02M
    case V_ASN1_UNIVERSALSTRING:
836
1.03M
    case V_ASN1_BMPSTRING:
837
1.61M
    case V_ASN1_UTF8STRING:
838
1.63M
    case V_ASN1_OTHER:
839
1.63M
    case V_ASN1_SET:
840
1.67M
    case V_ASN1_SEQUENCE:
841
    // TODO(crbug.com/boringssl/412): This default case should be removed, now
842
    // that we've resolved https://crbug.com/boringssl/561. However, it is still
843
    // needed to support some edge cases in |ASN1_PRINTABLE|. |ASN1_PRINTABLE|
844
    // broadly doesn't tolerate unrecognized universal tags, but except for
845
    // eight values that map to |B_ASN1_UNKNOWN| instead of zero. See the
846
    // X509Test.NameAttributeValues test.
847
1.67M
    default: {
848
1.67M
      CBS cbs;
849
1.67M
      CBS_init(&cbs, cont, (size_t)len);
850
1.67M
      if (utype == V_ASN1_BMPSTRING) {
851
6.00M
        while (CBS_len(&cbs) != 0) {
852
6.00M
          uint32_t c;
853
6.00M
          if (!cbs_get_ucs2_be(&cbs, &c)) {
854
304
            OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BMPSTRING);
855
304
            goto err;
856
304
          }
857
6.00M
        }
858
6.67k
      }
859
1.67M
      if (utype == V_ASN1_UNIVERSALSTRING) {
860
384k
        while (CBS_len(&cbs) != 0) {
861
379k
          uint32_t c;
862
379k
          if (!cbs_get_utf32_be(&cbs, &c)) {
863
592
            OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UNIVERSALSTRING);
864
592
            goto err;
865
592
          }
866
379k
        }
867
4.81k
      }
868
1.67M
      if (utype == V_ASN1_UTF8STRING) {
869
147M
        while (CBS_len(&cbs) != 0) {
870
147M
          uint32_t c;
871
147M
          if (!cbs_get_utf8(&cbs, &c)) {
872
643
            OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UTF8STRING);
873
643
            goto err;
874
643
          }
875
147M
        }
876
581k
      }
877
1.67M
      if (utype == V_ASN1_UTCTIME) {
878
249k
        if (!CBS_parse_utc_time(&cbs, NULL, /*allow_timezone_offset=*/1)) {
879
1.16k
          OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_TIME_FORMAT);
880
1.16k
          goto err;
881
1.16k
        }
882
249k
      }
883
1.67M
      if (utype == V_ASN1_GENERALIZEDTIME) {
884
2.54k
        if (!CBS_parse_generalized_time(&cbs, NULL,
885
2.54k
                                        /*allow_timezone_offset=*/0)) {
886
1.34k
          OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_TIME_FORMAT);
887
1.34k
          goto err;
888
1.34k
        }
889
2.54k
      }
890
      // TODO(https://crbug.com/boringssl/427): Check other string types.
891
892
      // All based on ASN1_STRING and handled the same
893
1.66M
      if (!*pval) {
894
142k
        stmp = ASN1_STRING_type_new(utype);
895
142k
        if (!stmp) {
896
0
          goto err;
897
0
        }
898
142k
        *pval = (ASN1_VALUE *)stmp;
899
1.52M
      } else {
900
1.52M
        stmp = (ASN1_STRING *)*pval;
901
1.52M
        stmp->type = utype;
902
1.52M
      }
903
1.66M
      if (!ASN1_STRING_set(stmp, cont, len)) {
904
0
        ASN1_STRING_free(stmp);
905
0
        *pval = NULL;
906
0
        goto err;
907
0
      }
908
1.66M
      break;
909
1.66M
    }
910
4.18M
  }
911
  // If ASN1_ANY and NULL type fix up value
912
4.18M
  if (typ && (utype == V_ASN1_NULL)) {
913
221k
    typ->value.ptr = NULL;
914
221k
  }
915
916
4.18M
  ret = 1;
917
4.18M
err:
918
4.18M
  if (!ret) {
919
5.37k
    ASN1_TYPE_free(typ);
920
5.37k
    if (opval) {
921
1.97k
      *opval = NULL;
922
1.97k
    }
923
5.37k
  }
924
4.18M
  return ret;
925
4.18M
}
926
927
// Check an ASN1 tag and length: a bit like ASN1_get_object but it
928
// checks the expected tag.
929
930
static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
931
                           char *cst, const unsigned char **in, long len,
932
10.3M
                           int exptag, int expclass, char opt) {
933
10.3M
  int i;
934
10.3M
  int ptag, pclass;
935
10.3M
  long plen;
936
10.3M
  const unsigned char *p;
937
10.3M
  p = *in;
938
939
10.3M
  i = ASN1_get_object(&p, &plen, &ptag, &pclass, len);
940
10.3M
  if (i & 0x80) {
941
6.10k
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_OBJECT_HEADER);
942
6.10k
    return 0;
943
6.10k
  }
944
10.3M
  if (exptag >= 0) {
945
8.80M
    if ((exptag != ptag) || (expclass != pclass)) {
946
      // If type is OPTIONAL, not an error: indicate missing type.
947
1.16M
      if (opt) {
948
1.16M
        return -1;
949
1.16M
      }
950
4.56k
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TAG);
951
4.56k
      return 0;
952
1.16M
    }
953
8.80M
  }
954
955
9.15M
  if (cst) {
956
6.54M
    *cst = i & V_ASN1_CONSTRUCTED;
957
6.54M
  }
958
959
9.15M
  if (olen) {
960
7.65M
    *olen = plen;
961
7.65M
  }
962
963
9.15M
  if (oclass) {
964
1.50M
    *oclass = pclass;
965
1.50M
  }
966
967
9.15M
  if (otag) {
968
1.50M
    *otag = ptag;
969
1.50M
  }
970
971
9.15M
  *in = p;
972
9.15M
  return 1;
973
10.3M
}