xref: /onnv-gate/usr/src/common/openssl/crypto/asn1/a_strex.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* a_strex.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate  * project 2000.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    licensing@OpenSSL.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <string.h>
61*2139Sjp161948 #include "cryptlib.h"
620Sstevel@tonic-gate #include <openssl/crypto.h>
630Sstevel@tonic-gate #include <openssl/x509.h>
640Sstevel@tonic-gate #include <openssl/asn1.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include "charmap.h"
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /* ASN1_STRING_print_ex() and X509_NAME_print_ex().
690Sstevel@tonic-gate  * Enhanced string and name printing routines handling
700Sstevel@tonic-gate  * multibyte characters, RFC2253 and a host of other
710Sstevel@tonic-gate  * options.
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #define CHARTYPE_BS_ESC		(ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /* Three IO functions for sending data to memory, a BIO and
790Sstevel@tonic-gate  * and a FILE pointer.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate #if 0				/* never used */
820Sstevel@tonic-gate static int send_mem_chars(void *arg, const void *buf, int len)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	unsigned char **out = arg;
850Sstevel@tonic-gate 	if(!out) return 1;
860Sstevel@tonic-gate 	memcpy(*out, buf, len);
870Sstevel@tonic-gate 	*out += len;
880Sstevel@tonic-gate 	return 1;
890Sstevel@tonic-gate }
900Sstevel@tonic-gate #endif
910Sstevel@tonic-gate 
send_bio_chars(void * arg,const void * buf,int len)920Sstevel@tonic-gate static int send_bio_chars(void *arg, const void *buf, int len)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	if(!arg) return 1;
950Sstevel@tonic-gate 	if(BIO_write(arg, buf, len) != len) return 0;
960Sstevel@tonic-gate 	return 1;
970Sstevel@tonic-gate }
980Sstevel@tonic-gate 
send_fp_chars(void * arg,const void * buf,int len)990Sstevel@tonic-gate static int send_fp_chars(void *arg, const void *buf, int len)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate 	if(!arg) return 1;
1020Sstevel@tonic-gate 	if(fwrite(buf, 1, len, arg) != (unsigned int)len) return 0;
1030Sstevel@tonic-gate 	return 1;
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate typedef int char_io(void *arg, const void *buf, int len);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate /* This function handles display of
1090Sstevel@tonic-gate  * strings, one character at a time.
1100Sstevel@tonic-gate  * It is passed an unsigned long for each
1110Sstevel@tonic-gate  * character because it could come from 2 or even
1120Sstevel@tonic-gate  * 4 byte forms.
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate 
do_esc_char(unsigned long c,unsigned char flags,char * do_quotes,char_io * io_ch,void * arg)1150Sstevel@tonic-gate static int do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, char_io *io_ch, void *arg)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	unsigned char chflgs, chtmp;
1180Sstevel@tonic-gate 	char tmphex[HEX_SIZE(long)+3];
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if(c > 0xffffffffL)
1210Sstevel@tonic-gate 		return -1;
1220Sstevel@tonic-gate 	if(c > 0xffff) {
1230Sstevel@tonic-gate 		BIO_snprintf(tmphex, sizeof tmphex, "\\W%08lX", c);
1240Sstevel@tonic-gate 		if(!io_ch(arg, tmphex, 10)) return -1;
1250Sstevel@tonic-gate 		return 10;
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	if(c > 0xff) {
1280Sstevel@tonic-gate 		BIO_snprintf(tmphex, sizeof tmphex, "\\U%04lX", c);
1290Sstevel@tonic-gate 		if(!io_ch(arg, tmphex, 6)) return -1;
1300Sstevel@tonic-gate 		return 6;
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 	chtmp = (unsigned char)c;
1330Sstevel@tonic-gate 	if(chtmp > 0x7f) chflgs = flags & ASN1_STRFLGS_ESC_MSB;
1340Sstevel@tonic-gate 	else chflgs = char_type[chtmp] & flags;
1350Sstevel@tonic-gate 	if(chflgs & CHARTYPE_BS_ESC) {
1360Sstevel@tonic-gate 		/* If we don't escape with quotes, signal we need quotes */
1370Sstevel@tonic-gate 		if(chflgs & ASN1_STRFLGS_ESC_QUOTE) {
1380Sstevel@tonic-gate 			if(do_quotes) *do_quotes = 1;
1390Sstevel@tonic-gate 			if(!io_ch(arg, &chtmp, 1)) return -1;
1400Sstevel@tonic-gate 			return 1;
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate 		if(!io_ch(arg, "\\", 1)) return -1;
1430Sstevel@tonic-gate 		if(!io_ch(arg, &chtmp, 1)) return -1;
1440Sstevel@tonic-gate 		return 2;
1450Sstevel@tonic-gate 	}
1460Sstevel@tonic-gate 	if(chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) {
1470Sstevel@tonic-gate 		BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
1480Sstevel@tonic-gate 		if(!io_ch(arg, tmphex, 3)) return -1;
1490Sstevel@tonic-gate 		return 3;
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 	if(!io_ch(arg, &chtmp, 1)) return -1;
1520Sstevel@tonic-gate 	return 1;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate #define BUF_TYPE_WIDTH_MASK	0x7
1560Sstevel@tonic-gate #define BUF_TYPE_CONVUTF8	0x8
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /* This function sends each character in a buffer to
1590Sstevel@tonic-gate  * do_esc_char(). It interprets the content formats
1600Sstevel@tonic-gate  * and converts to or from UTF8 as appropriate.
1610Sstevel@tonic-gate  */
1620Sstevel@tonic-gate 
do_buf(unsigned char * buf,int buflen,int type,unsigned char flags,char * quotes,char_io * io_ch,void * arg)1630Sstevel@tonic-gate static int do_buf(unsigned char *buf, int buflen,
1640Sstevel@tonic-gate 			int type, unsigned char flags, char *quotes, char_io *io_ch, void *arg)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	int i, outlen, len;
1670Sstevel@tonic-gate 	unsigned char orflags, *p, *q;
1680Sstevel@tonic-gate 	unsigned long c;
1690Sstevel@tonic-gate 	p = buf;
1700Sstevel@tonic-gate 	q = buf + buflen;
1710Sstevel@tonic-gate 	outlen = 0;
1720Sstevel@tonic-gate 	while(p != q) {
1730Sstevel@tonic-gate 		if(p == buf) orflags = CHARTYPE_FIRST_ESC_2253;
1740Sstevel@tonic-gate 		else orflags = 0;
1750Sstevel@tonic-gate 		switch(type & BUF_TYPE_WIDTH_MASK) {
1760Sstevel@tonic-gate 			case 4:
1770Sstevel@tonic-gate 			c = ((unsigned long)*p++) << 24;
1780Sstevel@tonic-gate 			c |= ((unsigned long)*p++) << 16;
1790Sstevel@tonic-gate 			c |= ((unsigned long)*p++) << 8;
1800Sstevel@tonic-gate 			c |= *p++;
1810Sstevel@tonic-gate 			break;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 			case 2:
1840Sstevel@tonic-gate 			c = ((unsigned long)*p++) << 8;
1850Sstevel@tonic-gate 			c |= *p++;
1860Sstevel@tonic-gate 			break;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 			case 1:
1890Sstevel@tonic-gate 			c = *p++;
1900Sstevel@tonic-gate 			break;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 			case 0:
1930Sstevel@tonic-gate 			i = UTF8_getc(p, buflen, &c);
1940Sstevel@tonic-gate 			if(i < 0) return -1;	/* Invalid UTF8String */
1950Sstevel@tonic-gate 			p += i;
1960Sstevel@tonic-gate 			break;
1970Sstevel@tonic-gate 		}
1980Sstevel@tonic-gate 		if (p == q) orflags = CHARTYPE_LAST_ESC_2253;
1990Sstevel@tonic-gate 		if(type & BUF_TYPE_CONVUTF8) {
2000Sstevel@tonic-gate 			unsigned char utfbuf[6];
2010Sstevel@tonic-gate 			int utflen;
2020Sstevel@tonic-gate 			utflen = UTF8_putc(utfbuf, sizeof utfbuf, c);
2030Sstevel@tonic-gate 			for(i = 0; i < utflen; i++) {
2040Sstevel@tonic-gate 				/* We don't need to worry about setting orflags correctly
2050Sstevel@tonic-gate 				 * because if utflen==1 its value will be correct anyway
2060Sstevel@tonic-gate 				 * otherwise each character will be > 0x7f and so the
2070Sstevel@tonic-gate 				 * character will never be escaped on first and last.
2080Sstevel@tonic-gate 				 */
2090Sstevel@tonic-gate 				len = do_esc_char(utfbuf[i], (unsigned char)(flags | orflags), quotes, io_ch, arg);
2100Sstevel@tonic-gate 				if(len < 0) return -1;
2110Sstevel@tonic-gate 				outlen += len;
2120Sstevel@tonic-gate 			}
2130Sstevel@tonic-gate 		} else {
2140Sstevel@tonic-gate 			len = do_esc_char(c, (unsigned char)(flags | orflags), quotes, io_ch, arg);
2150Sstevel@tonic-gate 			if(len < 0) return -1;
2160Sstevel@tonic-gate 			outlen += len;
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 	return outlen;
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate /* This function hex dumps a buffer of characters */
2230Sstevel@tonic-gate 
do_hex_dump(char_io * io_ch,void * arg,unsigned char * buf,int buflen)2240Sstevel@tonic-gate static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, int buflen)
2250Sstevel@tonic-gate {
226*2139Sjp161948 	static const char hexdig[] = "0123456789ABCDEF";
2270Sstevel@tonic-gate 	unsigned char *p, *q;
2280Sstevel@tonic-gate 	char hextmp[2];
2290Sstevel@tonic-gate 	if(arg) {
2300Sstevel@tonic-gate 		p = buf;
2310Sstevel@tonic-gate 		q = buf + buflen;
2320Sstevel@tonic-gate 		while(p != q) {
2330Sstevel@tonic-gate 			hextmp[0] = hexdig[*p >> 4];
2340Sstevel@tonic-gate 			hextmp[1] = hexdig[*p & 0xf];
2350Sstevel@tonic-gate 			if(!io_ch(arg, hextmp, 2)) return -1;
2360Sstevel@tonic-gate 			p++;
2370Sstevel@tonic-gate 		}
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 	return buflen << 1;
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate /* "dump" a string. This is done when the type is unknown,
2430Sstevel@tonic-gate  * or the flags request it. We can either dump the content
2440Sstevel@tonic-gate  * octets or the entire DER encoding. This uses the RFC2253
2450Sstevel@tonic-gate  * #01234 format.
2460Sstevel@tonic-gate  */
2470Sstevel@tonic-gate 
do_dump(unsigned long lflags,char_io * io_ch,void * arg,ASN1_STRING * str)2480Sstevel@tonic-gate static int do_dump(unsigned long lflags, char_io *io_ch, void *arg, ASN1_STRING *str)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate 	/* Placing the ASN1_STRING in a temp ASN1_TYPE allows
2510Sstevel@tonic-gate 	 * the DER encoding to readily obtained
2520Sstevel@tonic-gate 	 */
2530Sstevel@tonic-gate 	ASN1_TYPE t;
2540Sstevel@tonic-gate 	unsigned char *der_buf, *p;
2550Sstevel@tonic-gate 	int outlen, der_len;
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	if(!io_ch(arg, "#", 1)) return -1;
2580Sstevel@tonic-gate 	/* If we don't dump DER encoding just dump content octets */
2590Sstevel@tonic-gate 	if(!(lflags & ASN1_STRFLGS_DUMP_DER)) {
2600Sstevel@tonic-gate 		outlen = do_hex_dump(io_ch, arg, str->data, str->length);
2610Sstevel@tonic-gate 		if(outlen < 0) return -1;
2620Sstevel@tonic-gate 		return outlen + 1;
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 	t.type = str->type;
2650Sstevel@tonic-gate 	t.value.ptr = (char *)str;
2660Sstevel@tonic-gate 	der_len = i2d_ASN1_TYPE(&t, NULL);
2670Sstevel@tonic-gate 	der_buf = OPENSSL_malloc(der_len);
2680Sstevel@tonic-gate 	if(!der_buf) return -1;
2690Sstevel@tonic-gate 	p = der_buf;
2700Sstevel@tonic-gate 	i2d_ASN1_TYPE(&t, &p);
2710Sstevel@tonic-gate 	outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
2720Sstevel@tonic-gate 	OPENSSL_free(der_buf);
2730Sstevel@tonic-gate 	if(outlen < 0) return -1;
2740Sstevel@tonic-gate 	return outlen + 1;
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate /* Lookup table to convert tags to character widths,
2780Sstevel@tonic-gate  * 0 = UTF8 encoded, -1 is used for non string types
2790Sstevel@tonic-gate  * otherwise it is the number of bytes per character
2800Sstevel@tonic-gate  */
2810Sstevel@tonic-gate 
282*2139Sjp161948 static const signed char tag2nbyte[] = {
2830Sstevel@tonic-gate 	-1, -1, -1, -1, -1,	/* 0-4 */
2840Sstevel@tonic-gate 	-1, -1, -1, -1, -1,	/* 5-9 */
2850Sstevel@tonic-gate 	-1, -1, 0, -1,		/* 10-13 */
2860Sstevel@tonic-gate 	-1, -1, -1, -1,		/* 15-17 */
2870Sstevel@tonic-gate 	-1, 1, 1,		/* 18-20 */
2880Sstevel@tonic-gate 	-1, 1, 1, 1,		/* 21-24 */
2890Sstevel@tonic-gate 	-1, 1, -1,		/* 25-27 */
2900Sstevel@tonic-gate 	4, -1, 2		/* 28-30 */
2910Sstevel@tonic-gate };
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
2940Sstevel@tonic-gate 		  ASN1_STRFLGS_ESC_QUOTE | \
2950Sstevel@tonic-gate 		  ASN1_STRFLGS_ESC_CTRL | \
2960Sstevel@tonic-gate 		  ASN1_STRFLGS_ESC_MSB)
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate /* This is the main function, print out an
2990Sstevel@tonic-gate  * ASN1_STRING taking note of various escape
3000Sstevel@tonic-gate  * and display options. Returns number of
3010Sstevel@tonic-gate  * characters written or -1 if an error
3020Sstevel@tonic-gate  * occurred.
3030Sstevel@tonic-gate  */
3040Sstevel@tonic-gate 
do_print_ex(char_io * io_ch,void * arg,unsigned long lflags,ASN1_STRING * str)3050Sstevel@tonic-gate static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, ASN1_STRING *str)
3060Sstevel@tonic-gate {
3070Sstevel@tonic-gate 	int outlen, len;
3080Sstevel@tonic-gate 	int type;
3090Sstevel@tonic-gate 	char quotes;
3100Sstevel@tonic-gate 	unsigned char flags;
3110Sstevel@tonic-gate 	quotes = 0;
3120Sstevel@tonic-gate 	/* Keep a copy of escape flags */
3130Sstevel@tonic-gate 	flags = (unsigned char)(lflags & ESC_FLAGS);
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	type = str->type;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	outlen = 0;
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	if(lflags & ASN1_STRFLGS_SHOW_TYPE) {
3210Sstevel@tonic-gate 		const char *tagname;
3220Sstevel@tonic-gate 		tagname = ASN1_tag2str(type);
3230Sstevel@tonic-gate 		outlen += strlen(tagname);
3240Sstevel@tonic-gate 		if(!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1)) return -1;
3250Sstevel@tonic-gate 		outlen++;
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate 	/* Decide what to do with type, either dump content or display it */
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/* Dump everything */
3310Sstevel@tonic-gate 	if(lflags & ASN1_STRFLGS_DUMP_ALL) type = -1;
3320Sstevel@tonic-gate 	/* Ignore the string type */
3330Sstevel@tonic-gate 	else if(lflags & ASN1_STRFLGS_IGNORE_TYPE) type = 1;
3340Sstevel@tonic-gate 	else {
3350Sstevel@tonic-gate 		/* Else determine width based on type */
3360Sstevel@tonic-gate 		if((type > 0) && (type < 31)) type = tag2nbyte[type];
3370Sstevel@tonic-gate 		else type = -1;
3380Sstevel@tonic-gate 		if((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN)) type = 1;
3390Sstevel@tonic-gate 	}
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	if(type == -1) {
3420Sstevel@tonic-gate 		len = do_dump(lflags, io_ch, arg, str);
3430Sstevel@tonic-gate 		if(len < 0) return -1;
3440Sstevel@tonic-gate 		outlen += len;
3450Sstevel@tonic-gate 		return outlen;
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	if(lflags & ASN1_STRFLGS_UTF8_CONVERT) {
3490Sstevel@tonic-gate 		/* Note: if string is UTF8 and we want
3500Sstevel@tonic-gate 		 * to convert to UTF8 then we just interpret
3510Sstevel@tonic-gate 		 * it as 1 byte per character to avoid converting
3520Sstevel@tonic-gate 		 * twice.
3530Sstevel@tonic-gate 		 */
3540Sstevel@tonic-gate 		if(!type) type = 1;
3550Sstevel@tonic-gate 		else type |= BUF_TYPE_CONVUTF8;
3560Sstevel@tonic-gate 	}
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	len = do_buf(str->data, str->length, type, flags, &quotes, io_ch, NULL);
3590Sstevel@tonic-gate 	if(outlen < 0) return -1;
3600Sstevel@tonic-gate 	outlen += len;
3610Sstevel@tonic-gate 	if(quotes) outlen += 2;
3620Sstevel@tonic-gate 	if(!arg) return outlen;
3630Sstevel@tonic-gate 	if(quotes && !io_ch(arg, "\"", 1)) return -1;
3640Sstevel@tonic-gate 	do_buf(str->data, str->length, type, flags, NULL, io_ch, arg);
3650Sstevel@tonic-gate 	if(quotes && !io_ch(arg, "\"", 1)) return -1;
3660Sstevel@tonic-gate 	return outlen;
3670Sstevel@tonic-gate }
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate /* Used for line indenting: print 'indent' spaces */
3700Sstevel@tonic-gate 
do_indent(char_io * io_ch,void * arg,int indent)3710Sstevel@tonic-gate static int do_indent(char_io *io_ch, void *arg, int indent)
3720Sstevel@tonic-gate {
3730Sstevel@tonic-gate 	int i;
3740Sstevel@tonic-gate 	for(i = 0; i < indent; i++)
3750Sstevel@tonic-gate 			if(!io_ch(arg, " ", 1)) return 0;
3760Sstevel@tonic-gate 	return 1;
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate #define FN_WIDTH_LN	25
3800Sstevel@tonic-gate #define FN_WIDTH_SN	10
3810Sstevel@tonic-gate 
do_name_ex(char_io * io_ch,void * arg,X509_NAME * n,int indent,unsigned long flags)3820Sstevel@tonic-gate static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n,
3830Sstevel@tonic-gate 				int indent, unsigned long flags)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	int i, prev = -1, orflags, cnt;
3860Sstevel@tonic-gate 	int fn_opt, fn_nid;
3870Sstevel@tonic-gate 	ASN1_OBJECT *fn;
3880Sstevel@tonic-gate 	ASN1_STRING *val;
3890Sstevel@tonic-gate 	X509_NAME_ENTRY *ent;
3900Sstevel@tonic-gate 	char objtmp[80];
3910Sstevel@tonic-gate 	const char *objbuf;
3920Sstevel@tonic-gate 	int outlen, len;
3930Sstevel@tonic-gate 	char *sep_dn, *sep_mv, *sep_eq;
3940Sstevel@tonic-gate 	int sep_dn_len, sep_mv_len, sep_eq_len;
3950Sstevel@tonic-gate 	if(indent < 0) indent = 0;
3960Sstevel@tonic-gate 	outlen = indent;
3970Sstevel@tonic-gate 	if(!do_indent(io_ch, arg, indent)) return -1;
3980Sstevel@tonic-gate 	switch (flags & XN_FLAG_SEP_MASK)
3990Sstevel@tonic-gate 	{
4000Sstevel@tonic-gate 		case XN_FLAG_SEP_MULTILINE:
4010Sstevel@tonic-gate 		sep_dn = "\n";
4020Sstevel@tonic-gate 		sep_dn_len = 1;
4030Sstevel@tonic-gate 		sep_mv = " + ";
4040Sstevel@tonic-gate 		sep_mv_len = 3;
4050Sstevel@tonic-gate 		break;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 		case XN_FLAG_SEP_COMMA_PLUS:
4080Sstevel@tonic-gate 		sep_dn = ",";
4090Sstevel@tonic-gate 		sep_dn_len = 1;
4100Sstevel@tonic-gate 		sep_mv = "+";
4110Sstevel@tonic-gate 		sep_mv_len = 1;
4120Sstevel@tonic-gate 		indent = 0;
4130Sstevel@tonic-gate 		break;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 		case XN_FLAG_SEP_CPLUS_SPC:
4160Sstevel@tonic-gate 		sep_dn = ", ";
4170Sstevel@tonic-gate 		sep_dn_len = 2;
4180Sstevel@tonic-gate 		sep_mv = " + ";
4190Sstevel@tonic-gate 		sep_mv_len = 3;
4200Sstevel@tonic-gate 		indent = 0;
4210Sstevel@tonic-gate 		break;
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 		case XN_FLAG_SEP_SPLUS_SPC:
4240Sstevel@tonic-gate 		sep_dn = "; ";
4250Sstevel@tonic-gate 		sep_dn_len = 2;
4260Sstevel@tonic-gate 		sep_mv = " + ";
4270Sstevel@tonic-gate 		sep_mv_len = 3;
4280Sstevel@tonic-gate 		indent = 0;
4290Sstevel@tonic-gate 		break;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		default:
4320Sstevel@tonic-gate 		return -1;
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	if(flags & XN_FLAG_SPC_EQ) {
4360Sstevel@tonic-gate 		sep_eq = " = ";
4370Sstevel@tonic-gate 		sep_eq_len = 3;
4380Sstevel@tonic-gate 	} else {
4390Sstevel@tonic-gate 		sep_eq = "=";
4400Sstevel@tonic-gate 		sep_eq_len = 1;
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	fn_opt = flags & XN_FLAG_FN_MASK;
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	cnt = X509_NAME_entry_count(n);
4460Sstevel@tonic-gate 	for(i = 0; i < cnt; i++) {
4470Sstevel@tonic-gate 		if(flags & XN_FLAG_DN_REV)
4480Sstevel@tonic-gate 				ent = X509_NAME_get_entry(n, cnt - i - 1);
4490Sstevel@tonic-gate 		else ent = X509_NAME_get_entry(n, i);
4500Sstevel@tonic-gate 		if(prev != -1) {
4510Sstevel@tonic-gate 			if(prev == ent->set) {
4520Sstevel@tonic-gate 				if(!io_ch(arg, sep_mv, sep_mv_len)) return -1;
4530Sstevel@tonic-gate 				outlen += sep_mv_len;
4540Sstevel@tonic-gate 			} else {
4550Sstevel@tonic-gate 				if(!io_ch(arg, sep_dn, sep_dn_len)) return -1;
4560Sstevel@tonic-gate 				outlen += sep_dn_len;
4570Sstevel@tonic-gate 				if(!do_indent(io_ch, arg, indent)) return -1;
4580Sstevel@tonic-gate 				outlen += indent;
4590Sstevel@tonic-gate 			}
4600Sstevel@tonic-gate 		}
4610Sstevel@tonic-gate 		prev = ent->set;
4620Sstevel@tonic-gate 		fn = X509_NAME_ENTRY_get_object(ent);
4630Sstevel@tonic-gate 		val = X509_NAME_ENTRY_get_data(ent);
4640Sstevel@tonic-gate 		fn_nid = OBJ_obj2nid(fn);
4650Sstevel@tonic-gate 		if(fn_opt != XN_FLAG_FN_NONE) {
4660Sstevel@tonic-gate 			int objlen, fld_len;
4670Sstevel@tonic-gate 			if((fn_opt == XN_FLAG_FN_OID) || (fn_nid==NID_undef) ) {
4680Sstevel@tonic-gate 				OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1);
4690Sstevel@tonic-gate 				fld_len = 0; /* XXX: what should this be? */
4700Sstevel@tonic-gate 				objbuf = objtmp;
4710Sstevel@tonic-gate 			} else {
4720Sstevel@tonic-gate 				if(fn_opt == XN_FLAG_FN_SN) {
4730Sstevel@tonic-gate 					fld_len = FN_WIDTH_SN;
4740Sstevel@tonic-gate 					objbuf = OBJ_nid2sn(fn_nid);
4750Sstevel@tonic-gate 				} else if(fn_opt == XN_FLAG_FN_LN) {
4760Sstevel@tonic-gate 					fld_len = FN_WIDTH_LN;
4770Sstevel@tonic-gate 					objbuf = OBJ_nid2ln(fn_nid);
4780Sstevel@tonic-gate 				} else {
4790Sstevel@tonic-gate 					fld_len = 0; /* XXX: what should this be? */
4800Sstevel@tonic-gate 					objbuf = "";
4810Sstevel@tonic-gate 				}
4820Sstevel@tonic-gate 			}
4830Sstevel@tonic-gate 			objlen = strlen(objbuf);
4840Sstevel@tonic-gate 			if(!io_ch(arg, objbuf, objlen)) return -1;
4850Sstevel@tonic-gate 			if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
4860Sstevel@tonic-gate 				if (!do_indent(io_ch, arg, fld_len - objlen)) return -1;
4870Sstevel@tonic-gate 				outlen += fld_len - objlen;
4880Sstevel@tonic-gate 			}
4890Sstevel@tonic-gate 			if(!io_ch(arg, sep_eq, sep_eq_len)) return -1;
4900Sstevel@tonic-gate 			outlen += objlen + sep_eq_len;
4910Sstevel@tonic-gate 		}
4920Sstevel@tonic-gate 		/* If the field name is unknown then fix up the DER dump
4930Sstevel@tonic-gate 		 * flag. We might want to limit this further so it will
4940Sstevel@tonic-gate  		 * DER dump on anything other than a few 'standard' fields.
4950Sstevel@tonic-gate 		 */
4960Sstevel@tonic-gate 		if((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
4970Sstevel@tonic-gate 					orflags = ASN1_STRFLGS_DUMP_ALL;
4980Sstevel@tonic-gate 		else orflags = 0;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 		len = do_print_ex(io_ch, arg, flags | orflags, val);
5010Sstevel@tonic-gate 		if(len < 0) return -1;
5020Sstevel@tonic-gate 		outlen += len;
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 	return outlen;
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate /* Wrappers round the main functions */
5080Sstevel@tonic-gate 
X509_NAME_print_ex(BIO * out,X509_NAME * nm,int indent,unsigned long flags)5090Sstevel@tonic-gate int X509_NAME_print_ex(BIO *out, X509_NAME *nm, int indent, unsigned long flags)
5100Sstevel@tonic-gate {
5110Sstevel@tonic-gate 	if(flags == XN_FLAG_COMPAT)
5120Sstevel@tonic-gate 		return X509_NAME_print(out, nm, indent);
5130Sstevel@tonic-gate 	return do_name_ex(send_bio_chars, out, nm, indent, flags);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
516*2139Sjp161948 #ifndef OPENSSL_NO_FP_API
X509_NAME_print_ex_fp(FILE * fp,X509_NAME * nm,int indent,unsigned long flags)5170Sstevel@tonic-gate int X509_NAME_print_ex_fp(FILE *fp, X509_NAME *nm, int indent, unsigned long flags)
5180Sstevel@tonic-gate {
5190Sstevel@tonic-gate 	if(flags == XN_FLAG_COMPAT)
5200Sstevel@tonic-gate 		{
5210Sstevel@tonic-gate 		BIO *btmp;
5220Sstevel@tonic-gate 		int ret;
5230Sstevel@tonic-gate 		btmp = BIO_new_fp(fp, BIO_NOCLOSE);
5240Sstevel@tonic-gate 		if(!btmp) return -1;
5250Sstevel@tonic-gate 		ret = X509_NAME_print(btmp, nm, indent);
5260Sstevel@tonic-gate 		BIO_free(btmp);
5270Sstevel@tonic-gate 		return ret;
5280Sstevel@tonic-gate 		}
5290Sstevel@tonic-gate 	return do_name_ex(send_fp_chars, fp, nm, indent, flags);
5300Sstevel@tonic-gate }
531*2139Sjp161948 #endif
5320Sstevel@tonic-gate 
ASN1_STRING_print_ex(BIO * out,ASN1_STRING * str,unsigned long flags)5330Sstevel@tonic-gate int ASN1_STRING_print_ex(BIO *out, ASN1_STRING *str, unsigned long flags)
5340Sstevel@tonic-gate {
5350Sstevel@tonic-gate 	return do_print_ex(send_bio_chars, out, flags, str);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
538*2139Sjp161948 #ifndef OPENSSL_NO_FP_API
ASN1_STRING_print_ex_fp(FILE * fp,ASN1_STRING * str,unsigned long flags)5390Sstevel@tonic-gate int ASN1_STRING_print_ex_fp(FILE *fp, ASN1_STRING *str, unsigned long flags)
5400Sstevel@tonic-gate {
5410Sstevel@tonic-gate 	return do_print_ex(send_fp_chars, fp, flags, str);
5420Sstevel@tonic-gate }
543*2139Sjp161948 #endif
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate /* Utility function: convert any string type to UTF8, returns number of bytes
5460Sstevel@tonic-gate  * in output string or a negative error code
5470Sstevel@tonic-gate  */
5480Sstevel@tonic-gate 
ASN1_STRING_to_UTF8(unsigned char ** out,ASN1_STRING * in)5490Sstevel@tonic-gate int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in)
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	ASN1_STRING stmp, *str = &stmp;
5520Sstevel@tonic-gate 	int mbflag, type, ret;
5530Sstevel@tonic-gate 	if(!in) return -1;
5540Sstevel@tonic-gate 	type = in->type;
5550Sstevel@tonic-gate 	if((type < 0) || (type > 30)) return -1;
5560Sstevel@tonic-gate 	mbflag = tag2nbyte[type];
5570Sstevel@tonic-gate 	if(mbflag == -1) return -1;
5580Sstevel@tonic-gate 	mbflag |= MBSTRING_FLAG;
5590Sstevel@tonic-gate 	stmp.data = NULL;
5600Sstevel@tonic-gate 	ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag, B_ASN1_UTF8STRING);
5610Sstevel@tonic-gate 	if(ret < 0) return ret;
5620Sstevel@tonic-gate 	*out = stmp.data;
5630Sstevel@tonic-gate 	return stmp.length;
5640Sstevel@tonic-gate }
565