Coverage Report

Created: 2022-08-24 06:30

/src/libressl/crypto/asn1/asn1_gen.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: asn1_gen.c,v 1.19 2022/05/24 19:56:13 tb Exp $ */
2
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3
 * project 2002.
4
 */
5
/* ====================================================================
6
 * Copyright (c) 2002 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 <string.h>
60
61
#include <openssl/asn1.h>
62
#include <openssl/err.h>
63
#include <openssl/x509v3.h>
64
65
#include "asn1_locl.h"
66
67
0
#define ASN1_GEN_FLAG   0x10000
68
0
#define ASN1_GEN_FLAG_IMP (ASN1_GEN_FLAG|1)
69
0
#define ASN1_GEN_FLAG_EXP (ASN1_GEN_FLAG|2)
70
#define ASN1_GEN_FLAG_TAG (ASN1_GEN_FLAG|3)
71
0
#define ASN1_GEN_FLAG_BITWRAP (ASN1_GEN_FLAG|4)
72
0
#define ASN1_GEN_FLAG_OCTWRAP (ASN1_GEN_FLAG|5)
73
0
#define ASN1_GEN_FLAG_SEQWRAP (ASN1_GEN_FLAG|6)
74
0
#define ASN1_GEN_FLAG_SETWRAP (ASN1_GEN_FLAG|7)
75
0
#define ASN1_GEN_FLAG_FORMAT  (ASN1_GEN_FLAG|8)
76
77
0
#define ASN1_GEN_STR(str,val){str, sizeof(str) - 1, val}
78
79
0
#define ASN1_FLAG_EXP_MAX 20
80
81
/* Input formats */
82
83
/* ASCII: default */
84
0
#define ASN1_GEN_FORMAT_ASCII 1
85
/* UTF8 */
86
0
#define ASN1_GEN_FORMAT_UTF8  2
87
/* Hex */
88
0
#define ASN1_GEN_FORMAT_HEX 3
89
/* List of bits */
90
0
#define ASN1_GEN_FORMAT_BITLIST 4
91
92
struct tag_name_st {
93
  const char *strnam;
94
  int len;
95
  int tag;
96
};
97
98
typedef struct {
99
  int exp_tag;
100
  int exp_class;
101
  int exp_constructed;
102
  int exp_pad;
103
  long exp_len;
104
} tag_exp_type;
105
106
typedef struct {
107
  int imp_tag;
108
  int imp_class;
109
  int utype;
110
  int format;
111
  const char *str;
112
  tag_exp_type exp_list[ASN1_FLAG_EXP_MAX];
113
  int exp_count;
114
} tag_exp_arg;
115
116
static int bitstr_cb(const char *elem, int len, void *bitstr);
117
static int asn1_cb(const char *elem, int len, void *bitstr);
118
static int append_exp(tag_exp_arg *arg, int exp_tag, int exp_class,
119
    int exp_constructed, int exp_pad, int imp_ok);
