Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/a_strex.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/asn1.h>
5
6 #include <assert.h>
7 #include <ctype.h>
8 #include <inttypes.h>
9 #include <limits.h>
10 #include <string.h>
11 #include <time.h>
12
13 #include <openssl/bio.h>
14 #include <openssl/bytestring.h>
15 #include <openssl/mem.h>
16
17 #include "../bytestring/internal.h"
18 #include "../internal.h"
19 #include "internal.h"
20
21
22 #define ESC_FLAGS \
23 (ASN1_STRFLGS_ESC_2253 | ASN1_STRFLGS_ESC_QUOTE | ASN1_STRFLGS_ESC_CTRL | \
24 ASN1_STRFLGS_ESC_MSB)
25
26 static int maybe_write(BIO *out, const void *buf, int len) {
27 // If |out| is NULL, ignore the output but report the length.
28 return out == NULL || BIO_write(out, buf, len) == len;
29 }
30
31 static int is_control_character(unsigned char c) { return c < 32 || c == 127; }
32
33 static int do_esc_char(uint32_t c, unsigned long flags, char *do_quotes,
34 BIO *out, int is_first, int is_last) {
35 // |c| is a |uint32_t| because, depending on |ASN1_STRFLGS_UTF8_CONVERT|,
36 // we may be escaping bytes or Unicode codepoints.
37 char buf[16]; // Large enough for "\\W01234567".
38 unsigned char u8 = (unsigned char)c;
39 if (c > 0xffff) {
40 snprintf(buf, sizeof(buf), "\\W%08" PRIX32, c);
41 } else if (c > 0xff) {
42 snprintf(buf, sizeof(buf), "\\U%04" PRIX32, c);
43 } else if ((flags & ASN1_STRFLGS_ESC_MSB) && c > 0x7f) {
44 snprintf(buf, sizeof(buf), "\\%02X", c);
45 } else if ((flags & ASN1_STRFLGS_ESC_CTRL) && is_control_character(c)) {
46 snprintf(buf, sizeof(buf), "\\%02X", c);
47 } else if (flags & ASN1_STRFLGS_ESC_2253) {
48 // See RFC 2253, sections 2.4 and 4.
49 if (c == '\\' || c == '"') {
50 // Quotes and backslashes are always escaped, quoted or not.
51 snprintf(buf, sizeof(buf), "\\%c", (int)c);
52 } else if (c == ',' || c == '+' || c == '<' || c == '>' || c == ';' ||
53 (is_first && (c == ' ' || c == '#')) ||
54 (is_last && (c == ' '))) {
55 if (flags & ASN1_STRFLGS_ESC_QUOTE) {
56 // No need to escape, just tell the caller to quote.
57 if (do_quotes != NULL) {
58 *do_quotes = 1;
59 }
60 return maybe_write(out, &u8, 1) ? 1 : -1;
61 }
62 snprintf(buf, sizeof(buf), "\\%c", (int)c);
63 } else {
64 return maybe_write(out, &u8, 1) ? 1 : -1;
65 }
66 } else if ((flags & ESC_FLAGS) && c == '\\') {
67 // If any escape flags are set, also escape backslashes.
68 snprintf(buf, sizeof(buf), "\\%c", (int)c);
69 } else {
70 return maybe_write(out, &u8, 1) ? 1 : -1;
71 }
72
73 OPENSSL_STATIC_ASSERT(sizeof(buf) < INT_MAX, len_may_not_fit_in_int);
74 int len = (int)strlen(buf);
75 return maybe_write(out, buf, len) ? len : -1;
76 }
77
78 // This function sends each character in a buffer to do_esc_char(). It
79 // interprets the content formats and converts to or from UTF8 as
80 // appropriate.
81
82 static int do_buf(const unsigned char *buf, int buflen, int encoding,
83 int utf8_convert, unsigned long flags, char *quotes,
84 BIO *out) {
85 // Reject invalid UCS-4 and UCS-2 lengths without parsing.
86 switch (encoding) {
87 case MBSTRING_UNIV:
88 if (buflen & 3) {
89 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_UNIVERSALSTRING);
90 return -1;
91 }
92 break;
93 case MBSTRING_BMP:
94 if (buflen & 1) {
95 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_BMPSTRING);
96 return -1;
97 }
98 break;
99 }
100
101 const unsigned char *p = buf;
102 const unsigned char *q = buf + buflen;
103 int outlen = 0;
104 while (p != q) {
105 const int is_first = p == buf;
106 // TODO(davidben): Replace this with |cbs_get_ucs2_be|, etc., to check
107 // for invalid codepoints. Before doing that, enforce it in the parser,
108 // https://crbug.com/boringssl/427, so these error cases are not
109 // reachable from parsed objects.
110 uint32_t c = 0;
111 switch (encoding) {
112 case MBSTRING_UNIV:
113 c = ((uint32_t)*p++) << 24;
114 c |= ((uint32_t)*p++) << 16;
115 c |= ((uint32_t)*p++) << 8;
116 c |= *p++;
117 break;
118
119 case MBSTRING_BMP:
120 c = ((uint32_t)*p++) << 8;
121 c |= *p++;
122 break;
123
124 case MBSTRING_ASC:
125 c = *p++;
126 break;
127
128 case MBSTRING_UTF8: {
129 int consumed = UTF8_getc(p, buflen, &c);
130 if (consumed < 0) {
131 return -1; // Invalid UTF8String
132 }
133 buflen -= consumed;
134 p += consumed;
135 break;
136 }
137
138 default:
139 assert(0);
140 return -1;
141 }
142 const int is_last = p == q;
143 if (utf8_convert) {
144 unsigned char utfbuf[6];
145 int utflen;
146 utflen = UTF8_putc(utfbuf, sizeof utfbuf, c);
147 for (int i = 0; i < utflen; i++) {
148 int len = do_esc_char(utfbuf[i], flags, quotes, out, is_first && i == 0,
149 is_last && i == utflen - 1);
150 if (len < 0) {
151 return -1;
152 }
153
154 // Ensure addition doesn't overflow and corrupt the signed output length
155 if (len > INT_MAX - outlen) {
156 OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
157 return -1;
158 }
159 outlen += len;
160 }
161 } else {
162 int len = do_esc_char(c, flags, quotes, out, is_first, is_last);
163 if (len < 0) {
164 return -1;
165 }
166 if (len > INT_MAX - outlen) {
167 OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
168 return -1;
169 }
170 outlen += len;
171 }
172 }
173 return outlen;
174 }
175
176 // This function hex dumps a buffer of characters
177
178 static int do_hex_dump(BIO *out, unsigned char *buf, int buflen) {
179 static const char hexdig[] = "0123456789ABCDEF";
180 unsigned char *p, *q;
181 char hextmp[2];
182 if (out) {
183 p = buf;
184 q = buf + buflen;
185 while (p != q) {
186 hextmp[0] = hexdig[*p >> 4];
187 hextmp[1] = hexdig[*p & 0xf];
188 if (!maybe_write(out, hextmp, 2)) {
189 return -1;
190 }
191 p++;
192 }
193 }
194 return buflen << 1;
195 }
196
197 // "dump" a string. This is done when the type is unknown, or the flags
198 // request it. We can either dump the content octets or the entire DER
199 // encoding. This uses the RFC 2253 #01234 format.
200
201 static int do_dump(unsigned long flags, BIO *out, const ASN1_STRING *str) {
202 if (!maybe_write(out, "#", 1)) {
203 return -1;
204 }
205
206 // If we don't dump DER encoding just dump content octets
207 if (!(flags & ASN1_STRFLGS_DUMP_DER)) {
208 int outlen = do_hex_dump(out, str->data, str->length);
209 if (outlen < 0) {
210 return -1;
211 }
212 return outlen + 1;
213 }
214
215 // Placing the ASN1_STRING in a temporary ASN1_TYPE allows the DER encoding
216 // to readily obtained.
217 ASN1_TYPE t;
218 OPENSSL_memset(&t, 0, sizeof(ASN1_TYPE));
219 asn1_type_set0_string(&t, (ASN1_STRING *)str);
220 unsigned char *der_buf = NULL;
221 int der_len = i2d_ASN1_TYPE(&t, &der_buf);
222 if (der_len < 0) {
223 return -1;
224 }
225 int outlen = do_hex_dump(out, der_buf, der_len);
226 OPENSSL_free(der_buf);
227 if (outlen < 0) {
228 return -1;
229 }
230 return outlen + 1;
231 }
232
233 // string_type_to_encoding returns the |MBSTRING_*| constant for the encoding
234 // used by the |ASN1_STRING| type |type|, or -1 if |tag| is not a string
235 // type.
236 static int string_type_to_encoding(int type) {
237 // This function is sometimes passed ASN.1 universal types and sometimes
238 // passed |ASN1_STRING| type values
239 switch (type) {
240 case V_ASN1_UTF8STRING:
241 return MBSTRING_UTF8;
242 case V_ASN1_NUMERICSTRING:
243 case V_ASN1_PRINTABLESTRING:
244 case V_ASN1_T61STRING:
245 case V_ASN1_IA5STRING:
246 case V_ASN1_UTCTIME:
247 case V_ASN1_GENERALIZEDTIME:
248 case V_ASN1_ISO64STRING:
249 // |MBSTRING_ASC| refers to Latin-1, not ASCII.
250 return MBSTRING_ASC;
251 case V_ASN1_UNIVERSALSTRING:
252 return MBSTRING_UNIV;
253 case V_ASN1_BMPSTRING:
254 return MBSTRING_BMP;
255 }
256 return -1;
257 }
258
259 // This is the main function, print out an ASN1_STRING taking note of various
260 // escape and display options. Returns number of characters written or -1 if
261 // an error occurred.
262
263 int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str,
264 unsigned long flags) {
265 int type = str->type;
266 int outlen = 0;
267 if (flags & ASN1_STRFLGS_SHOW_TYPE) {
268 const char *tagname = ASN1_tag2str(type);
269 outlen += strlen(tagname);
270 if (!maybe_write(out, tagname, outlen) || !maybe_write(out, ":", 1)) {
271 return -1;
272 }
273 outlen++;
274 }
275
276 // Decide what to do with |str|, either dump the contents or display it.
277 int encoding;
278 if (flags & ASN1_STRFLGS_DUMP_ALL) {
279 // Dump everything.
280 encoding = -1;
281 } else if (flags & ASN1_STRFLGS_IGNORE_TYPE) {
282 // Ignore the string type and interpret the contents as Latin-1.
283 encoding = MBSTRING_ASC;
284 } else {
285 encoding = string_type_to_encoding(type);
286 if (encoding == -1 && (flags & ASN1_STRFLGS_DUMP_UNKNOWN) == 0) {
287 encoding = MBSTRING_ASC;
288 }
289 }
290
291 if (encoding == -1) {
292 int len = do_dump(flags, out, str);
293 if (len < 0) {
294 return -1;
295 }
296 outlen += len;
297 return outlen;
298 }
299
300 int utf8_convert = 0;
301 if (flags & ASN1_STRFLGS_UTF8_CONVERT) {
302 // If the string is UTF-8, skip decoding and just interpret it as 1 byte
303 // per character to avoid converting twice.
304 //
305 // TODO(davidben): This is not quite a valid optimization if the input
306 // was invalid UTF-8.
307 if (encoding == MBSTRING_UTF8) {
308 encoding = MBSTRING_ASC;
309 } else {
310 utf8_convert = 1;
311 }
312 }
313
314 // Measure the length.
315 char quotes = 0;
316 int len = do_buf(str->data, str->length, encoding, utf8_convert, flags,
317 &quotes, NULL);
318 if (len < 0) {
319 return -1;
320 }
321 outlen += len;
322 if (quotes) {
323 outlen += 2;
324 }
325 if (!out) {
326 return outlen;
327 }
328
329 // Encode the value.
330 if ((quotes && !maybe_write(out, "\"", 1)) ||
331 do_buf(str->data, str->length, encoding, utf8_convert, flags, NULL, out) <
332 0 ||
333 (quotes && !maybe_write(out, "\"", 1))) {
334 return -1;
335 }
336 return outlen;
337 }
338
339 int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str,
340 unsigned long flags) {
341 BIO *bio = NULL;
342 if (fp != NULL) {
343 // If |fp| is NULL, this function returns the number of bytes without
344 // writing.
345 bio = BIO_new_fp(fp, BIO_NOCLOSE);
346 if (bio == NULL) {
347 return -1;
348 }
349 }
350 int ret = ASN1_STRING_print_ex(bio, str, flags);
351 BIO_free(bio);
352 return ret;
353 }
354
355 int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in) {
356 if (!in) {
357 return -1;
358 }
359 int mbflag = string_type_to_encoding(in->type);
360 if (mbflag == -1) {
361 OPENSSL_PUT_ERROR(ASN1, ASN1_R_UNKNOWN_TAG);
362 return -1;
363 }
364 ASN1_STRING stmp, *str = &stmp;
365 stmp.data = NULL;
366 stmp.length = 0;
367 stmp.flags = 0;
368 int ret =
369 ASN1_mbstring_copy(&str, in->data, in->length, mbflag, B_ASN1_UTF8STRING);
370 if (ret < 0) {
371 return ret;
372 }
373 *out = stmp.data;
374 return stmp.length;
375 }
376
377 int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v) {
378 int i, n;
379 char buf[80];
380 const char *p;
381
382 if (v == NULL) {
383 return 0;
384 }
385 n = 0;
386 p = (const char *)v->data;
387 for (i = 0; i < v->length; i++) {
388 if ((p[i] > '~') || ((p[i] < ' ') && (p[i] != '\n') && (p[i] != '\r'))) {
389 buf[n] = '.';
390 } else {
391 buf[n] = p[i];
392 }
393 n++;
394 if (n >= 80) {
395 if (BIO_write(bp, buf, n) <= 0) {
396 return 0;
397 }
398 n = 0;
399 }
400 }
401 if (n > 0) {
402 if (BIO_write(bp, buf, n) <= 0) {
403 return 0;
404 }
405 }
406 return 1;
407 }
408
409 int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm) {
410 if (tm->type == V_ASN1_UTCTIME) {
411 return ASN1_UTCTIME_print(bp, tm);
412 }
413 if (tm->type == V_ASN1_GENERALIZEDTIME) {
414 return ASN1_GENERALIZEDTIME_print(bp, tm);
415 }
416 BIO_puts(bp, "Bad time value");
417 return 0;
418 }
419
420 static const char *const mon[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
421 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
422
423 int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm) {
424 CBS cbs;
425 CBS_init(&cbs, tm->data, tm->length);
426 struct tm utc;
427 if (!CBS_parse_generalized_time(&cbs, &utc, /*allow_timezone_offset=*/0)) {
428 BIO_puts(bp, "Bad time value");
429 return 0;
430 }
431
432 return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT", mon[utc.tm_mon],
433 utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec,
434 utc.tm_year + 1900) > 0;
435 }
436
437 int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm) {
438 CBS cbs;
439 CBS_init(&cbs, tm->data, tm->length);
440 struct tm utc;
441 if (!CBS_parse_utc_time(&cbs, &utc, /*allow_timezone_offset=*/0)) {
442 BIO_puts(bp, "Bad time value");
443 return 0;
444 }
445
446 return BIO_printf(bp, "%s %2d %02d:%02d:%02d %d GMT", mon[utc.tm_mon],
447 utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec,
448 utc.tm_year + 1900) > 0;
449 }