2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) 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).
61 #include <openssl/crypto.h>
62 #include <openssl/x509.h>
63 #include <openssl/asn1.h>
67 /* ASN1_STRING_print_ex() and X509_NAME_print_ex().
68 * Enhanced string and name printing routines handling
69 * multibyte characters, RFC2253 and a host of other
74 #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
77 /* Three IO functions for sending data to memory, a BIO and
80 #if 0 /* never used */
81 static int send_mem_chars(void *arg, const void *buf, int len)
83 unsigned char **out = arg;
85 memcpy(*out, buf, len);
91 static int send_bio_chars(void *arg, const void *buf, int len)
94 if(BIO_write(arg, buf, len) != len) return 0;
98 static int send_fp_chars(void *arg, const void *buf, int len)
101 if(fwrite(buf, 1, len, arg) != (unsigned int)len) return 0;
105 typedef int char_io(void *arg, const void *buf, int len);
107 /* This function handles display of
108 * strings, one character at a time.
109 * It is passed an unsigned long for each
110 * character because it could come from 2 or even
114 static int do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, char_io *io_ch, void *arg)
116 unsigned char chflgs, chtmp;
119 BIO_snprintf(tmphex, 11, "\\W%08lX", c);
120 if(!io_ch(arg, tmphex, 10)) return -1;
124 BIO_snprintf(tmphex, 11, "\\U%04lX", c);
125 if(!io_ch(arg, tmphex, 6)) return -1;
128 chtmp = (unsigned char)c;
129 if(chtmp > 0x7f) chflgs = flags & ASN1_STRFLGS_ESC_MSB;
130 else chflgs = char_type[chtmp] & flags;
131 if(chflgs & CHARTYPE_BS_ESC) {
132 /* If we don't escape with quotes, signal we need quotes */
133 if(chflgs & ASN1_STRFLGS_ESC_QUOTE) {
134 if(do_quotes) *do_quotes = 1;
135 if(!io_ch(arg, &chtmp, 1)) return -1;
138 if(!io_ch(arg, "\\", 1)) return -1;
139 if(!io_ch(arg, &chtmp, 1)) return -1;
142 if(chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) {
143 BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
144 if(!io_ch(arg, tmphex, 3)) return -1;
147 if(!io_ch(arg, &chtmp, 1)) return -1;
151 #define BUF_TYPE_WIDTH_MASK 0x7
152 #define BUF_TYPE_CONVUTF8 0x8
154 /* This function sends each character in a buffer to
155 * do_esc_char(). It interprets the content formats
156 * and converts to or from UTF8 as appropriate.
159 static int do_buf(unsigned char *buf, int buflen,
160 int type, unsigned char flags, char *quotes, char_io *io_ch, void *arg)
163 unsigned char orflags, *p, *q;
169 if(p == buf) orflags = CHARTYPE_FIRST_ESC_2253;
171 switch(type & BUF_TYPE_WIDTH_MASK) {
173 c = ((unsigned long)*p++) << 24;
174 c |= ((unsigned long)*p++) << 16;
175 c |= ((unsigned long)*p++) << 8;
180 c = ((unsigned long)*p++) << 8;
189 i = UTF8_getc(p, buflen, &c);
190 if(i < 0) return -1; /* Invalid UTF8String */
194 if (p == q) orflags = CHARTYPE_LAST_ESC_2253;
195 if(type & BUF_TYPE_CONVUTF8) {
196 unsigned char utfbuf[6];
198 utflen = UTF8_putc(utfbuf, 6, c);
199 for(i = 0; i < utflen; i++) {
200 /* We don't need to worry about setting orflags correctly
201 * because if utflen==1 its value will be correct anyway
202 * otherwise each character will be > 0x7f and so the
203 * character will never be escaped on first and last.
205 len = do_esc_char(utfbuf[i], (unsigned char)(flags | orflags), quotes, io_ch, arg);
206 if(len < 0) return -1;
210 len = do_esc_char(c, (unsigned char)(flags | orflags), quotes, io_ch, arg);
211 if(len < 0) return -1;
218 /* This function hex dumps a buffer of characters */
220 static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, int buflen)
222 const static char hexdig[] = "0123456789ABCDEF";
223 unsigned char *p, *q;
229 hextmp[0] = hexdig[*p >> 4];
230 hextmp[1] = hexdig[*p & 0xf];
231 if(!io_ch(arg, hextmp, 2)) return -1;
238 /* "dump" a string. This is done when the type is unknown,
239 * or the flags request it. We can either dump the content
240 * octets or the entire DER encoding. This uses the RFC2253
244 static int do_dump(unsigned long lflags, char_io *io_ch, void *arg, ASN1_STRING *str)
246 /* Placing the ASN1_STRING in a temp ASN1_TYPE allows
247 * the DER encoding to readily obtained
250 unsigned char *der_buf, *p;
253 if(!io_ch(arg, "#", 1)) return -1;
254 /* If we don't dump DER encoding just dump content octets */
255 if(!(lflags & ASN1_STRFLGS_DUMP_DER)) {
256 outlen = do_hex_dump(io_ch, arg, str->data, str->length);
257 if(outlen < 0) return -1;
261 t.value.ptr = (char *)str;
262 der_len = i2d_ASN1_TYPE(&t, NULL);
263 der_buf = OPENSSL_malloc(der_len);
264 if(!der_buf) return -1;
266 i2d_ASN1_TYPE(&t, &p);
267 outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
268 OPENSSL_free(der_buf);
269 if(outlen < 0) return -1;
273 /* Lookup table to convert tags to character widths,
274 * 0 = UTF8 encoded, -1 is used for non string types
275 * otherwise it is the number of bytes per character
278 const static char tag2nbyte[] = {
279 -1, -1, -1, -1, -1, /* 0-4 */
280 -1, -1, -1, -1, -1, /* 5-9 */
281 -1, -1, 0, -1, /* 10-13 */
282 -1, -1, -1, -1, /* 15-17 */
283 -1, 1, 1, /* 18-20 */
284 -1, 1, -1,-1, /* 21-24 */
285 -1, 1, -1, /* 25-27 */
289 #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
290 ASN1_STRFLGS_ESC_QUOTE | \
291 ASN1_STRFLGS_ESC_CTRL | \
292 ASN1_STRFLGS_ESC_MSB)
294 /* This is the main function, print out an
295 * ASN1_STRING taking note of various escape
296 * and display options. Returns number of
297 * characters written or -1 if an error
301 static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, ASN1_STRING *str)
308 /* Keep a copy of escape flags */
309 flags = (unsigned char)(lflags & ESC_FLAGS);
316 if(lflags & ASN1_STRFLGS_SHOW_TYPE) {
318 tagname = ASN1_tag2str(type);
319 outlen += strlen(tagname);
320 if(!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1)) return -1;
324 /* Decide what to do with type, either dump content or display it */
326 /* Dump everything */
327 if(lflags & ASN1_STRFLGS_DUMP_ALL) type = -1;
328 /* Ignore the string type */
329 else if(lflags & ASN1_STRFLGS_IGNORE_TYPE) type = 1;
331 /* Else determine width based on type */
332 if((type > 0) && (type < 31)) type = tag2nbyte[type];
334 if((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN)) type = 1;
338 len = do_dump(lflags, io_ch, arg, str);
339 if(len < 0) return -1;
344 if(lflags & ASN1_STRFLGS_UTF8_CONVERT) {
345 /* Note: if string is UTF8 and we want
346 * to convert to UTF8 then we just interpret
347 * it as 1 byte per character to avoid converting
351 else type |= BUF_TYPE_CONVUTF8;
354 len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL);
355 if(outlen < 0) return -1;
357 if(quotes) outlen += 2;
358 if(!arg) return outlen;
359 if(quotes && !io_ch(arg, "\"", 1)) return -1;
360 do_buf(str->data, str->length, type, flags, NULL, io_ch, arg);
361 if(quotes && !io_ch(arg, "\"", 1)) return -1;
365 /* Used for line indenting: print 'indent' spaces */
367 static int do_indent(char_io *io_ch, void *arg, int indent)
370 for(i = 0; i < indent; i++)
371 if(!io_ch(arg, " ", 1)) return 0;
375 #define FN_WIDTH_LN 25
376 #define FN_WIDTH_SN 10
378 static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n,
379 int indent, unsigned long flags)
381 int i, prev = -1, orflags, cnt;
385 X509_NAME_ENTRY *ent;
389 char *sep_dn, *sep_mv, *sep_eq;
390 int sep_dn_len, sep_mv_len, sep_eq_len;
391 if(indent < 0) indent = 0;
393 if(!do_indent(io_ch, arg, indent)) return -1;
394 switch (flags & XN_FLAG_SEP_MASK)
396 case XN_FLAG_SEP_MULTILINE:
403 case XN_FLAG_SEP_COMMA_PLUS:
411 case XN_FLAG_SEP_CPLUS_SPC:
419 case XN_FLAG_SEP_SPLUS_SPC:
431 if(flags & XN_FLAG_SPC_EQ) {
439 fn_opt = flags & XN_FLAG_FN_MASK;
441 cnt = X509_NAME_entry_count(n);
442 for(i = 0; i < cnt; i++) {
443 if(flags & XN_FLAG_DN_REV)
444 ent = X509_NAME_get_entry(n, cnt - i - 1);
445 else ent = X509_NAME_get_entry(n, i);
447 if(prev == ent->set) {
448 if(!io_ch(arg, sep_mv, sep_mv_len)) return -1;
449 outlen += sep_mv_len;
451 if(!io_ch(arg, sep_dn, sep_dn_len)) return -1;
452 outlen += sep_dn_len;
453 if(!do_indent(io_ch, arg, indent)) return -1;
458 fn = X509_NAME_ENTRY_get_object(ent);
459 val = X509_NAME_ENTRY_get_data(ent);
460 fn_nid = OBJ_obj2nid(fn);
461 if(fn_opt != XN_FLAG_FN_NONE) {
463 if((fn_opt == XN_FLAG_FN_OID) || (fn_nid==NID_undef) ) {
464 OBJ_obj2txt(objtmp, 80, fn, 1);
465 fld_len = 0; /* XXX: what should this be? */
468 if(fn_opt == XN_FLAG_FN_SN) {
469 fld_len = FN_WIDTH_SN;
470 objbuf = OBJ_nid2sn(fn_nid);
471 } else if(fn_opt == XN_FLAG_FN_LN) {
472 fld_len = FN_WIDTH_LN;
473 objbuf = OBJ_nid2ln(fn_nid);
475 fld_len = 0; /* XXX: what should this be? */
479 objlen = strlen(objbuf);
480 if(!io_ch(arg, objbuf, objlen)) return -1;
481 if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
482 if (!do_indent(io_ch, arg, fld_len - objlen)) return -1;
483 outlen += fld_len - objlen;
485 if(!io_ch(arg, sep_eq, sep_eq_len)) return -1;
486 outlen += objlen + sep_eq_len;
488 /* If the field name is unknown then fix up the DER dump
489 * flag. We might want to limit this further so it will
490 * DER dump on anything other than a few 'standard' fields.
492 if((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
493 orflags = ASN1_STRFLGS_DUMP_ALL;
496 len = do_print_ex(io_ch, arg, flags | orflags, val);
497 if(len < 0) return -1;
503 /* Wrappers round the main functions */
505 int X509_NAME_print_ex(BIO *out, X509_NAME *nm, int indent, unsigned long flags)
507 if(flags == XN_FLAG_COMPAT)
508 return X509_NAME_print(out, nm, indent);
509 return do_name_ex(send_bio_chars, out, nm, indent, flags);
513 int X509_NAME_print_ex_fp(FILE *fp, X509_NAME *nm, int indent, unsigned long flags)
515 if(flags == XN_FLAG_COMPAT)
519 btmp = BIO_new_fp(fp, BIO_NOCLOSE);
521 ret = X509_NAME_print(btmp, nm, indent);
525 return do_name_ex(send_fp_chars, fp, nm, indent, flags);
528 int ASN1_STRING_print_ex(BIO *out, ASN1_STRING *str, unsigned long flags)
530 return do_print_ex(send_bio_chars, out, flags, str);
534 int ASN1_STRING_print_ex_fp(FILE *fp, ASN1_STRING *str, unsigned long flags)
536 return do_print_ex(send_fp_chars, fp, flags, str);
539 /* Utility function: convert any string type to UTF8, returns number of bytes
540 * in output string or a negative error code
543 int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in)
545 ASN1_STRING stmp, *str = &stmp;
546 int mbflag, type, ret;
549 if((type < 0) || (type > 30)) return -1;
550 mbflag = tag2nbyte[type];
551 if(mbflag == -1) return -1;
552 mbflag |= MBSTRING_FLAG;
554 ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag, B_ASN1_UTF8STRING);
555 if(ret < 0) return ret;