3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/crypto.h>
64 #include <openssl/x509.h>
65 #include <openssl/asn1.h>
70 * ASN1_STRING_print_ex() and X509_NAME_print_ex(). Enhanced string and name
71 * printing routines handling multibyte characters, RFC2253 and a host of
75 #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
77 #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
78 ASN1_STRFLGS_ESC_QUOTE | \
79 ASN1_STRFLGS_ESC_CTRL | \
83 * Three IO functions for sending data to memory, a BIO and and a FILE
86 #if 0 /* never used */
87 static int send_mem_chars(void *arg
, const void *buf
, int len
)
89 unsigned char **out
= arg
;
92 memcpy(*out
, buf
, len
);
98 static int send_bio_chars(void *arg
, const void *buf
, int len
)
102 if (BIO_write(arg
, buf
, len
) != len
)
107 static int send_fp_chars(void *arg
, const void *buf
, int len
)
111 if (fwrite(buf
, 1, len
, arg
) != (unsigned int)len
)
116 typedef int char_io (void *arg
, const void *buf
, int len
);
119 * This function handles display of strings, one character at a time. It is
120 * passed an unsigned long for each character because it could come from 2 or
124 static int do_esc_char(unsigned long c
, unsigned char flags
, char *do_quotes
,
125 char_io
*io_ch
, void *arg
)
127 unsigned char chflgs
, chtmp
;
128 char tmphex
[HEX_SIZE(long) + 3];
133 BIO_snprintf(tmphex
, sizeof tmphex
, "\\W%08lX", c
);
134 if (!io_ch(arg
, tmphex
, 10))
139 BIO_snprintf(tmphex
, sizeof tmphex
, "\\U%04lX", c
);
140 if (!io_ch(arg
, tmphex
, 6))
144 chtmp
= (unsigned char)c
;
146 chflgs
= flags
& ASN1_STRFLGS_ESC_MSB
;
148 chflgs
= char_type
[chtmp
] & flags
;
149 if (chflgs
& CHARTYPE_BS_ESC
) {
150 /* If we don't escape with quotes, signal we need quotes */
151 if (chflgs
& ASN1_STRFLGS_ESC_QUOTE
) {
154 if (!io_ch(arg
, &chtmp
, 1))
158 if (!io_ch(arg
, "\\", 1))
160 if (!io_ch(arg
, &chtmp
, 1))
164 if (chflgs
& (ASN1_STRFLGS_ESC_CTRL
| ASN1_STRFLGS_ESC_MSB
)) {
165 BIO_snprintf(tmphex
, 11, "\\%02X", chtmp
);
166 if (!io_ch(arg
, tmphex
, 3))
171 * If we get this far and do any escaping at all must escape the escape
172 * character itself: backslash.
174 if (chtmp
== '\\' && flags
& ESC_FLAGS
) {
175 if (!io_ch(arg
, "\\\\", 2))
179 if (!io_ch(arg
, &chtmp
, 1))
184 #define BUF_TYPE_WIDTH_MASK 0x7
185 #define BUF_TYPE_CONVUTF8 0x8
188 * This function sends each character in a buffer to do_esc_char(). It
189 * interprets the content formats and converts to or from UTF8 as
193 static int do_buf(unsigned char *buf
, int buflen
,
194 int type
, unsigned char flags
, char *quotes
, char_io
*io_ch
,
198 unsigned char orflags
, *p
, *q
;
204 if (p
== buf
&& flags
& ASN1_STRFLGS_ESC_2253
)
205 orflags
= CHARTYPE_FIRST_ESC_2253
;
208 switch (type
& BUF_TYPE_WIDTH_MASK
) {
210 c
= ((unsigned long)*p
++) << 24;
211 c
|= ((unsigned long)*p
++) << 16;
212 c
|= ((unsigned long)*p
++) << 8;
217 c
= ((unsigned long)*p
++) << 8;
226 i
= UTF8_getc(p
, buflen
, &c
);
228 return -1; /* Invalid UTF8String */
232 return -1; /* invalid width */
234 if (p
== q
&& flags
& ASN1_STRFLGS_ESC_2253
)
235 orflags
= CHARTYPE_LAST_ESC_2253
;
236 if (type
& BUF_TYPE_CONVUTF8
) {
237 unsigned char utfbuf
[6];
239 utflen
= UTF8_putc(utfbuf
, sizeof utfbuf
, c
);
240 for (i
= 0; i
< utflen
; i
++) {
242 * We don't need to worry about setting orflags correctly
243 * because if utflen==1 its value will be correct anyway
244 * otherwise each character will be > 0x7f and so the
245 * character will never be escaped on first and last.
248 do_esc_char(utfbuf
[i
], (unsigned char)(flags
| orflags
),
256 do_esc_char(c
, (unsigned char)(flags
| orflags
), quotes
,
266 /* This function hex dumps a buffer of characters */
268 static int do_hex_dump(char_io
*io_ch
, void *arg
, unsigned char *buf
,
271 static const char hexdig
[] = "0123456789ABCDEF";
272 unsigned char *p
, *q
;
278 hextmp
[0] = hexdig
[*p
>> 4];
279 hextmp
[1] = hexdig
[*p
& 0xf];
280 if (!io_ch(arg
, hextmp
, 2))
289 * "dump" a string. This is done when the type is unknown, or the flags
290 * request it. We can either dump the content octets or the entire DER
291 * encoding. This uses the RFC2253 #01234 format.
294 static int do_dump(unsigned long lflags
, char_io
*io_ch
, void *arg
,
298 * Placing the ASN1_STRING in a temp ASN1_TYPE allows the DER encoding to
302 unsigned char *der_buf
, *p
;
305 if (!io_ch(arg
, "#", 1))
307 /* If we don't dump DER encoding just dump content octets */
308 if (!(lflags
& ASN1_STRFLGS_DUMP_DER
)) {
309 outlen
= do_hex_dump(io_ch
, arg
, str
->data
, str
->length
);
315 t
.value
.ptr
= (char *)str
;
316 der_len
= i2d_ASN1_TYPE(&t
, NULL
);
317 der_buf
= OPENSSL_malloc(der_len
);
321 i2d_ASN1_TYPE(&t
, &p
);
322 outlen
= do_hex_dump(io_ch
, arg
, der_buf
, der_len
);
323 OPENSSL_free(der_buf
);
330 * Lookup table to convert tags to character widths, 0 = UTF8 encoded, -1 is
331 * used for non string types otherwise it is the number of bytes per
335 static const signed char tag2nbyte
[] = {
336 -1, -1, -1, -1, -1, /* 0-4 */
337 -1, -1, -1, -1, -1, /* 5-9 */
338 -1, -1, 0, -1, /* 10-13 */
339 -1, -1, -1, -1, /* 15-17 */
340 -1, 1, 1, /* 18-20 */
341 -1, 1, 1, 1, /* 21-24 */
342 -1, 1, -1, /* 25-27 */
347 * This is the main function, print out an ASN1_STRING taking note of various
348 * escape and display options. Returns number of characters written or -1 if
352 static int do_print_ex(char_io
*io_ch
, void *arg
, unsigned long lflags
,
360 /* Keep a copy of escape flags */
361 flags
= (unsigned char)(lflags
& ESC_FLAGS
);
367 if (lflags
& ASN1_STRFLGS_SHOW_TYPE
) {
369 tagname
= ASN1_tag2str(type
);
370 outlen
+= strlen(tagname
);
371 if (!io_ch(arg
, tagname
, outlen
) || !io_ch(arg
, ":", 1))
376 /* Decide what to do with type, either dump content or display it */
378 /* Dump everything */
379 if (lflags
& ASN1_STRFLGS_DUMP_ALL
)
381 /* Ignore the string type */
382 else if (lflags
& ASN1_STRFLGS_IGNORE_TYPE
)
385 /* Else determine width based on type */
386 if ((type
> 0) && (type
< 31))
387 type
= tag2nbyte
[type
];
390 if ((type
== -1) && !(lflags
& ASN1_STRFLGS_DUMP_UNKNOWN
))
395 len
= do_dump(lflags
, io_ch
, arg
, str
);
402 if (lflags
& ASN1_STRFLGS_UTF8_CONVERT
) {
404 * Note: if string is UTF8 and we want to convert to UTF8 then we
405 * just interpret it as 1 byte per character to avoid converting
411 type
|= BUF_TYPE_CONVUTF8
;
414 len
= do_buf(str
->data
, str
->length
, type
, flags
, "es
, io_ch
, NULL
);
422 if (quotes
&& !io_ch(arg
, "\"", 1))
424 if (do_buf(str
->data
, str
->length
, type
, flags
, NULL
, io_ch
, arg
) < 0)
426 if (quotes
&& !io_ch(arg
, "\"", 1))
431 /* Used for line indenting: print 'indent' spaces */
433 static int do_indent(char_io
*io_ch
, void *arg
, int indent
)
436 for (i
= 0; i
< indent
; i
++)
437 if (!io_ch(arg
, " ", 1))
442 #define FN_WIDTH_LN 25
443 #define FN_WIDTH_SN 10
445 static int do_name_ex(char_io
*io_ch
, void *arg
, X509_NAME
*n
,
446 int indent
, unsigned long flags
)
448 int i
, prev
= -1, orflags
, cnt
;
452 X509_NAME_ENTRY
*ent
;
456 char *sep_dn
, *sep_mv
, *sep_eq
;
457 int sep_dn_len
, sep_mv_len
, sep_eq_len
;
461 if (!do_indent(io_ch
, arg
, indent
))
463 switch (flags
& XN_FLAG_SEP_MASK
) {
464 case XN_FLAG_SEP_MULTILINE
:
471 case XN_FLAG_SEP_COMMA_PLUS
:
479 case XN_FLAG_SEP_CPLUS_SPC
:
487 case XN_FLAG_SEP_SPLUS_SPC
:
499 if (flags
& XN_FLAG_SPC_EQ
) {
507 fn_opt
= flags
& XN_FLAG_FN_MASK
;
509 cnt
= X509_NAME_entry_count(n
);
510 for (i
= 0; i
< cnt
; i
++) {
511 if (flags
& XN_FLAG_DN_REV
)
512 ent
= X509_NAME_get_entry(n
, cnt
- i
- 1);
514 ent
= X509_NAME_get_entry(n
, i
);
516 if (prev
== ent
->set
) {
517 if (!io_ch(arg
, sep_mv
, sep_mv_len
))
519 outlen
+= sep_mv_len
;
521 if (!io_ch(arg
, sep_dn
, sep_dn_len
))
523 outlen
+= sep_dn_len
;
524 if (!do_indent(io_ch
, arg
, indent
))
530 fn
= X509_NAME_ENTRY_get_object(ent
);
531 val
= X509_NAME_ENTRY_get_data(ent
);
532 fn_nid
= OBJ_obj2nid(fn
);
533 if (fn_opt
!= XN_FLAG_FN_NONE
) {
535 if ((fn_opt
== XN_FLAG_FN_OID
) || (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
;
564 * If the field name is unknown then fix up the DER dump flag. We
565 * might want to limit this further so it will DER dump on anything
566 * other than a few 'standard' fields.
568 if ((fn_nid
== NID_undef
) && (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 */
583 int X509_NAME_print_ex(BIO
*out
, X509_NAME
*nm
, int indent
,
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
);
591 #ifndef OPENSSL_NO_FP_API
592 int X509_NAME_print_ex_fp(FILE *fp
, X509_NAME
*nm
, int indent
,
595 if (flags
== XN_FLAG_COMPAT
) {
598 btmp
= BIO_new_fp(fp
, BIO_NOCLOSE
);
601 ret
= X509_NAME_print(btmp
, nm
, indent
);
605 return do_name_ex(send_fp_chars
, fp
, nm
, indent
, flags
);
609 int ASN1_STRING_print_ex(BIO
*out
, ASN1_STRING
*str
, unsigned long flags
)
611 return do_print_ex(send_bio_chars
, out
, flags
, str
);
614 #ifndef OPENSSL_NO_FP_API
615 int ASN1_STRING_print_ex_fp(FILE *fp
, ASN1_STRING
*str
, unsigned long flags
)
617 return do_print_ex(send_fp_chars
, fp
, flags
, str
);
622 * Utility function: convert any string type to UTF8, returns number of bytes
623 * in output string or a negative error code
626 int ASN1_STRING_to_UTF8(unsigned char **out
, ASN1_STRING
*in
)
628 ASN1_STRING stmp
, *str
= &stmp
;
629 int mbflag
, type
, ret
;
633 if ((type
< 0) || (type
> 30))
635 mbflag
= tag2nbyte
[type
];
638 mbflag
|= MBSTRING_FLAG
;
642 ASN1_mbstring_copy(&str
, in
->data
, in
->length
, mbflag
,