1 /* $OpenBSD: a_strex.c,v 1.24 2014/07/11 08:44:47 jsing Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
62 #include <openssl/asn1.h>
63 #include <openssl/crypto.h>
64 #include <openssl/x509.h>
66 #include "asn1_locl.h"
70 /* ASN1_STRING_print_ex() and X509_NAME_print_ex().
71 * Enhanced string and name printing routines handling
72 * multibyte characters, RFC2253 and a host of other
76 #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
78 #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
79 ASN1_STRFLGS_ESC_QUOTE | \
80 ASN1_STRFLGS_ESC_CTRL | \
84 /* Three IO functions for sending data to memory, a BIO and
88 send_bio_chars(void *arg, const void *buf, int len)
92 if (BIO_write(arg, buf, len) != len)
98 send_fp_chars(void *arg, const void *buf, int len)
102 if (fwrite(buf, 1, (size_t)len, arg) != (size_t)len)
107 typedef int char_io(void *arg, const void *buf, int len);
109 /* This function handles display of
110 * strings, one character at a time.
111 * It is passed an unsigned long for each
112 * character because it could come from 2 or even
117 do_esc_char(unsigned long c, unsigned char flags, char *do_quotes,
118 char_io *io_ch, void *arg)
120 unsigned char chflgs, chtmp;
121 char tmphex[sizeof(long) * 2 + 3];
126 snprintf(tmphex, sizeof tmphex, "\\W%08lX", c);
127 if (!io_ch(arg, tmphex, 10))
132 snprintf(tmphex, sizeof tmphex, "\\U%04lX", c);
133 if (!io_ch(arg, tmphex, 6))
137 chtmp = (unsigned char)c;
139 chflgs = flags & ASN1_STRFLGS_ESC_MSB;
141 chflgs = char_type[chtmp] & flags;
142 if (chflgs & CHARTYPE_BS_ESC) {
143 /* If we don't escape with quotes, signal we need quotes */
144 if (chflgs & ASN1_STRFLGS_ESC_QUOTE) {
147 if (!io_ch(arg, &chtmp, 1))
151 if (!io_ch(arg, "\\", 1))
153 if (!io_ch(arg, &chtmp, 1))
157 if (chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) {
158 snprintf(tmphex, sizeof tmphex, "\\%02X", chtmp);
159 if (!io_ch(arg, tmphex, 3))
163 /* If we get this far and do any escaping at all must escape
164 * the escape character itself: backslash.
166 if (chtmp == '\\' && flags & ESC_FLAGS) {
167 if (!io_ch(arg, "\\\\", 2))
171 if (!io_ch(arg, &chtmp, 1))
176 #define BUF_TYPE_WIDTH_MASK 0x7
177 #define BUF_TYPE_CONVUTF8 0x8
179 /* This function sends each character in a buffer to
180 * do_esc_char(). It interprets the content formats
181 * and converts to or from UTF8 as appropriate.
185 do_buf(unsigned char *buf, int buflen, int type, unsigned char flags,
186 char *quotes, char_io *io_ch, void *arg)
189 unsigned char orflags, *p, *q;
196 if (p == buf && flags & ASN1_STRFLGS_ESC_2253)
197 orflags = CHARTYPE_FIRST_ESC_2253;
200 switch (type & BUF_TYPE_WIDTH_MASK) {
202 c = ((unsigned long)*p++) << 24;
203 c |= ((unsigned long)*p++) << 16;
204 c |= ((unsigned long)*p++) << 8;
206 if (c > UNICODE_MAX || UNICODE_IS_SURROGATE(c))
211 c = ((unsigned long)*p++) << 8;
213 if (UNICODE_IS_SURROGATE(c))
222 i = UTF8_getc(p, q - p, &c);
224 return -1; /* Invalid UTF8String */
228 return -1; /* invalid width */
230 if (p == q && flags & ASN1_STRFLGS_ESC_2253)
231 orflags = CHARTYPE_LAST_ESC_2253;
232 if (type & BUF_TYPE_CONVUTF8) {
233 unsigned char utfbuf[6];
236 utflen = UTF8_putc(utfbuf, sizeof utfbuf, c);
239 for (i = 0; i < utflen; i++) {
240 /* We don't need to worry about setting orflags correctly
241 * because if utflen==1 its value will be correct anyway
242 * otherwise each character will be > 0x7f and so the
243 * character will never be escaped on first and last.
245 len = do_esc_char(utfbuf[i],
246 (unsigned char)(flags | orflags), quotes,
253 len = do_esc_char(c, (unsigned char)(flags | orflags),
263 /* This function hex dumps a buffer of characters */
266 do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, int buflen)
268 static const char hexdig[] = "0123456789ABCDEF";
269 unsigned char *p, *q;
275 hextmp[0] = hexdig[*p >> 4];
276 hextmp[1] = hexdig[*p & 0xf];
277 if (!io_ch(arg, hextmp, 2))
285 /* "dump" a string. This is done when the type is unknown,
286 * or the flags request it. We can either dump the content
287 * octets or the entire DER encoding. This uses the RFC2253
292 do_dump(unsigned long lflags, char_io *io_ch, void *arg, ASN1_STRING *str)
294 /* Placing the ASN1_STRING in a temp ASN1_TYPE allows
295 * the DER encoding to readily obtained
298 unsigned char *der_buf, *p;
301 if (!io_ch(arg, "#", 1))
303 /* If we don't dump DER encoding just dump content octets */
304 if (!(lflags & ASN1_STRFLGS_DUMP_DER)) {
305 outlen = do_hex_dump(io_ch, arg, str->data, str->length);
311 t.value.ptr = (char *)str;
312 der_len = i2d_ASN1_TYPE(&t, NULL);
313 der_buf = malloc(der_len);
317 i2d_ASN1_TYPE(&t, &p);
318 outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
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
330 static 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 */
341 /* This is the main function, print out an
342 * ASN1_STRING taking note of various escape
343 * and display options. Returns number of
344 * characters written or -1 if an error
349 do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, ASN1_STRING *str)
357 /* Keep a copy of escape flags */
358 flags = (unsigned char)(lflags & ESC_FLAGS);
362 if (lflags & ASN1_STRFLGS_SHOW_TYPE) {
364 tagname = ASN1_tag2str(type);
365 outlen += strlen(tagname);
366 if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1))
371 /* Decide what to do with type, either dump content or display it */
373 /* Dump everything */
374 if (lflags & ASN1_STRFLGS_DUMP_ALL)
376 /* Ignore the string type */
377 else if (lflags & ASN1_STRFLGS_IGNORE_TYPE)
380 /* Else determine width based on type */
381 if ((type > 0) && (type < 31))
382 type = tag2nbyte[type];
385 if ((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN))
390 len = do_dump(lflags, io_ch, arg, str);
397 if (lflags & ASN1_STRFLGS_UTF8_CONVERT) {
398 /* Note: if string is UTF8 and we want
399 * to convert to UTF8 then we just interpret
400 * it as 1 byte per character to avoid converting
406 type |= BUF_TYPE_CONVUTF8;
409 len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL);
417 if (quotes && !io_ch(arg, "\"", 1))
419 if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
421 if (quotes && !io_ch(arg, "\"", 1))
426 /* Used for line indenting: print 'indent' spaces */
429 do_indent(char_io *io_ch, void *arg, int indent)
432 for (i = 0; i < indent; i++)
433 if (!io_ch(arg, " ", 1))
438 #define FN_WIDTH_LN 25
439 #define FN_WIDTH_SN 10
442 do_name_ex(char_io *io_ch, void *arg, X509_NAME *n, int indent,
445 int i, prev = -1, orflags, cnt;
449 X509_NAME_ENTRY *ent;
453 char *sep_dn, *sep_mv, *sep_eq;
454 int sep_dn_len, sep_mv_len, sep_eq_len;
459 if (!do_indent(io_ch, arg, indent))
462 switch (flags & XN_FLAG_SEP_MASK) {
463 case XN_FLAG_SEP_MULTILINE:
470 case XN_FLAG_SEP_COMMA_PLUS:
478 case XN_FLAG_SEP_CPLUS_SPC:
486 case XN_FLAG_SEP_SPLUS_SPC:
498 if (flags & XN_FLAG_SPC_EQ) {
506 fn_opt = flags & XN_FLAG_FN_MASK;
508 cnt = X509_NAME_entry_count(n);
509 for (i = 0; i < cnt; i++) {
510 if (flags & XN_FLAG_DN_REV)
511 ent = X509_NAME_get_entry(n, cnt - i - 1);
513 ent = X509_NAME_get_entry(n, i);
515 if (prev == ent->set) {
516 if (!io_ch(arg, sep_mv, sep_mv_len))
518 outlen += sep_mv_len;
520 if (!io_ch(arg, sep_dn, sep_dn_len))
522 outlen += sep_dn_len;
523 if (!do_indent(io_ch, arg, indent))
529 fn = X509_NAME_ENTRY_get_object(ent);
530 val = X509_NAME_ENTRY_get_data(ent);
531 fn_nid = OBJ_obj2nid(fn);
532 if (fn_opt != XN_FLAG_FN_NONE) {
534 if ((fn_opt == XN_FLAG_FN_OID) ||
535 (fn_nid == NID_undef)) {
536 OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1);
537 fld_len = 0; /* XXX: what should this be? */
540 if (fn_opt == XN_FLAG_FN_SN) {
541 fld_len = FN_WIDTH_SN;
542 objbuf = OBJ_nid2sn(fn_nid);
543 } else if (fn_opt == XN_FLAG_FN_LN) {
544 fld_len = FN_WIDTH_LN;
545 objbuf = OBJ_nid2ln(fn_nid);
547 fld_len = 0; /* XXX: what should this be? */
551 objlen = strlen(objbuf);
552 if (!io_ch(arg, objbuf, objlen))
554 if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
555 if (!do_indent(io_ch, arg, fld_len - objlen))
557 outlen += fld_len - objlen;
559 if (!io_ch(arg, sep_eq, sep_eq_len))
561 outlen += objlen + sep_eq_len;
563 /* If the field name is unknown then fix up the DER dump
564 * flag. We might want to limit this further so it will
565 * DER dump on anything other than a few 'standard' fields.
567 if ((fn_nid == NID_undef) &&
568 (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
569 orflags = ASN1_STRFLGS_DUMP_ALL;
573 len = do_print_ex(io_ch, arg, flags | orflags, val);
581 /* Wrappers round the main functions */
584 X509_NAME_print_ex(BIO *out, X509_NAME *nm, int indent, unsigned long flags)
586 if (flags == XN_FLAG_COMPAT)
587 return X509_NAME_print(out, nm, indent);
588 return do_name_ex(send_bio_chars, out, nm, indent, flags);
592 X509_NAME_print_ex_fp(FILE *fp, X509_NAME *nm, int indent, unsigned long flags)
594 if (flags == XN_FLAG_COMPAT) {
597 btmp = BIO_new_fp(fp, BIO_NOCLOSE);
600 ret = X509_NAME_print(btmp, nm, indent);
604 return do_name_ex(send_fp_chars, fp, nm, indent, flags);
608 ASN1_STRING_print_ex(BIO *out, ASN1_STRING *str, unsigned long flags)
610 return do_print_ex(send_bio_chars, out, flags, str);
614 ASN1_STRING_print_ex_fp(FILE *fp, ASN1_STRING *str, unsigned long flags)
616 return do_print_ex(send_fp_chars, fp, flags, str);
619 /* Utility function: convert any string type to UTF8, returns number of bytes
620 * in output string or a negative error code
624 ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in)
626 ASN1_STRING stmp, *str = &stmp;
627 int mbflag, type, ret;
632 if ((type < 0) || (type > 30))
634 mbflag = tag2nbyte[type];
637 mbflag |= MBSTRING_FLAG;
640 ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag,