summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2021-12-14 17:35:21 +0000
committerjsing <>2021-12-14 17:35:21 +0000
commit9ba2385eabb29de87df68ff88f34bbd29ed4952e (patch)
treec1a26d3aa6222d08c3946dc662d663cc793fc23d /src/lib
parentbb673fb853e10ee12848841c7d2608a5018a7e5c (diff)
downloadopenbsd-9ba2385eabb29de87df68ff88f34bbd29ed4952e.tar.gz
openbsd-9ba2385eabb29de87df68ff88f34bbd29ed4952e.tar.bz2
openbsd-9ba2385eabb29de87df68ff88f34bbd29ed4952e.zip
Consolidate ASN.1 universal tag type data.
There are currently three different tables in three different files that contain information about ASN.1 universal class tag types. Range checking is also implemented in three different places (with different implementations). Consolidate all of this into a single table, provide a lookup function that deals with the range checks and wrappers to deal with specific types. ok inoguchi@ tb@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/Makefile4
-rw-r--r--src/lib/libcrypto/asn1/a_strex.c47
-rw-r--r--src/lib/libcrypto/asn1/asn1_locl.h4
-rw-r--r--src/lib/libcrypto/asn1/asn1_par.c24
-rw-r--r--src/lib/libcrypto/asn1/asn1_types.c263
-rw-r--r--src/lib/libcrypto/asn1/tasn_dec.c23
6 files changed, 283 insertions, 82 deletions
diff --git a/src/lib/libcrypto/Makefile b/src/lib/libcrypto/Makefile
index df4649acaa..c7d84b437b 100644
--- a/src/lib/libcrypto/Makefile
+++ b/src/lib/libcrypto/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.55 2021/12/13 17:56:15 jsing Exp $ 1# $OpenBSD: Makefile,v 1.56 2021/12/14 17:35:21 jsing Exp $
2 2
3LIB= crypto 3LIB= crypto
4LIBREBUILD=y 4LIBREBUILD=y
@@ -65,7 +65,7 @@ SRCS+= f_int.c f_string.c n_pkey.c
65SRCS+= f_enum.c x_pkey.c x_exten.c bio_asn1.c bio_ndef.c asn_mime.c 65SRCS+= f_enum.c x_pkey.c x_exten.c bio_asn1.c bio_ndef.c asn_mime.c
66SRCS+= asn1_gen.c asn1_par.c asn1_lib.c asn1_err.c a_strnid.c 66SRCS+= asn1_gen.c asn1_par.c asn1_lib.c asn1_err.c a_strnid.c
67SRCS+= evp_asn1.c asn_pack.c p5_pbe.c p5_pbev2.c p8_pkey.c asn_moid.c 67SRCS+= evp_asn1.c asn_pack.c p5_pbe.c p5_pbev2.c p8_pkey.c asn_moid.c
68SRCS+= a_time_tm.c 68SRCS+= a_time_tm.c asn1_types.c
69 69
70# bf/ 70# bf/
71SRCS+= bf_skey.c bf_ecb.c bf_cfb64.c bf_ofb64.c 71SRCS+= bf_skey.c bf_ecb.c bf_cfb64.c bf_ofb64.c
diff --git a/src/lib/libcrypto/asn1/a_strex.c b/src/lib/libcrypto/asn1/a_strex.c
index 3b66ea8d36..61672d29a4 100644
--- a/src/lib/libcrypto/asn1/a_strex.c
+++ b/src/lib/libcrypto/asn1/a_strex.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_strex.c,v 1.29 2021/11/01 20:53:08 tb Exp $ */ 1/* $OpenBSD: a_strex.c,v 1.30 2021/12/14 17:35:21 jsing Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
@@ -322,22 +322,6 @@ do_dump(unsigned long lflags, char_io *io_ch, void *arg, const ASN1_STRING *str)
322 return outlen + 1; 322 return outlen + 1;
323} 323}
324 324
325/* Lookup table to convert tags to character widths,
326 * 0 = UTF8 encoded, -1 is used for non string types
327 * otherwise it is the number of bytes per character
328 */
329
330static const signed char tag2nbyte[] = {
331 -1, -1, -1, -1, -1, /* 0-4 */
332 -1, -1, -1, -1, -1, /* 5-9 */
333 -1, -1, 0, -1, /* 10-13 */
334 -1, -1, -1, -1, /* 15-17 */
335 -1, 1, 1, /* 18-20 */
336 -1, 1, 1, 1, /* 21-24 */
337 -1, 1, -1, /* 25-27 */
338 4, -1, 2 /* 28-30 */
339};
340
341/* This is the main function, print out an 325/* This is the main function, print out an
342 * ASN1_STRING taking note of various escape 326 * ASN1_STRING taking note of various escape
343 * and display options. Returns number of 327 * and display options. Returns number of
@@ -371,19 +355,16 @@ do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
371 355
372 /* Decide what to do with type, either dump content or display it */ 356 /* Decide what to do with type, either dump content or display it */
373 357
374 /* Dump everything */ 358 if (lflags & ASN1_STRFLGS_DUMP_ALL) {
375 if (lflags & ASN1_STRFLGS_DUMP_ALL) 359 /* Dump everything. */
376 type = -1; 360 type = -1;
377 /* Ignore the string type */ 361 } else if (lflags & ASN1_STRFLGS_IGNORE_TYPE) {
378 else if (lflags & ASN1_STRFLGS_IGNORE_TYPE) 362 /* Ignore the string type. */
379 type = 1; 363 type = 1;
380 else { 364 } else {
381 /* Else determine width based on type */ 365 /* Else determine width based on type. */
382 if ((type > 0) && (type < 31)) 366 type = asn1_tag2charwidth(type);
383 type = tag2nbyte[type]; 367 if (type == -1 && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN))
384 else
385 type = -1;
386 if ((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN))
387 type = 1; 368 type = 1;
388 } 369 }
389 370
@@ -627,17 +608,15 @@ int
627ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in) 608ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in)
628{ 609{
629 ASN1_STRING stmp, *str = &stmp; 610 ASN1_STRING stmp, *str = &stmp;
630 int mbflag, type, ret; 611 int mbflag, ret;
631 612
632 if (!in) 613 if (!in)
633 return -1; 614 return -1;
634 type = in->type; 615
635 if ((type < 0) || (type > 30)) 616 if ((mbflag = asn1_tag2charwidth(in->type)) == -1)
636 return -1;
637 mbflag = tag2nbyte[type];
638 if (mbflag == -1)
639 return -1; 617 return -1;
640 mbflag |= MBSTRING_FLAG; 618 mbflag |= MBSTRING_FLAG;
619
641 stmp.data = NULL; 620 stmp.data = NULL;
642 stmp.length = 0; 621 stmp.length = 0;
643 ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag, 622 ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
diff --git a/src/lib/libcrypto/asn1/asn1_locl.h b/src/lib/libcrypto/asn1/asn1_locl.h
index 39779d9377..f7731ec5dd 100644
--- a/src/lib/libcrypto/asn1/asn1_locl.h
+++ b/src/lib/libcrypto/asn1/asn1_locl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1_locl.h,v 1.12 2019/10/24 16:36:10 jsing Exp $ */ 1/* $OpenBSD: asn1_locl.h,v 1.13 2021/12/14 17:35:21 jsing Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2006. 3 * project 2006.
4 */ 4 */
@@ -155,4 +155,6 @@ struct x509_crl_method_st {
155int UTF8_getc(const unsigned char *str, int len, unsigned long *val); 155int UTF8_getc(const unsigned char *str, int len, unsigned long *val);
156int UTF8_putc(unsigned char *str, int len, unsigned long value); 156int UTF8_putc(unsigned char *str, int len, unsigned long value);
157 157
158int asn1_tag2charwidth(int tag);
159
158__END_HIDDEN_DECLS 160__END_HIDDEN_DECLS
diff --git a/src/lib/libcrypto/asn1/asn1_par.c b/src/lib/libcrypto/asn1/asn1_par.c
index af948c5267..4604930593 100644
--- a/src/lib/libcrypto/asn1/asn1_par.c
+++ b/src/lib/libcrypto/asn1/asn1_par.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1_par.c,v 1.29 2021/12/13 17:58:41 tb Exp $ */ 1/* $OpenBSD: asn1_par.c,v 1.30 2021/12/14 17:35:21 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -380,25 +380,3 @@ end:
380 *pp = p; 380 *pp = p;
381 return (ret); 381 return (ret);
382} 382}
383
384const char *
385ASN1_tag2str(int tag)
386{
387 static const char * const tag2str[] = {
388 "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING", /* 0-4 */
389 "NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL", /* 5-9 */
390 "ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>", /* 10-13 */
391 "<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET", /* 15-17 */
392 "NUMERICSTRING", "PRINTABLESTRING", "T61STRING", /* 18-20 */
393 "VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME", /* 21-24 */
394 "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", /* 25-27 */
395 "UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING" /* 28-30 */
396 };
397
398 if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
399 tag &= ~0x100;
400
401 if (tag < 0 || tag > 30)
402 return "(unknown)";
403 return tag2str[tag];
404}
diff --git a/src/lib/libcrypto/asn1/asn1_types.c b/src/lib/libcrypto/asn1/asn1_types.c
new file mode 100644
index 0000000000..c8e6b23f4f
--- /dev/null
+++ b/src/lib/libcrypto/asn1/asn1_types.c
@@ -0,0 +1,263 @@
1/* $OpenBSD: asn1_types.c,v 1.1 2021/12/14 17:35:21 jsing Exp $ */
2/*
3 * Copyright (c) 2021 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <stddef.h>
19
20#include <openssl/asn1.h>
21
22struct asn1_type {
23 const char *name;
24 uint32_t bit_value;
25 int char_width;
26};
27
28/*
29 * Universal class tag types - ITU X.680.
30 */
31static const struct asn1_type asn1_types[31] = {
32 [0] = {
33 /* Tag 0 (0x00) - Reserved for use by encoding rules */
34 .name = "EOC",
35 .bit_value = 0,
36 .char_width = -1,
37 },
38 [1] = {
39 /* Tag 1 (0x01) - Boolean */
40 .name = "BOOLEAN",
41 .bit_value = 0,
42 .char_width = -1,
43 },
44 [2] = {
45 /* Tag 2 (0x02) - Integer */
46 .name = "INTEGER",
47 .bit_value = 0,
48 .char_width = -1,
49 },
50 [3] = {
51 /* Tag 3 (0x03) - BitString */
52 .name = "BIT STRING",
53 .bit_value = B_ASN1_BIT_STRING,
54 .char_width = -1,
55 },
56 [4] = {
57 /* Tag 4 (0x04) - OctetString */
58 .name = "OCTET STRING",
59 .bit_value = B_ASN1_OCTET_STRING,
60 .char_width = -1,
61 },
62 [5] = {
63 /* Tag 5 (0x05) - Null */
64 .name = "NULL",
65 .bit_value = 0,
66 .char_width = -1,
67 },
68 [6] = {
69 /* Tag 6 (0x06) - Object Identifier */
70 .name = "OBJECT",
71 .bit_value = 0,
72 .char_width = -1,
73 },
74 [7] = {
75 /* Tag 7 (0x07) - Object Descriptor */
76 .name = "OBJECT DESCRIPTOR",
77 .bit_value = B_ASN1_UNKNOWN,
78 .char_width = -1,
79 },
80 [8] = {
81 /* Tag 8 (0x08) - External */
82 .name = "EXTERNAL",
83 .bit_value = B_ASN1_UNKNOWN,
84 .char_width = -1,
85 },
86 [9] = {
87 /* Tag 9 (0x09) - Real */
88 .name = "REAL",
89 .bit_value = B_ASN1_UNKNOWN,
90 .char_width = -1,
91 },
92 [10] = {
93 /* Tag 10 (0x0a) - Enumerated */
94 .name = "ENUMERATED",
95 .bit_value = B_ASN1_UNKNOWN,
96 .char_width = -1,
97 },
98 [11] = {
99 /* Tag 11 (0x0b) - Embedded PDV */
100 .name = "<ASN1 11 EMBEDDED PDV>",
101 .bit_value = B_ASN1_UNKNOWN,
102 .char_width = -1,
103 },
104 [12] = {
105 /* Tag 12 (0x0c) - UTF8String */
106 .name = "UTF8STRING",
107 .bit_value = B_ASN1_UTF8STRING,
108 .char_width = 0,
109 },
110 [13] = {
111 /* Tag 13 (0x0d) - Relative Object Identifier */
112 .name = "<ASN1 13 RELATIVE OID>",
113 .bit_value = B_ASN1_UNKNOWN,
114 .char_width = -1,
115 },
116 [14] = {
117 /* Tag 14 (0x0e) - Time */
118 .name = "<ASN1 14 TIME>",
119 .bit_value = B_ASN1_UNKNOWN,
120 .char_width = -1,
121 },
122 [15] = {
123 /* Tag 15 (0x0f) - Reserved */
124 .name = "<ASN1 15 RESERVED>",
125 .bit_value = B_ASN1_UNKNOWN,
126 .char_width = -1,
127 },
128 [16] = {
129 /* Tag 16 (0x10)- Sequence */
130 .name = "SEQUENCE",
131 .bit_value = B_ASN1_SEQUENCE,
132 .char_width = -1,
133 },
134 [17] = {
135 /* Tag 17 (0x11) - Set */
136 .name = "SET",
137 .bit_value = 0,
138 .char_width = -1,
139 },
140 [18] = {
141 /* Tag 18 (0x12) - NumericString */
142 .name = "NUMERICSTRING",
143 .bit_value = B_ASN1_NUMERICSTRING,
144 .char_width = -1,
145 },
146 [19] = {
147 /* Tag 19 (0x13) - PrintableString */
148 .name = "PRINTABLESTRING",
149 .bit_value = B_ASN1_PRINTABLESTRING,
150 .char_width = 1,
151 },
152 [20] = {
153 /* Tag 20 (0x14) - TeletexString (T61String) */
154 .name = "T61STRING",
155 .bit_value = B_ASN1_T61STRING,
156 .char_width = 1,
157 },
158 [21] = {
159 /* Tag 21 (0x15) - VideotexString */
160 .name = "VIDEOTEXSTRING",
161 .bit_value = B_ASN1_VIDEOTEXSTRING,
162 .char_width = -1,
163 },
164 [22] = {
165 /* Tag 22 (0x16) - IA5String */
166 .name = "IA5STRING",
167 .bit_value = B_ASN1_IA5STRING,
168 .char_width = 1,
169 },
170 [23] = {
171 /* Tag 23 (0x17) - UTCTime */
172 .name = "UTCTIME",
173 .bit_value = B_ASN1_UTCTIME,
174 .char_width = 1,
175 },
176 [24] = {
177 /* Tag 24 (0x18) - GeneralizedTime */
178 .name = "GENERALIZEDTIME",
179 .bit_value = B_ASN1_GENERALIZEDTIME,
180 .char_width = 1,
181 },
182 [25] = {
183 /* Tag 25 (0x19) - GraphicString */
184 .name = "GRAPHICSTRING",
185 .bit_value = B_ASN1_GRAPHICSTRING,
186 .char_width = -1,
187 },
188 [26] = {
189 /* Tag 26 (0x1a) - VisibleString (ISO646String) */
190 .name = "VISIBLESTRING",
191 .bit_value = B_ASN1_ISO64STRING,
192 .char_width = 1,
193 },
194 [27] = {
195 /* Tag 27 (0x1b) - GeneralString */
196 .name = "GENERALSTRING",
197 .bit_value = B_ASN1_GENERALSTRING,
198 .char_width = -1,
199 },
200 [28] = {
201 /* Tag 28 (0x1c) - UniversalString */
202 .name = "UNIVERSALSTRING",
203 .bit_value = B_ASN1_UNIVERSALSTRING,
204 .char_width = 4,
205 },
206 [29] = {
207 /* Tag 29 (0x1d) - Unallocated */
208 .name = "<ASN1 29>",
209 .bit_value = B_ASN1_UNKNOWN,
210 .char_width = -1,
211 },
212 [30] = {
213 /* Tag 30 (0x1e) - BMPString */
214 .name = "BMPSTRING",
215 .bit_value = B_ASN1_BMPSTRING,
216 .char_width = 2,
217 },
218};
219
220static const struct asn1_type *
221asn1_type_by_tag(int tag)
222{
223 if (tag < 0 || tag > 30)
224 return NULL;
225
226 return &asn1_types[tag];
227}
228
229int
230asn1_tag2charwidth(int tag)
231{
232 const struct asn1_type *at;
233
234 if ((at = asn1_type_by_tag(tag)) != NULL)
235 return at->char_width;
236
237 return -1;
238}
239
240unsigned long
241ASN1_tag2bit(int tag)
242{
243 const struct asn1_type *at;
244
245 if ((at = asn1_type_by_tag(tag)) != NULL)
246 return (unsigned long)at->bit_value;
247
248 return 0;
249}
250
251const char *
252ASN1_tag2str(int tag)
253{
254 const struct asn1_type *at;
255
256 if (tag == V_ASN1_NEG_INTEGER || tag == V_ASN1_NEG_ENUMERATED)
257 tag &= ~V_ASN1_NEG;
258
259 if ((at = asn1_type_by_tag(tag)) != NULL)
260 return at->name;
261
262 return "(unknown)";
263}
diff --git a/src/lib/libcrypto/asn1/tasn_dec.c b/src/lib/libcrypto/asn1/tasn_dec.c
index a04a84cce8..e207691e2e 100644
--- a/src/lib/libcrypto/asn1/tasn_dec.c
+++ b/src/lib/libcrypto/asn1/tasn_dec.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tasn_dec.c,v 1.45 2021/12/13 17:50:24 jsing Exp $ */ 1/* $OpenBSD: tasn_dec.c,v 1.46 2021/12/14 17:35:21 jsing Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000. 3 * project 2000.
4 */ 4 */
@@ -95,27 +95,6 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in,
95 long len, const ASN1_ITEM *it, int tag, int aclass, char opt, 95 long len, const ASN1_ITEM *it, int tag, int aclass, char opt,
96 ASN1_TLC *ctx); 96 ASN1_TLC *ctx);
97 97
98/* Table to convert tags to bit values, used for MSTRING type */
99static const unsigned long tag2bit[32] = {
100 0, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
101 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
102 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
103 B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
104 B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
105 B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */
106 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
107 B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */
108 B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
109};
110
111unsigned long
112ASN1_tag2bit(int tag)
113{
114 if ((tag < 0) || (tag > 30))
115 return 0;
116 return tag2bit[tag];
117}
118
119static void 98static void
120asn1_tlc_invalidate(ASN1_TLC *ctx) 99asn1_tlc_invalidate(ASN1_TLC *ctx)
121{ 100{