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