120
static int parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass);
121
static ASN1_TYPE *asn1_multi(int utype, const char *section, X509V3_CTX *cnf);
122
static ASN1_TYPE *asn1_str2type(const char *str, int format, int utype);
123
static int asn1_str2tag(const char *tagstr, int len);
124
125
ASN1_TYPE *
126
ASN1_generate_nconf(const char *str, CONF *nconf)
127
0
{
128
0
  X509V3_CTX cnf;
129
130
0
  if (!nconf)
131
0
    return ASN1_generate_v3(str, NULL);
132
133
0
  X509V3_set_nconf(&cnf, nconf);
134
0
  return ASN1_generate_v3(str, &cnf);
135
0
}
136
137
ASN1_TYPE *
138
ASN1_generate_v3(const char *str, X509V3_CTX *cnf)
139
0
{
140
0
  ASN1_TYPE *ret;
141
0
  tag_exp_arg asn1_tags;
142
0
  tag_exp_type *etmp;
143
144
0
  int i, len;
145
146
0
  unsigned char *orig_der = NULL, *new_der = NULL;
147
0
  const unsigned char *cpy_start;
148
0
  unsigned char *p;
149
0
  const unsigned char *cp;
150
0
  int cpy_len;
151
0
  long hdr_len = 0;
152
0
  int hdr_constructed = 0, hdr_tag, hdr_class;
153
0
  int r;
154
155
0
  asn1_tags.imp_tag = -1;
156
0
  asn1_tags.imp_class = -1;
157
0
  asn1_tags.format = ASN1_GEN_FORMAT_ASCII;
158
0
  asn1_tags.exp_count = 0;
159
0
  if (CONF_parse_list(str, ',', 1, asn1_cb, &asn1_tags) != 0)
160
0
    return NULL;
161
162
0
  if ((asn1_tags.utype == V_ASN1_SEQUENCE) ||
163
0
      (asn1_tags.utype == V_ASN1_SET)) {
164
0
    if (!cnf) {
165
0
      ASN1error(ASN1_R_SEQUENCE_OR_SET_NEEDS_CONFIG);
166
0
      return NULL;
167
0
    }
168
0
    ret = asn1_multi(asn1_tags.utype, asn1_tags.str, cnf);
169
0
  } else
170
0
    ret = asn1_str2type(asn1_tags.str, asn1_tags.format,
171
0
        asn1_tags.utype);
172
173
0
  if (!ret)
174
0
    return NULL;
175
176
  /* If no tagging return base type */
177
0
  if ((asn1_tags.imp_tag == -1) && (asn1_tags.exp_count == 0))
178
0
    return ret;
179
180
  /* Generate the encoding */
181
0
  cpy_len = i2d_ASN1_TYPE(ret, &orig_der);
182
0
  ASN1_TYPE_free(ret);
183
0
  ret = NULL;
184
  /* Set point to start copying for modified encoding */
185
0
  cpy_start = orig_der;
186
187
  /* Do we need IMPLICIT tagging? */
188
0
  if (asn1_tags.imp_tag != -1) {
189
    /* If IMPLICIT we will replace the underlying tag */
190
    /* Skip existing tag+len */
191
0
    r = ASN1_get_object(&cpy_start, &hdr_len, &hdr_tag,
192
0
        &hdr_class, cpy_len);
193
0
    if (r & 0x80)
194
0
      goto err;
195
    /* Update copy length */
196
0
    cpy_len -= cpy_start - orig_der;
197
    /* For IMPLICIT tagging the length should match the
198
     * original length and constructed flag should be
199
     * consistent.
200
     */
201
0
    if (r & 0x1) {
202
      /* Indefinite length constructed */
203
0
      hdr_constructed = 2;
204
0
      hdr_len = 0;
205
0
    } else
206
      /* Just retain constructed flag */
207
0
      hdr_constructed = r & V_ASN1_CONSTRUCTED;
208
    /* Work out new length with IMPLICIT tag: ignore constructed
209
     * because it will mess up if indefinite length
210
     */
211
0
    len = ASN1_object_size(0, hdr_len, asn1_tags.imp_tag);
212
0
  } else
213
0
    len = cpy_len;
214
215
  /* Work out length in any EXPLICIT, starting from end */
216
217
0
  for (i = 0, etmp = asn1_tags.exp_list + asn1_tags.exp_count - 1;
218
0
      i < asn1_tags.exp_count; i++, etmp--) {
219
    /* Content length: number of content octets + any padding */
220
0
    len += etmp->exp_pad;
221
0
    etmp->exp_len = len;
222
    /* Total object length: length including new header */
223
0
    len = ASN1_object_size(0, len, etmp->exp_tag);
224
0
  }
225
226
  /* Allocate buffer for new encoding */
227
228
0
  new_der = malloc(len);
229
0
  if (!new_der)
230
0
    goto err;
231
232
  /* Generate tagged encoding */
233
0
  p = new_der;
234
235
  /* Output explicit tags first */
236
0
  for (i = 0, etmp = asn1_tags.exp_list; i < asn1_tags.exp_count;
237
0
      i++, etmp++) {
238
0
    ASN1_put_object(&p, etmp->exp_constructed, etmp->exp_len,
239
0
        etmp->exp_tag, etmp->exp_class);
240
0
    if (etmp->exp_pad)
241
0
      *p++ = 0;
242
0
  }
243
244
  /* If IMPLICIT, output tag */
245
246
0
  if (asn1_tags.imp_tag != -1) {
247
0
    if (asn1_tags.imp_class == V_ASN1_UNIVERSAL &&
248
0
        (asn1_tags.imp_tag == V_ASN1_SEQUENCE ||
249
0
        asn1_tags.imp_tag == V_ASN1_SET))
250
0
      hdr_constructed = V_ASN1_CONSTRUCTED;
251
0
    ASN1_put_object(&p, hdr_constructed, hdr_len,
252
0
        asn1_tags.imp_tag, asn1_tags.imp_class);
253
0
  }
254
255
  /* Copy across original encoding */
256
0
  memcpy(p, cpy_start, cpy_len);
257
258
0
  cp = new_der;
259
260
  /* Obtain new ASN1_TYPE structure */
261
0
  ret = d2i_ASN1_TYPE(NULL, &cp, len);
262
263
0
 err:
264
0
  free(orig_der);
265
0
  free(new_der);
266
267
0
  return ret;
268
0
}
269
270
static int
271
asn1_cb(const char *elem, int len, void *bitstr)
272
0
{
273
0
  tag_exp_arg *arg = bitstr;
274
0
  int i;
275
0
  int utype;
276
0
  int vlen = 0;
277
0
  const char *p, *vstart = NULL;
278
279
0
  int tmp_tag, tmp_class;
280
281
0
  for (i = 0, p = elem; i < len; p++, i++) {
282
    /* Look for the ':' in name value pairs */
283
0
    if (*p == ':') {
284
0
      vstart = p + 1;
285
0
      vlen = len - (vstart - elem);
286
0
      len = p - elem;
287
0
      break;
288
0
    }
289
0
  }
290
291
0
  utype = asn1_str2tag(elem, len);
292
293
0
  if (utype == -1) {
294
0
    ASN1error(ASN1_R_UNKNOWN_TAG);
295
0
    ERR_asprintf_error_data("tag=%s", elem);
296
0
    return -1;
297
0
  }
298
299
  /* If this is not a modifier mark end of string and exit */
300
0
  if (!(utype & ASN1_GEN_FLAG)) {
301
0
    arg->utype = utype;
302
0
    arg->str = vstart;
303
    /* If no value and not end of string, error */
304
0
    if (!vstart && elem[len]) {
305
0
      ASN1error(ASN1_R_MISSING_VALUE);
306
0
      return -1;
307
0
    }
308
0
    return 0;
309
0
  }
310
311
0
  switch (utype) {
312
313
0
  case ASN1_GEN_FLAG_IMP:
314
    /* Check for illegal multiple IMPLICIT tagging */
315
0
    if (arg->imp_tag != -1) {
316
0
      ASN1error(ASN1_R_ILLEGAL_NESTED_TAGGING);
317
0
      return -1;
318
0
    }
319
0
    if (!parse_tagging(vstart, vlen, &arg->imp_tag,
320
0
        &arg->imp_class))
321
0
      return -1;
322
0
    break;
323
324
0
  case ASN1_GEN_FLAG_EXP:
325
0
    if (!parse_tagging(vstart, vlen, &tmp_tag, &tmp_class))
326
0
      return -1;
327
0
    if (!append_exp(arg, tmp_tag, tmp_class, 1, 0, 0))
328
0
      return -1;
329
0
    break;
330
331
0
  case ASN1_GEN_FLAG_SEQWRAP:
332
0
    if (!append_exp(arg, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL, 1, 0, 1))
333
0
      return -1;
334
0
    break;
335
336
0
  case ASN1_GEN_FLAG_SETWRAP:
337
0
    if (!append_exp(arg, V_ASN1_SET, V_ASN1_UNIVERSAL, 1, 0, 1))
338
0
      return -1;
339
0
    break;
340
341
0
  case ASN1_GEN_FLAG_BITWRAP:
342
0
    if (!append_exp(arg, V_ASN1_BIT_STRING, V_ASN1_UNIVERSAL, 0, 1, 1))
343
0
      return -1;
344
0
    break;
345
346
0
  case ASN1_GEN_FLAG_OCTWRAP:
347
0
    if (!append_exp(arg, V_ASN1_OCTET_STRING, V_ASN1_UNIVERSAL, 0, 0, 1))
348
0
      return -1;
349
0
    break;
350
351
0
  case ASN1_GEN_FLAG_FORMAT:
352
0
    if (vstart == NULL) {
353
0
      ASN1error(ASN1_R_ILLEGAL_FORMAT);
354
0
      return -1;
355
0
    }
356
0
    if (!strncmp(vstart, "ASCII", 5))
357
0
      arg->format = ASN1_GEN_FORMAT_ASCII;
358
0
    else if (!strncmp(vstart, "UTF8", 4))
359
0
      arg->format = ASN1_GEN_FORMAT_UTF8;
360
0
    else if (!strncmp(vstart, "HEX", 3))
361
0
      arg->format = ASN1_GEN_FORMAT_HEX;
362
0
    else if (!strncmp(vstart, "BITLIST", 7))
363
0
      arg->format = ASN1_GEN_FORMAT_BITLIST;
364
0
    else {
365
0
      ASN1error(ASN1_R_UNKOWN_FORMAT);
366
0
      return -1;
367
0
    }
368
0
    break;
369
370
0
  }
371
372
0
  return 1;
373
0
}
374
375
static int
376
parse_tagging(const char *vstart, int vlen, int *ptag, int *pclass)
377
0
{
378
0
  long tag_num;
379
0
  char *eptr;
380
381
0
  if (!vstart)
382
0
    return 0;
383
0
  tag_num = strtoul(vstart, &eptr, 10);
384
  /* Check we haven't gone past max length: should be impossible */
385
0
  if (eptr && *eptr && (eptr > vstart + vlen))
386
0
    return 0;
387
0
  if (tag_num < 0) {
388
0
    ASN1error(ASN1_R_INVALID_NUMBER);
389
0
    return 0;
390
0
  }
391
0
  *ptag = tag_num;
392
  /* If we have non numeric characters, parse them */
393
0
  if (eptr)
394
0
    vlen -= eptr - vstart;
395
0
  else
396
0
    vlen = 0;
397
0
  if (vlen) {
398
0
    switch (*eptr) {
399
400
0
    case 'U':
401
0
      *pclass = V_ASN1_UNIVERSAL;
402
0
      break;
403
404
0
    case 'A':
405
0
      *pclass = V_ASN1_APPLICATION;
406
0
      break;
407
408
0
    case 'P':
409
0
      *pclass = V_ASN1_PRIVATE;
410
0
      break;
411
412
0
    case 'C':
413
0
      *pclass = V_ASN1_CONTEXT_SPECIFIC;
414
0
      break;
415
416
0
    default:
417
0
      ASN1error(ASN1_R_INVALID_MODIFIER);
418
0
      ERR_asprintf_error_data("Char=%c", *eptr);
419
0
      return 0;
420
0
      break;
421
422
0
    }
423
0
  } else
424
0
    *pclass = V_ASN1_CONTEXT_SPECIFIC;
425
426
0
  return 1;
427
428
0
}
429
430
/* Handle multiple types: SET and SEQUENCE */
431
432
static ASN1_TYPE *
433
asn1_multi(int utype, const char *section, X509V3_CTX *cnf)
434
0
{
435
0
  ASN1_TYPE *ret = NULL;
436
0
  STACK_OF(ASN1_TYPE) *sk = NULL;
437
0
  STACK_OF(CONF_VALUE) *sect = NULL;
438
0
  unsigned char *der = NULL;
439
0
  int derlen;
440
0
  int i;
441
0
  sk = sk_ASN1_TYPE_new_null();
442
0
  if (!sk)
443
0
    goto bad;
444
0
  if (section) {
445
0
    if (!cnf)
446
0
      goto bad;
447
0
    sect = X509V3_get_section(cnf, (char *)section);
448
0
    if (!sect)
449
0
      goto bad;
450
0
    for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
451
0
      ASN1_TYPE *typ = ASN1_generate_v3(
452
0
          sk_CONF_VALUE_value(sect, i)->value, cnf);
453
0
      if (!typ)
454
0
        goto bad;
455
0
      if (!sk_ASN1_TYPE_push(sk, typ))
456
0
        goto bad;
457
0
    }
458
0
  }
