2 * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include "internal/asn1_int.h"
14 #include <openssl/crypto.h>
15 #include <openssl/x509.h>
16 #include <openssl/asn1.h>
21 * ASN1_STRING_print_ex() and X509_NAME_print_ex(). Enhanced string and name
22 * printing routines handling multibyte characters, RFC2253 and a host of
26 #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
28 #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
29 ASN1_STRFLGS_ESC_2254 | \
30 ASN1_STRFLGS_ESC_QUOTE | \
31 ASN1_STRFLGS_ESC_CTRL | \
35 * Three IO functions for sending data to memory, a BIO and and a FILE
38 static int send_bio_chars(void *arg, const void *buf, int len)
42 if (BIO_write(arg, buf, len) != len)
47 #ifndef OPENSSL_NO_STDIO
48 static int send_fp_chars(void *arg, const void *buf, int len)
52 if (fwrite(buf, 1, len, arg) != (unsigned int)len)
58 typedef int char_io (void *arg, const void *buf, int len);
61 * This function handles display of strings, one character at a time. It is
62 * passed an unsigned long for each character because it could come from 2 or
66 static int do_esc_char(unsigned long c, unsigned short flags, char *do_quotes,
67 char_io *io_ch, void *arg)
69 unsigned short chflgs;
71 char tmphex[HEX_SIZE(long) + 3];
76 BIO_snprintf(tmphex, sizeof(tmphex), "\\W%08lX", c);
77 if (!io_ch(arg, tmphex, 10))
82 BIO_snprintf(tmphex, sizeof(tmphex), "\\U%04lX", c);
83 if (!io_ch(arg, tmphex, 6))
87 chtmp = (unsigned char)c;
89 chflgs = flags & ASN1_STRFLGS_ESC_MSB;
91 chflgs = char_type[chtmp] & flags;
92 if (chflgs & CHARTYPE_BS_ESC) {
93 /* If we don't escape with quotes, signal we need quotes */
94 if (chflgs & ASN1_STRFLGS_ESC_QUOTE) {
97 if (!io_ch(arg, &chtmp, 1))
101 if (!io_ch(arg, "\\", 1))
103 if (!io_ch(arg, &chtmp, 1))
107 if (chflgs & (ASN1_STRFLGS_ESC_CTRL
108 | ASN1_STRFLGS_ESC_MSB
109 | ASN1_STRFLGS_ESC_2254)) {
110 BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
111 if (!io_ch(arg, tmphex, 3))
116 * If we get this far and do any escaping at all must escape the escape
117 * character itself: backslash.
119 if (chtmp == '\\' && (flags & ESC_FLAGS)) {
120 if (!io_ch(arg, "\\\\", 2))
124 if (!io_ch(arg, &chtmp, 1))
129 #define BUF_TYPE_WIDTH_MASK 0x7
130 #define BUF_TYPE_CONVUTF8 0x8
133 * This function sends each character in a buffer to do_esc_char(). It
134 * interprets the content formats and converts to or from UTF8 as
138 static int do_buf(unsigned char *buf, int buflen,
139 int type, unsigned short flags, char *quotes, char_io *io_ch,
142 int i, outlen, len, charwidth;
143 unsigned short orflags;
144 unsigned char *p, *q;
150 charwidth = type & BUF_TYPE_WIDTH_MASK;
155 ASN1err(ASN1_F_DO_BUF, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
161 ASN1err(ASN1_F_DO_BUF, ASN1_R_INVALID_BMPSTRING_LENGTH);
170 if (p == buf && flags & ASN1_STRFLGS_ESC_2253)
171 orflags = CHARTYPE_FIRST_ESC_2253;
177 c = ((unsigned long)*p++) << 24;
178 c |= ((unsigned long)*p++) << 16;
179 c |= ((unsigned long)*p++) << 8;
184 c = ((unsigned long)*p++) << 8;
193 i = UTF8_getc(p, buflen, &c);
195 return -1; /* Invalid UTF8String */
200 return -1; /* invalid width */
202 if (p == q && flags & ASN1_STRFLGS_ESC_2253)
203 orflags = CHARTYPE_LAST_ESC_2253;
204 if (type & BUF_TYPE_CONVUTF8) {
205 unsigned char utfbuf[6];
207 utflen = UTF8_putc(utfbuf, sizeof(utfbuf), c);
208 for (i = 0; i < utflen; i++) {
210 * We don't need to worry about setting orflags correctly
211 * because if utflen==1 its value will be correct anyway
212 * otherwise each character will be > 0x7f and so the
213 * character will never be escaped on first and last.
215 len = do_esc_char(utfbuf[i], flags | orflags, quotes,
222 len = do_esc_char(c, flags | orflags, quotes,
232 /* This function hex dumps a buffer of characters */
234 static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf,
237 static const char hexdig[] = "0123456789ABCDEF";
238 unsigned char *p, *q;
244 hextmp[0] = hexdig[*p >> 4];
245 hextmp[1] = hexdig[*p & 0xf];
246 if (!io_ch(arg, hextmp, 2))
255 * "dump" a string. This is done when the type is unknown, or the flags
256 * request it. We can either dump the content octets or the entire DER
257 * encoding. This uses the RFC2253 #01234 format.
260 static int do_dump(unsigned long lflags, char_io *io_ch, void *arg,
261 const ASN1_STRING *str)
264 * Placing the ASN1_STRING in a temp ASN1_TYPE allows the DER encoding to
268 unsigned char *der_buf, *p;
271 if (!io_ch(arg, "#", 1))
273 /* If we don't dump DER encoding just dump content octets */
274 if (!(lflags & ASN1_STRFLGS_DUMP_DER)) {
275 outlen = do_hex_dump(io_ch, arg, str->data, str->length);
281 t.value.ptr = (char *)str;
282 der_len = i2d_ASN1_TYPE(&t, NULL);
283 if ((der_buf = OPENSSL_malloc(der_len)) == NULL) {
284 ASN1err(ASN1_F_DO_DUMP, ERR_R_MALLOC_FAILURE);
288 i2d_ASN1_TYPE(&t, &p);
289 outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
290 OPENSSL_free(der_buf);
297 * Lookup table to convert tags to character widths, 0 = UTF8 encoded, -1 is
298 * used for non string types otherwise it is the number of bytes per
302 static const signed char tag2nbyte[] = {
303 -1, -1, -1, -1, -1, /* 0-4 */
304 -1, -1, -1, -1, -1, /* 5-9 */
306 0, /* 12 V_ASN1_UTF8STRING */
307 -1, -1, -1, -1, -1, /* 13-17 */
308 1, /* 18 V_ASN1_NUMERICSTRING */
309 1, /* 19 V_ASN1_PRINTABLESTRING */
310 1, /* 20 V_ASN1_T61STRING */
312 1, /* 22 V_ASN1_IA5STRING */
313 1, /* 23 V_ASN1_UTCTIME */
314 1, /* 24 V_ASN1_GENERALIZEDTIME */
316 1, /* 26 V_ASN1_ISO64STRING */
318 4, /* 28 V_ASN1_UNIVERSALSTRING */
320 2 /* 30 V_ASN1_BMPSTRING */
324 * This is the main function, print out an ASN1_STRING taking note of various
325 * escape and display options. Returns number of characters written or -1 if
329 static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags,
330 const ASN1_STRING *str)
335 unsigned short flags;
337 /* Keep a copy of escape flags */
338 flags = (unsigned short)(lflags & ESC_FLAGS);
344 if (lflags & ASN1_STRFLGS_SHOW_TYPE) {
346 tagname = ASN1_tag2str(type);
347 outlen += strlen(tagname);
348 if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1))
353 /* Decide what to do with type, either dump content or display it */
355 /* Dump everything */
356 if (lflags & ASN1_STRFLGS_DUMP_ALL)
358 /* Ignore the string type */
359 else if (lflags & ASN1_STRFLGS_IGNORE_TYPE)
362 /* Else determine width based on type */
363 if ((type > 0) && (type < 31))
364 type = tag2nbyte[type];
367 if ((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN))
372 len = do_dump(lflags, io_ch, arg, str);
379 if (lflags & ASN1_STRFLGS_UTF8_CONVERT) {
381 * Note: if string is UTF8 and we want to convert to UTF8 then we
382 * just interpret it as 1 byte per character to avoid converting
388 type |= BUF_TYPE_CONVUTF8;
391 len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL);
399 if (quotes && !io_ch(arg, "\"", 1))
401 if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
403 if (quotes && !io_ch(arg, "\"", 1))
408 /* Used for line indenting: print 'indent' spaces */
410 static int do_indent(char_io *io_ch, void *arg, int indent)
413 for (i = 0; i < indent; i++)
414 if (!io_ch(arg, " ", 1))
419 #define FN_WIDTH_LN 25
420 #define FN_WIDTH_SN 10
422 static int do_name_ex(char_io *io_ch, void *arg, const X509_NAME *n,
423 int indent, unsigned long flags)
425 int i, prev = -1, orflags, cnt;
428 const ASN1_STRING *val;
429 const X509_NAME_ENTRY *ent;
433 char *sep_dn, *sep_mv, *sep_eq;
434 int sep_dn_len, sep_mv_len, sep_eq_len;
438 if (!do_indent(io_ch, arg, indent))
440 switch (flags & XN_FLAG_SEP_MASK) {
441 case XN_FLAG_SEP_MULTILINE:
448 case XN_FLAG_SEP_COMMA_PLUS:
456 case XN_FLAG_SEP_CPLUS_SPC:
464 case XN_FLAG_SEP_SPLUS_SPC:
476 if (flags & XN_FLAG_SPC_EQ) {
484 fn_opt = flags & XN_FLAG_FN_MASK;
486 cnt = X509_NAME_entry_count(n);
487 for (i = 0; i < cnt; i++) {
488 if (flags & XN_FLAG_DN_REV)
489 ent = X509_NAME_get_entry(n, cnt - i - 1);
491 ent = X509_NAME_get_entry(n, i);
493 if (prev == X509_NAME_ENTRY_set(ent)) {
494 if (!io_ch(arg, sep_mv, sep_mv_len))
496 outlen += sep_mv_len;
498 if (!io_ch(arg, sep_dn, sep_dn_len))
500 outlen += sep_dn_len;
501 if (!do_indent(io_ch, arg, indent))
506 prev = X509_NAME_ENTRY_set(ent);
507 fn = X509_NAME_ENTRY_get_object(ent);
508 val = X509_NAME_ENTRY_get_data(ent);
509 fn_nid = OBJ_obj2nid(fn);
510 if (fn_opt != XN_FLAG_FN_NONE) {
512 if ((fn_opt == XN_FLAG_FN_OID) || (fn_nid == NID_undef)) {
513 OBJ_obj2txt(objtmp, sizeof(objtmp), fn, 1);
514 fld_len = 0; /* XXX: what should this be? */
517 if (fn_opt == XN_FLAG_FN_SN) {
518 fld_len = FN_WIDTH_SN;
519 objbuf = OBJ_nid2sn(fn_nid);
520 } else if (fn_opt == XN_FLAG_FN_LN) {
521 fld_len = FN_WIDTH_LN;
522 objbuf = OBJ_nid2ln(fn_nid);
524 fld_len = 0; /* XXX: what should this be? */
528 objlen = strlen(objbuf);
529 if (!io_ch(arg, objbuf, objlen))
531 if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
532 if (!do_indent(io_ch, arg, fld_len - objlen))
534 outlen += fld_len - objlen;
536 if (!io_ch(arg, sep_eq, sep_eq_len))
538 outlen += objlen + sep_eq_len;
541 * If the field name is unknown then fix up the DER dump flag. We
542 * might want to limit this further so it will DER dump on anything
543 * other than a few 'standard' fields.
545 if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
546 orflags = ASN1_STRFLGS_DUMP_ALL;
550 len = do_print_ex(io_ch, arg, flags | orflags, val);
558 /* Wrappers round the main functions */
560 int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent,
563 if (flags == XN_FLAG_COMPAT)
564 return X509_NAME_print(out, nm, indent);
565 return do_name_ex(send_bio_chars, out, nm, indent, flags);
568 #ifndef OPENSSL_NO_STDIO
569 int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent,
572 if (flags == XN_FLAG_COMPAT) {
575 btmp = BIO_new_fp(fp, BIO_NOCLOSE);
578 ret = X509_NAME_print(btmp, nm, indent);
582 return do_name_ex(send_fp_chars, fp, nm, indent, flags);
586 int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags)
588 return do_print_ex(send_bio_chars, out, flags, str);
591 #ifndef OPENSSL_NO_STDIO
592 int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags)
594 return do_print_ex(send_fp_chars, fp, flags, str);
599 * Utility function: convert any string type to UTF8, returns number of bytes
600 * in output string or a negative error code
603 int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in)
605 ASN1_STRING stmp, *str = &stmp;
606 int mbflag, type, ret;
610 if ((type < 0) || (type > 30))
612 mbflag = tag2nbyte[type];
615 mbflag |= MBSTRING_FLAG;
620 ASN1_mbstring_copy(&str, in->data, in->length, mbflag,
628 /* Return 1 if host is a valid hostname and 0 otherwise */
629 int asn1_valid_host(const ASN1_STRING *host)
631 int hostlen = host->length;
632 const unsigned char *hostptr = host->data;
633 int type = host->type;
635 signed char width = -1;
636 unsigned short chflags = 0, prevchflags;
638 if (type > 0 && type < 31)
639 width = tag2nbyte[type];
640 if (width == -1 || hostlen == 0)
642 /* Treat UTF8String as width 1 as any MSB set is invalid */
645 for (i = 0 ; i < hostlen; i+= width) {
646 prevchflags = chflags;
647 /* Value must be <= 0x7F: check upper bytes are all zeroes */
649 if (*hostptr++ != 0 || *hostptr++ != 0 || *hostptr++ != 0)
651 } else if (width == 2) {
657 chflags = char_type[*hostptr++];
658 if (!(chflags & (CHARTYPE_HOST_ANY | CHARTYPE_HOST_WILD))) {
659 /* Nothing else allowed at start or end of string */
660 if (i == 0 || i == hostlen - 1)
662 /* Otherwise invalid if not dot or hyphen */
663 if (!(chflags & (CHARTYPE_HOST_DOT | CHARTYPE_HOST_HYPHEN)))
666 * If previous is dot or hyphen then illegal unless both
667 * are hyphens: as .- -. .. are all illegal
669 if (prevchflags & (CHARTYPE_HOST_DOT | CHARTYPE_HOST_HYPHEN)
670 && ((prevchflags & CHARTYPE_HOST_DOT)
671 || (chflags & CHARTYPE_HOST_DOT)))