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