Update contrib.
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).
59 © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
65 #include <openssl/crypto.h>
66 #include <openssl/x509.h>
67 #include <openssl/asn1.h>
68 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
69 #include "libcrypto_wsd_macros.h"
70 #include "libcrypto_wsd.h"
76 /* ASN1_STRING_print_ex() and X509_NAME_print_ex().
77 * Enhanced string and name printing routines handling
78 * multibyte characters, RFC2253 and a host of other
83 #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)
86 /* Three IO functions for sending data to memory, a BIO and
89 #if 0 /* never used */
90 static int send_mem_chars(void *arg, const void *buf, int len)
92 unsigned char **out = arg;
94 memcpy(*out, buf, len);
100 static int send_bio_chars(void *arg, const void *buf, int len)
103 if(BIO_write(arg, buf, len) != len) return 0;
107 static int send_fp_chars(void *arg, const void *buf, int len)
110 if(fwrite(buf, 1, len, arg) != (unsigned int)len) return 0;
114 typedef int char_io(void *arg, const void *buf, int len);
116 /* This function handles display of
117 * strings, one character at a time.
118 * It is passed an unsigned long for each
119 * character because it could come from 2 or even
123 static int do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, char_io *io_ch, void *arg)
125 unsigned char chflgs, chtmp;
126 char tmphex[HEX_SIZE(long)+3];
131 BIO_snprintf(tmphex, sizeof tmphex, "\\W%08lX", c);
132 if(!io_ch(arg, tmphex, 10)) return -1;
136 BIO_snprintf(tmphex, sizeof tmphex, "\\U%04lX", c);
137 if(!io_ch(arg, tmphex, 6)) return -1;
140 chtmp = (unsigned char)c;
141 if(chtmp > 0x7f) chflgs = flags & ASN1_STRFLGS_ESC_MSB;
142 else chflgs = char_type[chtmp] & flags;
143 if(chflgs & CHARTYPE_BS_ESC) {
144 /* If we don't escape with quotes, signal we need quotes */
145 if(chflgs & ASN1_STRFLGS_ESC_QUOTE) {
146 if(do_quotes) *do_quotes = 1;
147 if(!io_ch(arg, &chtmp, 1)) return -1;
150 if(!io_ch(arg, "\\", 1)) return -1;
151 if(!io_ch(arg, &chtmp, 1)) return -1;
154 if(chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) {
155 BIO_snprintf(tmphex, 11, "\\%02X", chtmp);
156 if(!io_ch(arg, tmphex, 3)) return -1;
159 if(!io_ch(arg, &chtmp, 1)) return -1;
163 #define BUF_TYPE_WIDTH_MASK 0x7
164 #define BUF_TYPE_CONVUTF8 0x8
166 /* This function sends each character in a buffer to
167 * do_esc_char(). It interprets the content formats
168 * and converts to or from UTF8 as appropriate.
171 static int do_buf(unsigned char *buf, int buflen,
172 int type, unsigned char flags, char *quotes, char_io *io_ch, void *arg)
175 unsigned char orflags, *p, *q;
181 if(p == buf && flags & ASN1_STRFLGS_ESC_2253) orflags = CHARTYPE_FIRST_ESC_2253;
183 switch(type & BUF_TYPE_WIDTH_MASK) {
185 c = ((unsigned long)*p++) << 24;
186 c |= ((unsigned long)*p++) << 16;
187 c |= ((unsigned long)*p++) << 8;
192 c = ((unsigned long)*p++) << 8;
201 i = UTF8_getc(p, buflen, &c);
202 if(i < 0) return -1; /* Invalid UTF8String */
206 return -1; /* invalid width */
209 if (p == q && flags & ASN1_STRFLGS_ESC_2253) orflags = CHARTYPE_LAST_ESC_2253;
210 if(type & BUF_TYPE_CONVUTF8) {
211 unsigned char utfbuf[6];
213 utflen = UTF8_putc(utfbuf, sizeof utfbuf, c);
214 for(i = 0; i < utflen; i++) {
215 /* We don't need to worry about setting orflags correctly
216 * because if utflen==1 its value will be correct anyway
217 * otherwise each character will be > 0x7f and so the
218 * character will never be escaped on first and last.
220 len = do_esc_char(utfbuf[i], (unsigned char)(flags | orflags), quotes, io_ch, arg);
221 if(len < 0) return -1;
225 len = do_esc_char(c, (unsigned char)(flags | orflags), quotes, io_ch, arg);
226 if(len < 0) return -1;
233 /* This function hex dumps a buffer of characters */
235 static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, int buflen)
238 static const char hexdig[] = "0123456789ABCDEF";
239 unsigned char *p, *q;
245 hextmp[0] = hexdig[*p >> 4];
246 hextmp[1] = hexdig[*p & 0xf];
247 if(!io_ch(arg, hextmp, 2)) return -1;
254 /* "dump" a string. This is done when the type is unknown,
255 * or the flags request it. We can either dump the content
256 * octets or the entire DER encoding. This uses the RFC2253
260 static int do_dump(unsigned long lflags, char_io *io_ch, void *arg, ASN1_STRING *str)
262 /* Placing the ASN1_STRING in a temp ASN1_TYPE allows
263 * the DER encoding to readily obtained
266 unsigned char *der_buf, *p;
269 if(!io_ch(arg, "#", 1)) return -1;
270 /* If we don't dump DER encoding just dump content octets */
271 if(!(lflags & ASN1_STRFLGS_DUMP_DER)) {
272 outlen = do_hex_dump(io_ch, arg, str->data, str->length);
273 if(outlen < 0) return -1;
277 t.value.ptr = (char *)str;
278 der_len = i2d_ASN1_TYPE(&t, NULL);
279 der_buf = OPENSSL_malloc(der_len);
280 if(!der_buf) return -1;
282 i2d_ASN1_TYPE(&t, &p);
283 outlen = do_hex_dump(io_ch, arg, der_buf, der_len);
284 OPENSSL_free(der_buf);
285 if(outlen < 0) return -1;
289 /* Lookup table to convert tags to character widths,
290 * 0 = UTF8 encoded, -1 is used for non string types
291 * otherwise it is the number of bytes per character
295 static const signed char tag2nbyte[] = {
296 -1, -1, -1, -1, -1, /* 0-4 */
297 -1, -1, -1, -1, -1, /* 5-9 */
298 -1, -1, 0, -1, /* 10-13 */
299 -1, -1, -1, -1, /* 15-17 */
300 -1, 1, 1, /* 18-20 */
301 -1, 1, 1, 1, /* 21-24 */
302 -1, 1, -1, /* 25-27 */
306 #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \
307 ASN1_STRFLGS_ESC_QUOTE | \
308 ASN1_STRFLGS_ESC_CTRL | \
309 ASN1_STRFLGS_ESC_MSB)
311 /* This is the main function, print out an
312 * ASN1_STRING taking note of various escape
313 * and display options. Returns number of
314 * characters written or -1 if an error
318 static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, ASN1_STRING *str)
325 /* Keep a copy of escape flags */
326 flags = (unsigned char)(lflags & ESC_FLAGS);
333 if(lflags & ASN1_STRFLGS_SHOW_TYPE) {
335 tagname = ASN1_tag2str(type);
336 outlen += strlen(tagname);
337 if(!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1)) return -1;
341 /* Decide what to do with type, either dump content or display it */
343 /* Dump everything */
344 if(lflags & ASN1_STRFLGS_DUMP_ALL) type = -1;
345 /* Ignore the string type */
346 else if(lflags & ASN1_STRFLGS_IGNORE_TYPE) type = 1;
348 /* Else determine width based on type */
349 if((type > 0) && (type < 31)) type = tag2nbyte[type];
351 if((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN)) type = 1;
355 len = do_dump(lflags, io_ch, arg, str);
356 if(len < 0) return -1;
361 if(lflags & ASN1_STRFLGS_UTF8_CONVERT) {
362 /* Note: if string is UTF8 and we want
363 * to convert to UTF8 then we just interpret
364 * it as 1 byte per character to avoid converting
368 else type |= BUF_TYPE_CONVUTF8;
371 len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL);
372 if(len < 0) return -1;
374 if(quotes) outlen += 2;
375 if(!arg) return outlen;
376 if(quotes && !io_ch(arg, "\"", 1)) return -1;
377 if(do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
379 if(quotes && !io_ch(arg, "\"", 1)) return -1;
383 /* Used for line indenting: print 'indent' spaces */
385 static int do_indent(char_io *io_ch, void *arg, int indent)
388 for(i = 0; i < indent; i++)
389 if(!io_ch(arg, " ", 1)) return 0;
393 #define FN_WIDTH_LN 25
394 #define FN_WIDTH_SN 10
396 static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n,
397 int indent, unsigned long flags)
399 int i, prev = -1, orflags, cnt;
403 X509_NAME_ENTRY *ent;
407 char *sep_dn, *sep_mv, *sep_eq;
408 int sep_dn_len, sep_mv_len, sep_eq_len;
409 if(indent < 0) indent = 0;
411 if(!do_indent(io_ch, arg, indent)) return -1;
412 switch (flags & XN_FLAG_SEP_MASK)
414 case XN_FLAG_SEP_MULTILINE:
421 case XN_FLAG_SEP_COMMA_PLUS:
429 case XN_FLAG_SEP_CPLUS_SPC:
437 case XN_FLAG_SEP_SPLUS_SPC:
449 if(flags & XN_FLAG_SPC_EQ) {
457 fn_opt = flags & XN_FLAG_FN_MASK;
459 cnt = X509_NAME_entry_count(n);
460 for(i = 0; i < cnt; i++) {
461 if(flags & XN_FLAG_DN_REV)
462 ent = X509_NAME_get_entry(n, cnt - i - 1);
463 else ent = X509_NAME_get_entry(n, i);
465 if(prev == ent->set) {
466 if(!io_ch(arg, sep_mv, sep_mv_len)) return -1;
467 outlen += sep_mv_len;
469 if(!io_ch(arg, sep_dn, sep_dn_len)) return -1;
470 outlen += sep_dn_len;
471 if(!do_indent(io_ch, arg, indent)) return -1;
476 fn = X509_NAME_ENTRY_get_object(ent);
477 val = X509_NAME_ENTRY_get_data(ent);
478 fn_nid = OBJ_obj2nid(fn);
479 if(fn_opt != XN_FLAG_FN_NONE) {
481 if((fn_opt == XN_FLAG_FN_OID) || (fn_nid==NID_undef) ) {
482 OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1);
483 fld_len = 0; /* XXX: what should this be? */
486 if(fn_opt == XN_FLAG_FN_SN) {
487 fld_len = FN_WIDTH_SN;
488 objbuf = OBJ_nid2sn(fn_nid);
489 } else if(fn_opt == XN_FLAG_FN_LN) {
490 fld_len = FN_WIDTH_LN;
491 objbuf = OBJ_nid2ln(fn_nid);
493 fld_len = 0; /* XXX: what should this be? */
497 objlen = strlen(objbuf);
498 if(!io_ch(arg, objbuf, objlen)) return -1;
499 if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
500 if (!do_indent(io_ch, arg, fld_len - objlen)) return -1;
501 outlen += fld_len - objlen;
503 if(!io_ch(arg, sep_eq, sep_eq_len)) return -1;
504 outlen += objlen + sep_eq_len;
506 /* If the field name is unknown then fix up the DER dump
507 * flag. We might want to limit this further so it will
508 * DER dump on anything other than a few 'standard' fields.
510 if((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
511 orflags = ASN1_STRFLGS_DUMP_ALL;
514 len = do_print_ex(io_ch, arg, flags | orflags, val);
515 if(len < 0) return -1;
521 /* Wrappers round the main functions */
523 EXPORT_C int X509_NAME_print_ex(BIO *out, X509_NAME *nm, int indent, unsigned long flags)
525 if(flags == XN_FLAG_COMPAT)
526 return X509_NAME_print(out, nm, indent);
527 return do_name_ex(send_bio_chars, out, nm, indent, flags);
530 #ifndef OPENSSL_NO_FP_API
531 EXPORT_C int X509_NAME_print_ex_fp(FILE *fp, X509_NAME *nm, int indent, unsigned long flags)
533 if(flags == XN_FLAG_COMPAT)
537 btmp = BIO_new_fp(fp, BIO_NOCLOSE);
539 ret = X509_NAME_print(btmp, nm, indent);
543 return do_name_ex(send_fp_chars, fp, nm, indent, flags);
547 EXPORT_C int ASN1_STRING_print_ex(BIO *out, ASN1_STRING *str, unsigned long flags)
549 return do_print_ex(send_bio_chars, out, flags, str);
552 #ifndef OPENSSL_NO_FP_API
553 EXPORT_C int ASN1_STRING_print_ex_fp(FILE *fp, ASN1_STRING *str, unsigned long flags)
555 return do_print_ex(send_fp_chars, fp, flags, str);
559 /* Utility function: convert any string type to UTF8, returns number of bytes
560 * in output string or a negative error code
563 EXPORT_C int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in)
565 ASN1_STRING stmp, *str = &stmp;
566 int mbflag, type, ret;
569 if((type < 0) || (type > 30)) return -1;
570 mbflag = tag2nbyte[type];
571 if(mbflag == -1) return -1;
572 mbflag |= MBSTRING_FLAG;
574 ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag, B_ASN1_UTF8STRING);
575 if(ret < 0) return ret;