459
460
  /* Now we has a STACK of the components, convert to the correct form */
461
462
0
  if (utype == V_ASN1_SET)
463
0
    derlen = i2d_ASN1_SET_ANY(sk, &der);
464
0
  else
465
0
    derlen = i2d_ASN1_SEQUENCE_ANY(sk, &der);
466
467
0
  if (derlen < 0)
468
0
    goto bad;
469
470
0
  if (!(ret = ASN1_TYPE_new()))
471
0
    goto bad;
472
473
0
  if (!(ret->value.asn1_string = ASN1_STRING_type_new(utype)))
474
0
    goto bad;
475
476
0
  ret->type = utype;
477
478
0
  ret->value.asn1_string->data = der;
479
0
  ret->value.asn1_string->length = derlen;
480
481
0
  der = NULL;
482
483
0
 bad:
484
0
  free(der);
485
0
  if (sk)
486
0
    sk_ASN1_TYPE_pop_free(sk, ASN1_TYPE_free);
487
0
  if (sect)
488
0
    X509V3_section_free(cnf, sect);
489
490
0
  return ret;
491
0
}
492
493
static int
494
append_exp(tag_exp_arg *arg, int exp_tag, int exp_class, int exp_constructed,
495
    int exp_pad, int imp_ok)
496
0
{
497
0
  tag_exp_type *exp_tmp;
498
499
  /* Can only have IMPLICIT if permitted */
500
0
  if ((arg->imp_tag != -1) && !imp_ok) {
501
0
    ASN1error(ASN1_R_ILLEGAL_IMPLICIT_TAG);
502
0
    return 0;
503
0
  }
504
505
0
  if (arg->exp_count == ASN1_FLAG_EXP_MAX) {
506
0
    ASN1error(ASN1_R_DEPTH_EXCEEDED);
507
0
    return 0;
508
0
  }
509
510
0
  exp_tmp = &arg->exp_list[arg->exp_count++];
511
512
  /* If IMPLICIT set tag to implicit value then
513
   * reset implicit tag since it has been used.
514
   */
515
0
  if (arg->imp_tag != -1) {
516
0
    exp_tmp->exp_tag = arg->imp_tag;
517
0
    exp_tmp->exp_class = arg->imp_class;
518
0
    arg->imp_tag = -1;
519
0
    arg->imp_class = -1;
520
0
  } else {
521
0
    exp_tmp->exp_tag = exp_tag;
522
0
    exp_tmp->exp_class = exp_class;
523
0
  }
524
0
  exp_tmp->exp_constructed = exp_constructed;
525
0
  exp_tmp->exp_pad = exp_pad;
526
527
0
  return 1;
528
0
}
529
530
static int
531
asn1_str2tag(const char *tagstr, int len)
532
0
{
533
0
  unsigned int i;
534
0
  static const struct tag_name_st *tntmp, tnst [] = {
535
0
    ASN1_GEN_STR("BOOL", V_ASN1_BOOLEAN),
536
0
    ASN1_GEN_STR("BOOLEAN", V_ASN1_BOOLEAN),
537
0
    ASN1_GEN_STR("NULL", V_ASN1_NULL),
538
0
    ASN1_GEN_STR("INT", V_ASN1_INTEGER),
539
0
    ASN1_GEN_STR("INTEGER", V_ASN1_INTEGER),
540
0
    ASN1_GEN_STR("ENUM", V_ASN1_ENUMERATED),
541
0
    ASN1_GEN_STR("ENUMERATED", V_ASN1_ENUMERATED),
542
0
    ASN1_GEN_STR("OID", V_ASN1_OBJECT),
543
0
    ASN1_GEN_STR("OBJECT", V_ASN1_OBJECT),
544
0
    ASN1_GEN_STR("UTCTIME", V_ASN1_UTCTIME),
545
0
    ASN1_GEN_STR("UTC", V_ASN1_UTCTIME),
546
0
    ASN1_GEN_STR("GENERALIZEDTIME", V_ASN1_GENERALIZEDTIME),
547
0
    ASN1_GEN_STR("GENTIME", V_ASN1_GENERALIZEDTIME),
548
0
    ASN1_GEN_STR("OCT", V_ASN1_OCTET_STRING),
549
0
    ASN1_GEN_STR("OCTETSTRING", V_ASN1_OCTET_STRING),
550
0
    ASN1_GEN_STR("BITSTR", V_ASN1_BIT_STRING),
551
0
    ASN1_GEN_STR("BITSTRING", V_ASN1_BIT_STRING),
552
0
    ASN1_GEN_STR("UNIVERSALSTRING", V_ASN1_UNIVERSALSTRING),
553
0
    ASN1_GEN_STR("UNIV", V_ASN1_UNIVERSALSTRING),
554
0
    ASN1_GEN_STR("IA5", V_ASN1_IA5STRING),
555
0
    ASN1_GEN_STR("IA5STRING", V_ASN1_IA5STRING),
556
0
    ASN1_GEN_STR("UTF8", V_ASN1_UTF8STRING),
557
0
    ASN1_GEN_STR("UTF8String", V_ASN1_UTF8STRING),
558
0
    ASN1_GEN_STR("BMP", V_ASN1_BMPSTRING),
559
0
    ASN1_GEN_STR("BMPSTRING", V_ASN1_BMPSTRING),
560
0
    ASN1_GEN_STR("VISIBLESTRING", V_ASN1_VISIBLESTRING),
561
0
    ASN1_GEN_STR("VISIBLE", V_ASN1_VISIBLESTRING),
562
0
    ASN1_GEN_STR("PRINTABLESTRING", V_ASN1_PRINTABLESTRING),
563
0
    ASN1_GEN_STR("PRINTABLE", V_ASN1_PRINTABLESTRING),
564
0
    ASN1_GEN_STR("T61", V_ASN1_T61STRING),
565
0
    ASN1_GEN_STR("T61STRING", V_ASN1_T61STRING),
566
0
    ASN1_GEN_STR("TELETEXSTRING", V_ASN1_T61STRING),
567
0
    ASN1_GEN_STR("GeneralString", V_ASN1_GENERALSTRING),
568
0
    ASN1_GEN_STR("GENSTR", V_ASN1_GENERALSTRING),
569
0
    ASN1_GEN_STR("NUMERIC", V_ASN1_NUMERICSTRING),
570
0
    ASN1_GEN_STR("NUMERICSTRING", V_ASN1_NUMERICSTRING),
571
572
    /* Special cases */
573
0
    ASN1_GEN_STR("SEQUENCE", V_ASN1_SEQUENCE),
574
0
    ASN1_GEN_STR("SEQ", V_ASN1_SEQUENCE),
575
0
    ASN1_GEN_STR("SET", V_ASN1_SET),
576
    /* type modifiers */
577
    /* Explicit tag */
578
0
    ASN1_GEN_STR("EXP", ASN1_GEN_FLAG_EXP),
579
0
    ASN1_GEN_STR("EXPLICIT", ASN1_GEN_FLAG_EXP),
580
    /* Implicit tag */
581
0
    ASN1_GEN_STR("IMP", ASN1_GEN_FLAG_IMP),
582
0
    ASN1_GEN_STR("IMPLICIT", ASN1_GEN_FLAG_IMP),
583
    /* OCTET STRING wrapper */
584
0
    ASN1_GEN_STR("OCTWRAP", ASN1_GEN_FLAG_OCTWRAP),
585
    /* SEQUENCE wrapper */
586
0
    ASN1_GEN_STR("SEQWRAP", ASN1_GEN_FLAG_SEQWRAP),
587
    /* SET wrapper */
588
0
    ASN1_GEN_STR("SETWRAP", ASN1_GEN_FLAG_SETWRAP),
589
    /* BIT STRING wrapper */
590
0
    ASN1_GEN_STR("BITWRAP", ASN1_GEN_FLAG_BITWRAP),
591
0
    ASN1_GEN_STR("FORM", ASN1_GEN_FLAG_FORMAT),
592
0
    ASN1_GEN_STR("FORMAT", ASN1_GEN_FLAG_FORMAT),
593
0
  };
594
595
0
  if (len == -1)
596
0
    len = strlen(tagstr);
597
598
0
  tntmp = tnst;
599
0
  for (i = 0; i < sizeof(tnst) / sizeof(struct tag_name_st);
600
0
      i++, tntmp++) {
601
0
    if ((len == tntmp->len) && !strncmp(tntmp->strnam, tagstr, len))
602
0
      return tntmp->tag;
603
0
  }
604
605
0
  return -1;
606
0
}
607
608
static ASN1_TYPE *
609
asn1_str2type(const char *str, int format, int utype)
610
0
{
611
0
  ASN1_TYPE *atmp = NULL;
612
0
  CONF_VALUE vtmp;
613
0
  unsigned char *rdata;
614
0
  long rdlen;
615
0
  int no_unused = 1;
616
617
0
  if (!(atmp = ASN1_TYPE_new())) {
618
0
    ASN1error(ERR_R_MALLOC_FAILURE);
619
0
    return NULL;
620
0
  }
621
622
0
  if (!str)
623
0
    str = "";
624
625
0
  switch (utype) {
626
627
0
  case V_ASN1_NULL:
628
0
    if (str && *str) {
629
0
      ASN1error(ASN1_R_ILLEGAL_NULL_VALUE);
630
0
      goto bad_form;
631
0
    }
632
0
    break;
633
634
0
  case V_ASN1_BOOLEAN:
635
0
    if (format != ASN1_GEN_FORMAT_ASCII) {
636
0
      ASN1error(ASN1_R_NOT_ASCII_FORMAT);
637
0
      goto bad_form;
638
0
    }
639
0
    vtmp.name = NULL;
640
0
    vtmp.section = NULL;
641
0
    vtmp.value = (char *)str;
642
0
    if (!X509V3_get_value_bool(&vtmp, &atmp->value.boolean)) {
643
0
      ASN1error(ASN1_R_ILLEGAL_BOOLEAN);
644
0
      goto bad_str;
645
0
    }
646
0
    break;
647
648
0
  case V_ASN1_INTEGER:
649
0
  case V_ASN1_ENUMERATED:
650
0
    if (format != ASN1_GEN_FORMAT_ASCII) {
651
0
      ASN1error(ASN1_R_INTEGER_NOT_ASCII_FORMAT);
652
0
      goto bad_form;
653
0
    }
654
0
    if (!(atmp->value.integer =
655
0
        s2i_ASN1_INTEGER(NULL, (char *)str))) {
656
0
      ASN1error(ASN1_R_ILLEGAL_INTEGER);
657
0
      goto bad_str;
658
0
    }
659
0
    break;
660
661
0
  case V_ASN1_OBJECT:
662
0
    if (format != ASN1_GEN_FORMAT_ASCII) {
663
0
      ASN1error(ASN1_R_OBJECT_NOT_ASCII_FORMAT);
664
0
      goto bad_form;
665
0
    }
666
0
    if (!(atmp->value.object = OBJ_txt2obj(str, 0))) {
667
0
      ASN1error(ASN1_R_ILLEGAL_OBJECT);
668
0
      goto bad_str;
669
0
    }
670
0
    break;
671
672
0
  case V_ASN1_UTCTIME:
673
0
  case V_ASN1_GENERALIZEDTIME:
674
0
    if (format != ASN1_GEN_FORMAT_ASCII) {
675
0
      ASN1error(ASN1_R_TIME_NOT_ASCII_FORMAT);
676
0
      goto bad_form;
677
0
    }
678
0
    if (!(atmp->value.asn1_string = ASN1_STRING_new())) {
679
0
      ASN1error(ERR_R_MALLOC_FAILURE);
680
0
      goto bad_str;
681
0
    }
682
0
    if (!ASN1_STRING_set(atmp->value.asn1_string, str, -1)) {
683
0
      ASN1error(ERR_R_MALLOC_FAILURE);
684
0
      goto bad_str;
685
0
    }
686
0
    atmp->value.asn1_string->type = utype;
687
0
    if (!ASN1_TIME_check(atmp->value.asn1_string)) {
688
0
      ASN1error(ASN1_R_ILLEGAL_TIME_VALUE);
689
0
      goto bad_str;
690
0
    }
691
0
    break;
692
693
0
  case V_ASN1_BMPSTRING:
694
0
  case V_ASN1_PRINTABLESTRING:
695
0
  case V_ASN1_IA5STRING:
696
0
  case V_ASN1_T61STRING:
697
0
  case V_ASN1_UTF8STRING:
698
0
  case V_ASN1_VISIBLESTRING:
699
0
  case V_ASN1_UNIVERSALSTRING:
700
0
  case V_ASN1_GENERALSTRING:
701
0
  case V_ASN1_NUMERICSTRING:
702
703
0
    if (format == ASN1_GEN_FORMAT_ASCII)
704
0
      format = MBSTRING_ASC;
705
0
    else if (format == ASN1_GEN_FORMAT_UTF8)
706
0
      format = MBSTRING_UTF8;
707
0
    else {
708
0
      ASN1error(ASN1_R_ILLEGAL_FORMAT);
709
0
      goto bad_form;
710
0
    }
711
712
0
    if (ASN1_mbstring_copy(&atmp->value.asn1_string,
713
0
        (unsigned char *)str, -1, format,
714
0
        ASN1_tag2bit(utype)) <= 0) {
715
0
      ASN1error(ERR_R_MALLOC_FAILURE);
716
0
      goto bad_str;
717
0
    }
718
0
    break;
719
720
0
  case V_ASN1_BIT_STRING:
721
0
  case V_ASN1_OCTET_STRING:
722
0
    if (!(atmp->value.asn1_string = ASN1_STRING_new())) {
723
0
      ASN1error(ERR_R_MALLOC_FAILURE);
724
0
      goto bad_form;
725
0
    }
726
727
0
    if (format == ASN1_GEN_FORMAT_HEX) {
728
729
0
      if (!(rdata = string_to_hex((char *)str, &rdlen))) {
730
0
        ASN1error(ASN1_R_ILLEGAL_HEX);
731
0
        goto bad_str;
732
0
      }
733
734
0
      atmp->value.asn1_string->data = rdata;
735
0
      atmp->value.asn1_string->length = rdlen;
736
0
      atmp->value.asn1_string->type = utype;
737
738
0
    } else if (format == ASN1_GEN_FORMAT_ASCII) {
739
0
      if (ASN1_STRING_set(atmp->value.asn1_string, str,
740
0
          -1) == 0) {
741
0
        ASN1error(ERR_R_MALLOC_FAILURE);
742
0
        goto bad_str;
743
0
      }
744
0
    } else if ((format == ASN1_GEN_FORMAT_BITLIST) &&
745
0
        (utype == V_ASN1_BIT_STRING)) {
746
0
      if (!CONF_parse_list(str, ',', 1, bitstr_cb,
747
0
          atmp->value.bit_string)) {
748
0
        ASN1error(ASN1_R_LIST_ERROR);
749
0
        goto bad_str;
750
0
      }
751
0
      no_unused = 0;
752
753
0
    } else {
754
0
      ASN1error(ASN1_R_ILLEGAL_BITSTRING_FORMAT);
755
0
      goto bad_form;
756
0
    }
757
758
0
    if ((utype == V_ASN1_BIT_STRING) && no_unused) {
759
0
      if (!asn1_abs_set_unused_bits(atmp->value.asn1_string,
760
0
          0))
761
0
        goto bad_str;
762
0
    }
763
764
0
    break;
765
766
0
  default:
767
0
    ASN1error(ASN1_R_UNSUPPORTED_TYPE);
768
0
    goto bad_str;
769
0
    break;
770
0
  }
771
772
0
  atmp->type = utype;
773
0
  return atmp;
774
775
0
 bad_str:
776
0
  ERR_asprintf_error_data("string=%s", str);
777
0
 bad_form:
778
0
  ASN1_TYPE_free(atmp);
779
0
  return NULL;
780
0
}
781
782
static int
783
bitstr_cb(const char *elem, int len, void *bitstr)
784
0
{
785
0
  long bitnum;
786
0
  char *eptr;
787
788
0
  if (!elem)
789
0
    return 0;
790
0
  bitnum = strtoul(elem, &eptr, 10);
791
0
  if (eptr && *eptr && (eptr != elem + len))
792
0
    return 0;
793
0
  if (bitnum < 0) {
794
0
    ASN1error(ASN1_R_INVALID_NUMBER);
795
0
    return 0;
796
0
  }
797
0
  if (!ASN1_BIT_STRING_set_bit(bitstr, bitnum, 1)) {
798
0
    ASN1error(ERR_R_MALLOC_FAILURE);
799
0
    return 0;
800
0
  }
801
0
  return 1;
802
0
}