xref: /onnv-gate/usr/src/common/openssl/crypto/asn1/a_strex.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* a_strex.c */
2*0Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3*0Sstevel@tonic-gate  * project 2000.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate /* ====================================================================
6*0Sstevel@tonic-gate  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*0Sstevel@tonic-gate  * are met:
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
13*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
17*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
18*0Sstevel@tonic-gate  *    distribution.
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
21*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
22*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
23*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
27*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
28*0Sstevel@tonic-gate  *    licensing@OpenSSL.org.
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
31*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
32*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
35*0Sstevel@tonic-gate  *    acknowledgment:
36*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
37*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
51*0Sstevel@tonic-gate  * ====================================================================
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
54*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
55*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
56*0Sstevel@tonic-gate  *
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include <string.h>
61*0Sstevel@tonic-gate #include <openssl/crypto.h>
62*0Sstevel@tonic-gate #include <openssl/x509.h>
63*0Sstevel@tonic-gate #include <openssl/asn1.h>
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate #include "charmap.h"
66*0Sstevel@tonic-gate #include "cryptlib.h"
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate /* ASN1_STRING_print_ex() and X509_NAME_print_ex().
69*0Sstevel@tonic-gate  * Enhanced string and name printing routines handling
70*0Sstevel@tonic-gate  * multibyte characters, RFC2253 and a host of other
71*0Sstevel@tonic-gate  * options.
72*0Sstevel@tonic-gate  */
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate #define CHARTYPE_BS_ESC		(ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /* Three IO functions for sending data to memory, a BIO and
79*0Sstevel@tonic-gate  * and a FILE pointer.
80*0Sstevel@tonic-gate  */
81*0Sstevel@tonic-gate #if 0				/* never used */
82*0Sstevel@tonic-gate static int send_mem_chars(void *arg, const void *buf, int len)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	unsigned char **out = arg;
85*0Sstevel@tonic-gate 	if(!out) return 1;
86*0Sstevel@tonic-gate 	memcpy(*out, buf, len);
87*0Sstevel@tonic-gate 	*out += len;
88*0Sstevel@tonic-gate 	return 1;
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate #endif
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate static int send_bio_chars(void *arg, const void *buf, int len)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	if(!arg) return 1;
95*0Sstevel@tonic-gate 	if(BIO_write(arg, buf, len) != len) return 0;
96*0Sstevel@tonic-gate 	return 1;
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate static int send_fp_chars(void *arg, const void *buf, int len)
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate 	if(!arg) return 1;
102*0Sstevel@tonic-gate 	if(fwrite(buf, 1, len, arg) != (unsigned int)len) return 0;
103*0Sstevel@tonic-gate 	return 1;
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate typedef int char_io(void *arg, const void *buf, int len);
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate /* This function handles display of
109*0Sstevel@tonic-gate  * strings, one character at a time.
110*0Sstevel@tonic-gate  * It is passed an unsigned long for each
111*0Sstevel@tonic-gate  * character because it could come from 2 or even
112*0Sstevel@tonic-gate  * 4 byte forms.
113*0Sstevel@tonic-gate  */
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate static int do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, char_io *io_ch, void *arg)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	unsigned char chflgs, chtmp;
118*0Sstevel@tonic-gate 	char tmphex[HEX_SIZE(long)+3];
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	if(c > 0xffffffffL)
121*0Sstevel@tonic-gate 		return -1;
122*0Sstevel@tonic-gate 	if(c > 0xffff) {
123*0Sstevel@tonic-gate 		BIO_snprintf(tmphex, sizeof tmphex, "\\W%08lX", c);
124*0Sstevel@tonic-gate 		if(!io_ch(arg, tmphex, 10)) return -1;
125*0Sstevel@tonic-gate 		return 10;
126*0Sstevel@tonic-gate 	}
127*0Sstevel@tonic-gate 	if(c > 0xff) {
128*0Sstevel@tonic-gate 		BIO_snprintf(tmphex, sizeof tmphex, "\\U%04lX", c);
129*0Sstevel@tonic-gate 		if(!io_ch(arg, tmphex, 6)) return -1;
130*0Sstevel@tonic-gate 		return 6;
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 	chtmp = (unsigned char)c;
133*0Sstevel@tonic-gate 	if(chtmp > 0x7f) chflgs = flags & ASN1_STRFLGS_ESC_MSB;
134*0Sstevel@tonic-gate 	else chflgs = char_type[chtmp] & flags;
135*0Sstevel@tonic-gate 	if(chflgs & CHARTYPE_BS_ESC) {
136*0Sstevel@tonic-gate 		/* If we don't escape with quotes, signal we need quotes */
137*0Sstevel@tonic-gate 		if(chflgs & ASN1_STRFLGS_ESC_QUOTE) {
138*0Sstevel@tonic-gate 			if(do_quotes) *do_quotes = 1;
139*0Sstevel@tonic-gate 			if(!io_ch(arg, &chtmp, 1)) return -1;
140*0Sstevel@tonic-gate 			return 1;
141*0Sstevel@tonic-gate 		}
142*0Sstevel@tonic-gate 		if(!io_ch(arg, "\\", 1)) return -1;
143*0Sstevel@tonic-gate 		if(!io_ch(arg, &chtmp, 1)) return -1;
144*0Sstevel@tonic-gate 		return 2;
145*0Sstevel@tonic-gate 	}
146*0Sstevel@tonic-gate 	if(chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) {
147*0Sstevel@tonic-gate 		BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
148*0Sstevel@tonic-gate 		if(!io_ch(arg, tmphex, 3)) return -1;
149*0Sstevel@tonic-gate 		return 3;
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate 	if(!io_ch(arg, &chtmp, 1)) return -1;
152*0Sstevel@tonic-gate 	return 1;
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate #define BUF_TYPE_WIDTH_MASK	0x7
156*0Sstevel@tonic-gate #define BUF_TYPE_CONVUTF8	0x8
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate /* This function sends each character in a buffer to
159*0Sstevel@tonic-gate  * do_esc_char(). It interprets the content formats
160*0Sstevel@tonic-gate  * and converts to or from UTF8 as appropriate.
161*0Sstevel@tonic-gate  */
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate static int do_buf(unsigned char *buf, int buflen,
164*0Sstevel@tonic-gate 			int type, unsigned char flags, char *quotes, char_io *io_ch, void *arg)
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate 	int i, outlen, len;
167*0Sstevel@tonic-gate 	unsigned char orflags, *p, *q;
168*0Sstevel@tonic-gate 	unsigned long c;
169*0Sstevel@tonic-gate 	p = buf;
170*0Sstevel@tonic-gate 	q = buf + buflen;
171*0Sstevel@tonic-gate 	outlen = 0;
172*0Sstevel@tonic-gate 	while(p != q) {
173*0Sstevel@tonic-gate 		if(p == buf) orflags = CHARTYPE_FIRST_ESC_2253;
174*0Sstevel@tonic-gate 		else orflags = 0;
175*0Sstevel@tonic-gate 		switch(type & BUF_TYPE_WIDTH_MASK) {
176*0Sstevel@tonic-gate 			case 4:
177*0Sstevel@tonic-gate 			c = ((unsigned long)*p++) << 24;
178*0Sstevel@tonic-gate 			c |= ((unsigned long)*p++) << 16;
179*0Sstevel@tonic-gate 			c |= ((unsigned long)*p++) << 8;
180*0Sstevel@tonic-gate 			c |= *p++;
181*0Sstevel@tonic-gate 			break;
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 			case 2:
184*0Sstevel@tonic-gate 			c = ((unsigned long)*p++) << 8;
185*0Sstevel@tonic-gate 			c |= *p++;
186*0Sstevel@tonic-gate 			break;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 			case 1:
189*0Sstevel@tonic-gate 			c = *p++;
190*0Sstevel@tonic-gate 			break;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 			case 0:
193*0Sstevel@tonic-gate 			i = UTF8_getc(p, buflen, &c);
194*0Sstevel@tonic-gate 			if(i < 0) return -1;	/* Invalid UTF8String */
195*0Sstevel@tonic-gate 			p += i;
196*0Sstevel@tonic-gate 			break;
197*0Sstevel@tonic-gate 		}
198*0Sstevel@tonic-gate 		if (p == q) orflags = CHARTYPE_LAST_ESC_2253;
199*0Sstevel@tonic-gate 		if(type & BUF_TYPE_CONVUTF8) {
200*0Sstevel@tonic-gate 			unsigned char utfbuf[6];
201*0Sstevel@tonic-gate 			int utflen;
202*0Sstevel@tonic-gate 			utflen = UTF8_putc(utfbuf, sizeof utfbuf, c);
203*0Sstevel@tonic-gate 			for(i = 0; i < utflen; i++) {
204*0Sstevel@tonic-gate 				/* We don't need to worry about setting orflags correctly
205*0Sstevel@tonic-gate 				 * because if utflen==1 its value will be correct anyway
206*0Sstevel@tonic-gate 				 * otherwise each character will be > 0x7f and so the
207*0Sstevel@tonic-gate 				 * character will never be escaped on first and last.
208*0Sstevel@tonic-gate 				 */
209*0Sstevel@tonic-gate 				len = do_esc_char(utfbuf[i], (unsigned char)(flags | orflags), quotes, io_ch, arg);
210*0Sstevel@tonic-gate 				if(len < 0) return -1;
211*0Sstevel@tonic-gate 				outlen += len;
212*0Sstevel@tonic-gate 			}
213*0Sstevel@tonic-gate 		} else {
214*0Sstevel@tonic-gate 			len = do_esc_char(c, (unsigned char)(flags | orflags), quotes, io_ch, arg);
215*0Sstevel@tonic-gate 			if(len < 0) return -1;
216*0Sstevel@tonic-gate 			outlen += len;
217*0Sstevel@tonic-gate 		}
218*0Sstevel@tonic-gate 	}
219*0Sstevel@tonic-gate 	return outlen;
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate /* This function hex dumps a buffer of characters */
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, int buflen)
225*0Sstevel@tonic-gate {
226*0Sstevel@tonic-gate 	const static char hexdig[] = "0123456789ABCDEF";
227*0Sstevel@tonic-gate 	unsigned char *p, *q;
228*0Sstevel@tonic-gate 	char hextmp[2];
229*0Sstevel@tonic-gate 	if(arg) {
230*0Sstevel@tonic-gate 		p = buf;
231*0Sstevel@tonic-gate 		q = buf + buflen;
232*0Sstevel@tonic-gate 		while(p != q) {
233*0Sstevel@tonic-gate 			hextmp[0] = hexdig[*p >> 4];
234*0Sstevel@tonic-gate 			hextmp[1] = hexdig[*p & 0xf];
235*0Sstevel@tonic-gate 			if(!io_ch(arg, hextmp, 2)) return -1;
236*0Sstevel@tonic-gate 			p++;
237*0Sstevel@tonic-gate 		}
238*0Sstevel@tonic-gate 	}
239*0Sstevel@tonic-gate 	return buflen << 1;
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate /* "dump" a string. This is done when the type is unknown,
243*0Sstevel@tonic-gate  * or the flags request it. We can either dump the content
244*0Sstevel@tonic-gate  * octets or the entire DER encoding. This uses the RFC2253
245*0Sstevel@tonic-gate  * #01234 format.
246*0Sstevel@tonic-gate  */
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate static int do_dump(unsigned long lflags, char_io *io_ch, void *arg, ASN1_STRING *str)
249*0Sstevel@tonic-gate {
250*0Sstevel@tonic-gate 	/* Placing the ASN1_STRING in a temp ASN1_TYPE allows
251*0Sstevel@tonic-gate 	 * the DER encoding to readily obtained
252*0Sstevel@tonic-gate 	 */
253*0Sstevel@tonic-gate 	ASN1_TYPE t;
254*0Sstevel@tonic-gate 	unsigned char *der_buf, *p;
255*0Sstevel@tonic-gate 	int outlen, der_len;
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	if(!io_ch(arg, "#", 1)) return -1;
258*0Sstevel@tonic-gate 	/* If we don't dump DER encoding just dump content octets */
259*0Sstevel@tonic-gate 	if(!(lflags & ASN1_STRFLGS_DUMP_DER)) {
260*0Sstevel@tonic-gate 		outlen = do_hex_dump(io_ch, arg, str->data, str->length);
261*0Sstevel@tonic-gate 		if(outlen < 0) return -1;
262*0Sstevel@tonic-gate 		return outlen + 1;
263*0Sstevel@tonic-gate 	}
264*0Sstevel@tonic-gate 	t.type = str->type;
265*0Sstevel@tonic-gate 	t.value.ptr = (char *)str;
266*0Sstevel@tonic-gate 	der_len = i2d_ASN1_TYPE(&t, NULL);
267*0Sstevel@tonic-gate 	der_buf = OPENSSL_malloc(der_len);
268*0Sstevel@tonic-gate 	if(!der_buf) return -1;
269*0Sstevel@tonic-gate 	p = der_buf;
270*0Sstevel@tonic-gate 	i2d_ASN1_TYPE(&t, &p);
271*0Sstevel@tonic-gate 	outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
272*0Sstevel@tonic-gate 	OPENSSL_free(der_buf);
273*0Sstevel@tonic-gate 	if(outlen < 0) return -1;
274*0Sstevel@tonic-gate 	return outlen + 1;
275*0Sstevel@tonic-gate }
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate /* Lookup table to convert tags to character widths,
278*0Sstevel@tonic-gate  * 0 = UTF8 encoded, -1 is used for non string types
279*0Sstevel@tonic-gate  * otherwise it is the number of bytes per character
280*0Sstevel@tonic-gate  */
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate const static signed char tag2nbyte[] = {
283*0Sstevel@tonic-gate 	-1, -1, -1, -1, -1,	/* 0-4 */
284*0Sstevel@tonic-gate 	-1, -1, -1, -1, -1,	/* 5-9 */
285*0Sstevel@tonic-gate 	-1, -1, 0, -1,		/* 10-13 */
286*0Sstevel@tonic-gate 	-1, -1, -1, -1,		/* 15-17 */
287*0Sstevel@tonic-gate 	-1, 1, 1,		/* 18-20 */
288*0Sstevel@tonic-gate 	-1, 1, 1, 1,		/* 21-24 */
289*0Sstevel@tonic-gate 	-1, 1, -1,		/* 25-27 */
290*0Sstevel@tonic-gate 	4, -1, 2		/* 28-30 */
291*0Sstevel@tonic-gate };
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
294*0Sstevel@tonic-gate 		  ASN1_STRFLGS_ESC_QUOTE | \
295*0Sstevel@tonic-gate 		  ASN1_STRFLGS_ESC_CTRL | \
296*0Sstevel@tonic-gate 		  ASN1_STRFLGS_ESC_MSB)
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate /* This is the main function, print out an
299*0Sstevel@tonic-gate  * ASN1_STRING taking note of various escape
300*0Sstevel@tonic-gate  * and display options. Returns number of
301*0Sstevel@tonic-gate  * characters written or -1 if an error
302*0Sstevel@tonic-gate  * occurred.
303*0Sstevel@tonic-gate  */
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, ASN1_STRING *str)
306*0Sstevel@tonic-gate {
307*0Sstevel@tonic-gate 	int outlen, len;
308*0Sstevel@tonic-gate 	int type;
309*0Sstevel@tonic-gate 	char quotes;
310*0Sstevel@tonic-gate 	unsigned char flags;
311*0Sstevel@tonic-gate 	quotes = 0;
312*0Sstevel@tonic-gate 	/* Keep a copy of escape flags */
313*0Sstevel@tonic-gate 	flags = (unsigned char)(lflags & ESC_FLAGS);
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	type = str->type;
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate 	outlen = 0;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate 	if(lflags & ASN1_STRFLGS_SHOW_TYPE) {
321*0Sstevel@tonic-gate 		const char *tagname;
322*0Sstevel@tonic-gate 		tagname = ASN1_tag2str(type);
323*0Sstevel@tonic-gate 		outlen += strlen(tagname);
324*0Sstevel@tonic-gate 		if(!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1)) return -1;
325*0Sstevel@tonic-gate 		outlen++;
326*0Sstevel@tonic-gate 	}
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate 	/* Decide what to do with type, either dump content or display it */
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	/* Dump everything */
331*0Sstevel@tonic-gate 	if(lflags & ASN1_STRFLGS_DUMP_ALL) type = -1;
332*0Sstevel@tonic-gate 	/* Ignore the string type */
333*0Sstevel@tonic-gate 	else if(lflags & ASN1_STRFLGS_IGNORE_TYPE) type = 1;
334*0Sstevel@tonic-gate 	else {
335*0Sstevel@tonic-gate 		/* Else determine width based on type */
336*0Sstevel@tonic-gate 		if((type > 0) && (type < 31)) type = tag2nbyte[type];
337*0Sstevel@tonic-gate 		else type = -1;
338*0Sstevel@tonic-gate 		if((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN)) type = 1;
339*0Sstevel@tonic-gate 	}
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	if(type == -1) {
342*0Sstevel@tonic-gate 		len = do_dump(lflags, io_ch, arg, str);
343*0Sstevel@tonic-gate 		if(len < 0) return -1;
344*0Sstevel@tonic-gate 		outlen += len;
345*0Sstevel@tonic-gate 		return outlen;
346*0Sstevel@tonic-gate 	}
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	if(lflags & ASN1_STRFLGS_UTF8_CONVERT) {
349*0Sstevel@tonic-gate 		/* Note: if string is UTF8 and we want
350*0Sstevel@tonic-gate 		 * to convert to UTF8 then we just interpret
351*0Sstevel@tonic-gate 		 * it as 1 byte per character to avoid converting
352*0Sstevel@tonic-gate 		 * twice.
353*0Sstevel@tonic-gate 		 */
354*0Sstevel@tonic-gate 		if(!type) type = 1;
355*0Sstevel@tonic-gate 		else type |= BUF_TYPE_CONVUTF8;
356*0Sstevel@tonic-gate 	}
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 	len = do_buf(str->data, str->length, type, flags, &quotes, io_ch, NULL);
359*0Sstevel@tonic-gate 	if(outlen < 0) return -1;
360*0Sstevel@tonic-gate 	outlen += len;
361*0Sstevel@tonic-gate 	if(quotes) outlen += 2;
362*0Sstevel@tonic-gate 	if(!arg) return outlen;
363*0Sstevel@tonic-gate 	if(quotes && !io_ch(arg, "\"", 1)) return -1;
364*0Sstevel@tonic-gate 	do_buf(str->data, str->length, type, flags, NULL, io_ch, arg);
365*0Sstevel@tonic-gate 	if(quotes && !io_ch(arg, "\"", 1)) return -1;
366*0Sstevel@tonic-gate 	return outlen;
367*0Sstevel@tonic-gate }
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate /* Used for line indenting: print 'indent' spaces */
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate static int do_indent(char_io *io_ch, void *arg, int indent)
372*0Sstevel@tonic-gate {
373*0Sstevel@tonic-gate 	int i;
374*0Sstevel@tonic-gate 	for(i = 0; i < indent; i++)
375*0Sstevel@tonic-gate 			if(!io_ch(arg, " ", 1)) return 0;
376*0Sstevel@tonic-gate 	return 1;
377*0Sstevel@tonic-gate }
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate #define FN_WIDTH_LN	25
380*0Sstevel@tonic-gate #define FN_WIDTH_SN	10
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n,
383*0Sstevel@tonic-gate 				int indent, unsigned long flags)
384*0Sstevel@tonic-gate {
385*0Sstevel@tonic-gate 	int i, prev = -1, orflags, cnt;
386*0Sstevel@tonic-gate 	int fn_opt, fn_nid;
387*0Sstevel@tonic-gate 	ASN1_OBJECT *fn;
388*0Sstevel@tonic-gate 	ASN1_STRING *val;
389*0Sstevel@tonic-gate 	X509_NAME_ENTRY *ent;
390*0Sstevel@tonic-gate 	char objtmp[80];
391*0Sstevel@tonic-gate 	const char *objbuf;
392*0Sstevel@tonic-gate 	int outlen, len;
393*0Sstevel@tonic-gate 	char *sep_dn, *sep_mv, *sep_eq;
394*0Sstevel@tonic-gate 	int sep_dn_len, sep_mv_len, sep_eq_len;
395*0Sstevel@tonic-gate 	if(indent < 0) indent = 0;
396*0Sstevel@tonic-gate 	outlen = indent;
397*0Sstevel@tonic-gate 	if(!do_indent(io_ch, arg, indent)) return -1;
398*0Sstevel@tonic-gate 	switch (flags & XN_FLAG_SEP_MASK)
399*0Sstevel@tonic-gate 	{
400*0Sstevel@tonic-gate 		case XN_FLAG_SEP_MULTILINE:
401*0Sstevel@tonic-gate 		sep_dn = "\n";
402*0Sstevel@tonic-gate 		sep_dn_len = 1;
403*0Sstevel@tonic-gate 		sep_mv = " + ";
404*0Sstevel@tonic-gate 		sep_mv_len = 3;
405*0Sstevel@tonic-gate 		break;
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 		case XN_FLAG_SEP_COMMA_PLUS:
408*0Sstevel@tonic-gate 		sep_dn = ",";
409*0Sstevel@tonic-gate 		sep_dn_len = 1;
410*0Sstevel@tonic-gate 		sep_mv = "+";
411*0Sstevel@tonic-gate 		sep_mv_len = 1;
412*0Sstevel@tonic-gate 		indent = 0;
413*0Sstevel@tonic-gate 		break;
414*0Sstevel@tonic-gate 
415*0Sstevel@tonic-gate 		case XN_FLAG_SEP_CPLUS_SPC:
416*0Sstevel@tonic-gate 		sep_dn = ", ";
417*0Sstevel@tonic-gate 		sep_dn_len = 2;
418*0Sstevel@tonic-gate 		sep_mv = " + ";
419*0Sstevel@tonic-gate 		sep_mv_len = 3;
420*0Sstevel@tonic-gate 		indent = 0;
421*0Sstevel@tonic-gate 		break;
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate 		case XN_FLAG_SEP_SPLUS_SPC:
424*0Sstevel@tonic-gate 		sep_dn = "; ";
425*0Sstevel@tonic-gate 		sep_dn_len = 2;
426*0Sstevel@tonic-gate 		sep_mv = " + ";
427*0Sstevel@tonic-gate 		sep_mv_len = 3;
428*0Sstevel@tonic-gate 		indent = 0;
429*0Sstevel@tonic-gate 		break;
430*0Sstevel@tonic-gate 
431*0Sstevel@tonic-gate 		default:
432*0Sstevel@tonic-gate 		return -1;
433*0Sstevel@tonic-gate 	}
434*0Sstevel@tonic-gate 
435*0Sstevel@tonic-gate 	if(flags & XN_FLAG_SPC_EQ) {
436*0Sstevel@tonic-gate 		sep_eq = " = ";
437*0Sstevel@tonic-gate 		sep_eq_len = 3;
438*0Sstevel@tonic-gate 	} else {
439*0Sstevel@tonic-gate 		sep_eq = "=";
440*0Sstevel@tonic-gate 		sep_eq_len = 1;
441*0Sstevel@tonic-gate 	}
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 	fn_opt = flags & XN_FLAG_FN_MASK;
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate 	cnt = X509_NAME_entry_count(n);
446*0Sstevel@tonic-gate 	for(i = 0; i < cnt; i++) {
447*0Sstevel@tonic-gate 		if(flags & XN_FLAG_DN_REV)
448*0Sstevel@tonic-gate 				ent = X509_NAME_get_entry(n, cnt - i - 1);
449*0Sstevel@tonic-gate 		else ent = X509_NAME_get_entry(n, i);
450*0Sstevel@tonic-gate 		if(prev != -1) {
451*0Sstevel@tonic-gate 			if(prev == ent->set) {
452*0Sstevel@tonic-gate 				if(!io_ch(arg, sep_mv, sep_mv_len)) return -1;
453*0Sstevel@tonic-gate 				outlen += sep_mv_len;
454*0Sstevel@tonic-gate 			} else {
455*0Sstevel@tonic-gate 				if(!io_ch(arg, sep_dn, sep_dn_len)) return -1;
456*0Sstevel@tonic-gate 				outlen += sep_dn_len;
457*0Sstevel@tonic-gate 				if(!do_indent(io_ch, arg, indent)) return -1;
458*0Sstevel@tonic-gate 				outlen += indent;
459*0Sstevel@tonic-gate 			}
460*0Sstevel@tonic-gate 		}
461*0Sstevel@tonic-gate 		prev = ent->set;
462*0Sstevel@tonic-gate 		fn = X509_NAME_ENTRY_get_object(ent);
463*0Sstevel@tonic-gate 		val = X509_NAME_ENTRY_get_data(ent);
464*0Sstevel@tonic-gate 		fn_nid = OBJ_obj2nid(fn);
465*0Sstevel@tonic-gate 		if(fn_opt != XN_FLAG_FN_NONE) {
466*0Sstevel@tonic-gate 			int objlen, fld_len;
467*0Sstevel@tonic-gate 			if((fn_opt == XN_FLAG_FN_OID) || (fn_nid==NID_undef) ) {
468*0Sstevel@tonic-gate 				OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1);
469*0Sstevel@tonic-gate 				fld_len = 0; /* XXX: what should this be? */
470*0Sstevel@tonic-gate 				objbuf = objtmp;
471*0Sstevel@tonic-gate 			} else {
472*0Sstevel@tonic-gate 				if(fn_opt == XN_FLAG_FN_SN) {
473*0Sstevel@tonic-gate 					fld_len = FN_WIDTH_SN;
474*0Sstevel@tonic-gate 					objbuf = OBJ_nid2sn(fn_nid);
475*0Sstevel@tonic-gate 				} else if(fn_opt == XN_FLAG_FN_LN) {
476*0Sstevel@tonic-gate 					fld_len = FN_WIDTH_LN;
477*0Sstevel@tonic-gate 					objbuf = OBJ_nid2ln(fn_nid);
478*0Sstevel@tonic-gate 				} else {
479*0Sstevel@tonic-gate 					fld_len = 0; /* XXX: what should this be? */
480*0Sstevel@tonic-gate 					objbuf = "";
481*0Sstevel@tonic-gate 				}
482*0Sstevel@tonic-gate 			}
483*0Sstevel@tonic-gate 			objlen = strlen(objbuf);
484*0Sstevel@tonic-gate 			if(!io_ch(arg, objbuf, objlen)) return -1;
485*0Sstevel@tonic-gate 			if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
486*0Sstevel@tonic-gate 				if (!do_indent(io_ch, arg, fld_len - objlen)) return -1;
487*0Sstevel@tonic-gate 				outlen += fld_len - objlen;
488*0Sstevel@tonic-gate 			}
489*0Sstevel@tonic-gate 			if(!io_ch(arg, sep_eq, sep_eq_len)) return -1;
490*0Sstevel@tonic-gate 			outlen += objlen + sep_eq_len;
491*0Sstevel@tonic-gate 		}
492*0Sstevel@tonic-gate 		/* If the field name is unknown then fix up the DER dump
493*0Sstevel@tonic-gate 		 * flag. We might want to limit this further so it will
494*0Sstevel@tonic-gate  		 * DER dump on anything other than a few 'standard' fields.
495*0Sstevel@tonic-gate 		 */
496*0Sstevel@tonic-gate 		if((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
497*0Sstevel@tonic-gate 					orflags = ASN1_STRFLGS_DUMP_ALL;
498*0Sstevel@tonic-gate 		else orflags = 0;
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 		len = do_print_ex(io_ch, arg, flags | orflags, val);
501*0Sstevel@tonic-gate 		if(len < 0) return -1;
502*0Sstevel@tonic-gate 		outlen += len;
503*0Sstevel@tonic-gate 	}
504*0Sstevel@tonic-gate 	return outlen;
505*0Sstevel@tonic-gate }
506*0Sstevel@tonic-gate 
507*0Sstevel@tonic-gate /* Wrappers round the main functions */
508*0Sstevel@tonic-gate 
509*0Sstevel@tonic-gate int X509_NAME_print_ex(BIO *out, X509_NAME *nm, int indent, unsigned long flags)
510*0Sstevel@tonic-gate {
511*0Sstevel@tonic-gate 	if(flags == XN_FLAG_COMPAT)
512*0Sstevel@tonic-gate 		return X509_NAME_print(out, nm, indent);
513*0Sstevel@tonic-gate 	return do_name_ex(send_bio_chars, out, nm, indent, flags);
514*0Sstevel@tonic-gate }
515*0Sstevel@tonic-gate 
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate int X509_NAME_print_ex_fp(FILE *fp, X509_NAME *nm, int indent, unsigned long flags)
518*0Sstevel@tonic-gate {
519*0Sstevel@tonic-gate 	if(flags == XN_FLAG_COMPAT)
520*0Sstevel@tonic-gate 		{
521*0Sstevel@tonic-gate 		BIO *btmp;
522*0Sstevel@tonic-gate 		int ret;
523*0Sstevel@tonic-gate 		btmp = BIO_new_fp(fp, BIO_NOCLOSE);
524*0Sstevel@tonic-gate 		if(!btmp) return -1;
525*0Sstevel@tonic-gate 		ret = X509_NAME_print(btmp, nm, indent);
526*0Sstevel@tonic-gate 		BIO_free(btmp);
527*0Sstevel@tonic-gate 		return ret;
528*0Sstevel@tonic-gate 		}
529*0Sstevel@tonic-gate 	return do_name_ex(send_fp_chars, fp, nm, indent, flags);
530*0Sstevel@tonic-gate }
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate int ASN1_STRING_print_ex(BIO *out, ASN1_STRING *str, unsigned long flags)
533*0Sstevel@tonic-gate {
534*0Sstevel@tonic-gate 	return do_print_ex(send_bio_chars, out, flags, str);
535*0Sstevel@tonic-gate }
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate int ASN1_STRING_print_ex_fp(FILE *fp, ASN1_STRING *str, unsigned long flags)
539*0Sstevel@tonic-gate {
540*0Sstevel@tonic-gate 	return do_print_ex(send_fp_chars, fp, flags, str);
541*0Sstevel@tonic-gate }
542*0Sstevel@tonic-gate 
543*0Sstevel@tonic-gate /* Utility function: convert any string type to UTF8, returns number of bytes
544*0Sstevel@tonic-gate  * in output string or a negative error code
545*0Sstevel@tonic-gate  */
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in)
548*0Sstevel@tonic-gate {
549*0Sstevel@tonic-gate 	ASN1_STRING stmp, *str = &stmp;
550*0Sstevel@tonic-gate 	int mbflag, type, ret;
551*0Sstevel@tonic-gate 	if(!in) return -1;
552*0Sstevel@tonic-gate 	type = in->type;
553*0Sstevel@tonic-gate 	if((type < 0) || (type > 30)) return -1;
554*0Sstevel@tonic-gate 	mbflag = tag2nbyte[type];
555*0Sstevel@tonic-gate 	if(mbflag == -1) return -1;
556*0Sstevel@tonic-gate 	mbflag |= MBSTRING_FLAG;
557*0Sstevel@tonic-gate 	stmp.data = NULL;
558*0Sstevel@tonic-gate 	ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag, B_ASN1_UTF8STRING);
559*0Sstevel@tonic-gate 	if(ret < 0) return ret;
560*0Sstevel@tonic-gate 	*out = stmp.data;
561*0Sstevel@tonic-gate 	return stmp.length;
562*0Sstevel@tonic-gate }
563