diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/a_strex.c')
| -rw-r--r-- | src/lib/libcrypto/asn1/a_strex.c | 658 |
1 files changed, 0 insertions, 658 deletions
diff --git a/src/lib/libcrypto/asn1/a_strex.c b/src/lib/libcrypto/asn1/a_strex.c deleted file mode 100644 index 52e1b7db5d..0000000000 --- a/src/lib/libcrypto/asn1/a_strex.c +++ /dev/null | |||
| @@ -1,658 +0,0 @@ | |||
| 1 | /* $OpenBSD: a_strex.c,v 1.38 2025/03/19 11:18:38 tb Exp $ */ | ||
| 2 | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL | ||
| 3 | * project 2000. | ||
| 4 | */ | ||
| 5 | /* ==================================================================== | ||
| 6 | * Copyright (c) 2000 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 <stdint.h> | ||
| 60 | #include <stdio.h> | ||
| 61 | #include <stdlib.h> | ||
| 62 | #include <string.h> | ||
| 63 | |||
| 64 | #include <openssl/asn1.h> | ||
| 65 | #include <openssl/bio.h> | ||
| 66 | #include <openssl/objects.h> | ||
| 67 | #include <openssl/x509.h> | ||
| 68 | |||
| 69 | #include "asn1_local.h" | ||
| 70 | #include "bytestring.h" | ||
| 71 | #include "x509_local.h" | ||
| 72 | |||
| 73 | #include "charmap.h" | ||
| 74 | |||
| 75 | /* ASN1_STRING_print_ex() and X509_NAME_print_ex(). | ||
| 76 | * Enhanced string and name printing routines handling | ||
| 77 | * multibyte characters, RFC2253 and a host of other | ||
| 78 | * options. | ||
| 79 | */ | ||
| 80 | |||
| 81 | #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253) | ||
| 82 | |||
| 83 | #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \ | ||
| 84 | ASN1_STRFLGS_ESC_QUOTE | \ | ||
| 85 | ASN1_STRFLGS_ESC_CTRL | \ | ||
| 86 | ASN1_STRFLGS_ESC_MSB) | ||
| 87 | |||
| 88 | |||
| 89 | /* Three IO functions for sending data to memory, a BIO and | ||
| 90 | * and a FILE pointer. | ||
| 91 | */ | ||
| 92 | static int | ||
| 93 | send_bio_chars(void *arg, const void *buf, int len) | ||
| 94 | { | ||
| 95 | if (!arg) | ||
| 96 | return 1; | ||
| 97 | if (BIO_write(arg, buf, len) != len) | ||
| 98 | return 0; | ||
| 99 | return 1; | ||
| 100 | } | ||
| 101 | |||
| 102 | static int | ||
| 103 | send_fp_chars(void *arg, const void *buf, int len) | ||
| 104 | { | ||
| 105 | if (!arg) | ||
| 106 | return 1; | ||
| 107 | if (fwrite(buf, 1, (size_t)len, arg) != (size_t)len) | ||
| 108 | return 0; | ||
| 109 | return 1; | ||
| 110 | } | ||
| 111 | |||
| 112 | typedef int char_io(void *arg, const void *buf, int len); | ||
| 113 | |||
| 114 | /* This function handles display of | ||
| 115 | * strings, one character at a time. | ||
| 116 | * It is passed an unsigned long for each | ||
| 117 | * character because it could come from 2 or even | ||
| 118 | * 4 byte forms. | ||
| 119 | */ | ||
| 120 | |||
| 121 | static int | ||
| 122 | do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, | ||
| 123 | char_io *io_ch, void *arg) | ||
| 124 | { | ||
| 125 | unsigned char chflgs, chtmp; | ||
| 126 | char tmphex[sizeof(long) * 2 + 3]; | ||
| 127 | |||
| 128 | if (c > 0xffffffffL) | ||
| 129 | return -1; | ||
| 130 | if (c > 0xffff) { | ||
| 131 | snprintf(tmphex, sizeof tmphex, "\\W%08lX", c); | ||
| 132 | if (!io_ch(arg, tmphex, 10)) | ||
| 133 | return -1; | ||
| 134 | return 10; | ||
| 135 | } | ||
| 136 | if (c > 0xff) { | ||
| 137 | snprintf(tmphex, sizeof tmphex, "\\U%04lX", c); | ||
| 138 | if (!io_ch(arg, tmphex, 6)) | ||
| 139 | return -1; | ||
| 140 | return 6; | ||
| 141 | } | ||
| 142 | chtmp = (unsigned char)c; | ||
| 143 | if (chtmp > 0x7f) | ||
| 144 | chflgs = flags & ASN1_STRFLGS_ESC_MSB; | ||
| 145 | else | ||
| 146 | chflgs = char_type[chtmp] & flags; | ||
| 147 | if (chflgs & CHARTYPE_BS_ESC) { | ||
| 148 | /* If we don't escape with quotes, signal we need quotes */ | ||
| 149 | if (chflgs & ASN1_STRFLGS_ESC_QUOTE) { | ||
| 150 | if (do_quotes) | ||
| 151 | *do_quotes = 1; | ||
| 152 | if (!io_ch(arg, &chtmp, 1)) | ||
| 153 | return -1; | ||
| 154 | return 1; | ||
| 155 | } | ||
| 156 | if (!io_ch(arg, "\\", 1)) | ||
| 157 | return -1; | ||
| 158 | if (!io_ch(arg, &chtmp, 1)) | ||
| 159 | return -1; | ||
| 160 | return 2; | ||
| 161 | } | ||
| 162 | if (chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) { | ||
| 163 | snprintf(tmphex, sizeof tmphex, "\\%02X", chtmp); | ||
| 164 | if (!io_ch(arg, tmphex, 3)) | ||
| 165 | return -1; | ||
| 166 | return 3; | ||
| 167 | } | ||
| 168 | /* If we get this far and do any escaping at all must escape | ||
| 169 | * the escape character itself: backslash. | ||
| 170 | */ | ||
| 171 | if (chtmp == '\\' && flags & ESC_FLAGS) { | ||
| 172 | if (!io_ch(arg, "\\\\", 2)) | ||
| 173 | return -1; | ||
| 174 | return 2; | ||
| 175 | } | ||
| 176 | if (!io_ch(arg, &chtmp, 1)) | ||
| 177 | return -1; | ||
| 178 | return 1; | ||
| 179 | } | ||
| 180 | |||
| 181 | #define BUF_TYPE_WIDTH_MASK 0x7 | ||
| 182 | #define BUF_TYPE_CONVUTF8 0x8 | ||
| 183 | |||
| 184 | /* This function sends each character in a buffer to | ||
| 185 | * do_esc_char(). It interprets the content formats | ||
| 186 | * and converts to or from UTF8 as appropriate. | ||
| 187 | */ | ||
| 188 | |||
| 189 | static int | ||
| 190 | do_buf(unsigned char *buf, int buflen, int type, unsigned char flags, | ||
| 191 | char *quotes, char_io *io_ch, void *arg) | ||
| 192 | { | ||
| 193 | int i, outlen, len; | ||
| 194 | unsigned char orflags, *p, *q; | ||
| 195 | unsigned long c; | ||
| 196 | |||
| 197 | p = buf; | ||
| 198 | q = buf + buflen; | ||
| 199 | outlen = 0; | ||
| 200 | while (p != q) { | ||
| 201 | if (p == buf && flags & ASN1_STRFLGS_ESC_2253) | ||
| 202 | orflags = CHARTYPE_FIRST_ESC_2253; | ||
| 203 | else | ||
| 204 | orflags = 0; | ||
| 205 | switch (type & BUF_TYPE_WIDTH_MASK) { | ||
| 206 | case 4: | ||
| 207 | c = ((unsigned long)*p++) << 24; | ||
| 208 | c |= ((unsigned long)*p++) << 16; | ||
| 209 | c |= ((unsigned long)*p++) << 8; | ||
| 210 | c |= *p++; | ||
| 211 | if (c > UNICODE_MAX || UNICODE_IS_SURROGATE(c)) | ||
| 212 | return -1; | ||
| 213 | break; | ||
| 214 | |||
| 215 | case 2: | ||
| 216 | c = ((unsigned long)*p++) << 8; | ||
| 217 | c |= *p++; | ||
| 218 | if (UNICODE_IS_SURROGATE(c)) | ||
| 219 | return -1; | ||
| 220 | break; | ||
| 221 | |||
| 222 | case 1: | ||
| 223 | c = *p++; | ||
| 224 | break; | ||
| 225 | |||
| 226 | case 0: | ||
| 227 | i = UTF8_getc(p, q - p, &c); | ||
| 228 | if (i < 0) | ||
| 229 | return -1; /* Invalid UTF8String */ | ||
| 230 | p += i; | ||
| 231 | break; | ||
| 232 | default: | ||
| 233 | return -1; /* invalid width */ | ||
| 234 | } | ||
| 235 | if (p == q && flags & ASN1_STRFLGS_ESC_2253) | ||
| 236 | orflags = CHARTYPE_LAST_ESC_2253; | ||
| 237 | if (type & BUF_TYPE_CONVUTF8) { | ||
| 238 | unsigned char utfbuf[6]; | ||
| 239 | int utflen; | ||
| 240 | |||
| 241 | utflen = UTF8_putc(utfbuf, sizeof utfbuf, c); | ||
| 242 | if (utflen < 0) | ||
| 243 | return -1; | ||
| 244 | for (i = 0; i < utflen; i++) { | ||
| 245 | /* We don't need to worry about setting orflags correctly | ||
| 246 | * because if utflen==1 its value will be correct anyway | ||
| 247 | * otherwise each character will be > 0x7f and so the | ||
| 248 | * character will never be escaped on first and last. | ||
| 249 | */ | ||
| 250 | len = do_esc_char(utfbuf[i], | ||
| 251 | (unsigned char)(flags | orflags), quotes, | ||
| 252 | io_ch, arg); | ||
| 253 | if (len < 0) | ||
| 254 | return -1; | ||
| 255 | outlen += len; | ||
| 256 | } | ||
| 257 | } else { | ||
| 258 | len = do_esc_char(c, (unsigned char)(flags | orflags), | ||
| 259 | quotes, 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 | ||
| 271 | do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, 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 | /* "dump" a string. This is done when the type is unknown, | ||
| 291 | * or the flags request it. We can either dump the content | ||
| 292 | * octets or the entire DER encoding. This uses the RFC2253 | ||
| 293 | * #01234 format. | ||
| 294 | */ | ||
| 295 | |||
| 296 | static int | ||
| 297 | do_dump(unsigned long lflags, char_io *io_ch, void *arg, const ASN1_STRING *str) | ||
| 298 | { | ||
| 299 | /* Placing the ASN1_STRING in a temp ASN1_TYPE allows | ||
| 300 | * the DER encoding to readily obtained | ||
| 301 | */ | ||
| 302 | ASN1_TYPE t; | ||
| 303 | unsigned char *der_buf, *p; | ||
| 304 | int outlen, der_len; | ||
| 305 | |||
| 306 | if (!io_ch(arg, "#", 1)) | ||
| 307 | return -1; | ||
| 308 | /* If we don't dump DER encoding just dump content octets */ | ||
| 309 | if (!(lflags & ASN1_STRFLGS_DUMP_DER)) { | ||
| 310 | outlen = do_hex_dump(io_ch, arg, str->data, str->length); | ||
| 311 | if (outlen < 0) | ||
| 312 | return -1; | ||
| 313 | return outlen + 1; | ||
| 314 | } | ||
| 315 | t.type = str->type; | ||
| 316 | t.value.ptr = (char *)str; | ||
| 317 | der_len = i2d_ASN1_TYPE(&t, NULL); | ||
| 318 | der_buf = malloc(der_len); | ||
| 319 | if (!der_buf) | ||
| 320 | return -1; | ||
| 321 | p = der_buf; | ||
| 322 | i2d_ASN1_TYPE(&t, &p); | ||
| 323 | outlen = do_hex_dump(io_ch, arg, der_buf, der_len); | ||
| 324 | free(der_buf); | ||
| 325 | if (outlen < 0) | ||
| 326 | return -1; | ||
| 327 | return outlen + 1; | ||
| 328 | } | ||
| 329 | |||
| 330 | /* This is the main function, print out an | ||
| 331 | * ASN1_STRING taking note of various escape | ||
| 332 | * and display options. Returns number of | ||
| 333 | * characters written or -1 if an error | ||
| 334 | * occurred. | ||
| 335 | */ | ||
| 336 | |||
| 337 | static int | ||
| 338 | do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, | ||
| 339 | const ASN1_STRING *str) | ||
| 340 | { | ||
| 341 | int outlen, len; | ||
| 342 | int type; | ||
| 343 | char quotes; | ||
| 344 | unsigned char flags; | ||
| 345 | |||
| 346 | quotes = 0; | ||
| 347 | /* Keep a copy of escape flags */ | ||
| 348 | flags = (unsigned char)(lflags & ESC_FLAGS); | ||
| 349 | type = str->type; | ||
| 350 | outlen = 0; | ||
| 351 | |||
| 352 | if (lflags & ASN1_STRFLGS_SHOW_TYPE) { | ||
| 353 | const char *tagname; | ||
| 354 | tagname = ASN1_tag2str(type); | ||
| 355 | outlen += strlen(tagname); | ||
| 356 | if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1)) | ||
| 357 | return -1; | ||
| 358 | outlen++; | ||
| 359 | } | ||
| 360 | |||
| 361 | /* Decide what to do with type, either dump content or display it */ | ||
| 362 | |||
| 363 | if (lflags & ASN1_STRFLGS_DUMP_ALL) { | ||
| 364 | /* Dump everything. */ | ||
| 365 | type = -1; | ||
| 366 | } else if (lflags & ASN1_STRFLGS_IGNORE_TYPE) { | ||
| 367 | /* Ignore the string type. */ | ||
| 368 | type = 1; | ||
| 369 | } else { | ||
| 370 | /* Else determine width based on type. */ | ||
| 371 | type = asn1_tag2charwidth(type); | ||
| 372 | if (type == -1 && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN)) | ||
| 373 | type = 1; | ||
| 374 | } | ||
| 375 | |||
| 376 | if (type == -1) { | ||
| 377 | len = do_dump(lflags, io_ch, arg, str); | ||
| 378 | if (len < 0) | ||
| 379 | return -1; | ||
| 380 | outlen += len; | ||
| 381 | return outlen; | ||
| 382 | } | ||
| 383 | |||
| 384 | if (lflags & ASN1_STRFLGS_UTF8_CONVERT) { | ||
| 385 | /* Note: if string is UTF8 and we want | ||
| 386 | * to convert to UTF8 then we just interpret | ||
| 387 | * it as 1 byte per character to avoid converting | ||
| 388 | * twice. | ||
| 389 | */ | ||
| 390 | if (!type) | ||
| 391 | type = 1; | ||
| 392 | else | ||
| 393 | type |= BUF_TYPE_CONVUTF8; | ||
| 394 | } | ||
| 395 | |||
| 396 | len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL); | ||
| 397 | if (len < 0) | ||
| 398 | return -1; | ||
| 399 | outlen += len; | ||
| 400 | if (quotes) | ||
| 401 | outlen += 2; | ||
| 402 | if (!arg) | ||
| 403 | return outlen; | ||
| 404 | if (quotes && !io_ch(arg, "\"", 1)) | ||
| 405 | return -1; | ||
| 406 | if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0) | ||
| 407 | return -1; | ||
| 408 | if (quotes && !io_ch(arg, "\"", 1)) | ||
| 409 | return -1; | ||
| 410 | return outlen; | ||
| 411 | } | ||
| 412 | |||
| 413 | /* Used for line indenting: print 'indent' spaces */ | ||
| 414 | |||
| 415 | static int | ||
| 416 | do_indent(char_io *io_ch, void *arg, int indent) | ||
| 417 | { | ||
| 418 | int i; | ||
| 419 | for (i = 0; i < indent; i++) | ||
| 420 | if (!io_ch(arg, " ", 1)) | ||
| 421 | return 0; | ||
| 422 | return 1; | ||
| 423 | } | ||
| 424 | |||
| 425 | #define FN_WIDTH_LN 25 | ||
| 426 | #define FN_WIDTH_SN 10 | ||
| 427 | |||
| 428 | static int | ||
| 429 | do_name_ex(char_io *io_ch, void *arg, const X509_NAME *n, int indent, | ||
| 430 | unsigned long flags) | ||
| 431 | { | ||
| 432 | int i, prev = -1, orflags, cnt; | ||
| 433 | int fn_opt, fn_nid; | ||
| 434 | ASN1_OBJECT *fn; | ||
| 435 | ASN1_STRING *val; | ||
| 436 | X509_NAME_ENTRY *ent; | ||
| 437 | char objtmp[80]; | ||
| 438 | const char *objbuf; | ||
| 439 | int outlen, len; | ||
| 440 | char *sep_dn, *sep_mv, *sep_eq; | ||
| 441 | int sep_dn_len, sep_mv_len, sep_eq_len; | ||
| 442 | |||
| 443 | if (indent < 0) | ||
| 444 | indent = 0; | ||
| 445 | outlen = indent; | ||
| 446 | if (!do_indent(io_ch, arg, indent)) | ||
| 447 | return -1; | ||
| 448 | |||
| 449 | switch (flags & XN_FLAG_SEP_MASK) { | ||
| 450 | case XN_FLAG_SEP_MULTILINE: | ||
| 451 | sep_dn = "\n"; | ||
| 452 | sep_dn_len = 1; | ||
| 453 | sep_mv = " + "; | ||
| 454 | sep_mv_len = 3; | ||
| 455 | break; | ||
| 456 | |||
| 457 | case XN_FLAG_SEP_COMMA_PLUS: | ||
| 458 | sep_dn = ","; | ||
| 459 | sep_dn_len = 1; | ||
| 460 | sep_mv = "+"; | ||
| 461 | sep_mv_len = 1; | ||
| 462 | indent = 0; | ||
| 463 | break; | ||
| 464 | |||
| 465 | case XN_FLAG_SEP_CPLUS_SPC: | ||
| 466 | sep_dn = ", "; | ||
| 467 | sep_dn_len = 2; | ||
| 468 | sep_mv = " + "; | ||
| 469 | sep_mv_len = 3; | ||
| 470 | indent = 0; | ||
| 471 | break; | ||
| 472 | |||
| 473 | case XN_FLAG_SEP_SPLUS_SPC: | ||
| 474 | sep_dn = "; "; | ||
| 475 | sep_dn_len = 2; | ||
| 476 | sep_mv = " + "; | ||
| 477 | sep_mv_len = 3; | ||
| 478 | indent = 0; | ||
| 479 | break; | ||
| 480 | |||
| 481 | default: | ||
| 482 | return -1; | ||
| 483 | } | ||
| 484 | |||
| 485 | if (flags & XN_FLAG_SPC_EQ) { | ||
| 486 | sep_eq = " = "; | ||
| 487 | sep_eq_len = 3; | ||
| 488 | } else { | ||
| 489 | sep_eq = "="; | ||
| 490 | sep_eq_len = 1; | ||
| 491 | } | ||
| 492 | |||
| 493 | fn_opt = flags & XN_FLAG_FN_MASK; | ||
| 494 | |||
| 495 | cnt = X509_NAME_entry_count(n); | ||
| 496 | for (i = 0; i < cnt; i++) { | ||
| 497 | if (flags & XN_FLAG_DN_REV) | ||
| 498 | ent = X509_NAME_get_entry(n, cnt - i - 1); | ||
| 499 | else | ||
| 500 | ent = X509_NAME_get_entry(n, i); | ||
| 501 | if (prev != -1) { | ||
| 502 | if (prev == X509_NAME_ENTRY_set(ent)) { | ||
| 503 | if (!io_ch(arg, sep_mv, sep_mv_len)) | ||
| 504 | return -1; | ||
| 505 | outlen += sep_mv_len; | ||
| 506 | } else { | ||
| 507 | if (!io_ch(arg, sep_dn, sep_dn_len)) | ||
| 508 | return -1; | ||
| 509 | outlen += sep_dn_len; | ||
| 510 | if (!do_indent(io_ch, arg, indent)) | ||
| 511 | return -1; | ||
| 512 | outlen += indent; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | prev = X509_NAME_ENTRY_set(ent); | ||
| 516 | fn = X509_NAME_ENTRY_get_object(ent); | ||
| 517 | val = X509_NAME_ENTRY_get_data(ent); | ||
| 518 | fn_nid = OBJ_obj2nid(fn); | ||
| 519 | if (fn_opt != XN_FLAG_FN_NONE) { | ||
| 520 | int objlen, fld_len; | ||
| 521 | if ((fn_opt == XN_FLAG_FN_OID) || | ||
| 522 | (fn_nid == NID_undef)) { | ||
| 523 | OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1); | ||
| 524 | fld_len = 0; /* XXX: what should this be? */ | ||
| 525 | objbuf = objtmp; | ||
| 526 | } else { | ||
| 527 | if (fn_opt == XN_FLAG_FN_SN) { | ||
| 528 | fld_len = FN_WIDTH_SN; | ||
| 529 | objbuf = OBJ_nid2sn(fn_nid); | ||
| 530 | } else if (fn_opt == XN_FLAG_FN_LN) { | ||
| 531 | fld_len = FN_WIDTH_LN; | ||
| 532 | objbuf = OBJ_nid2ln(fn_nid); | ||
| 533 | } else { | ||
| 534 | fld_len = 0; /* XXX: what should this be? */ | ||
| 535 | objbuf = ""; | ||
| 536 | } | ||
| 537 | } | ||
| 538 | objlen = strlen(objbuf); | ||
| 539 | if (!io_ch(arg, objbuf, objlen)) | ||
| 540 | return -1; | ||
| 541 | if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) { | ||
| 542 | if (!do_indent(io_ch, arg, fld_len - objlen)) | ||
| 543 | return -1; | ||
| 544 | outlen += fld_len - objlen; | ||
| 545 | } | ||
| 546 | if (!io_ch(arg, sep_eq, sep_eq_len)) | ||
| 547 | return -1; | ||
| 548 | outlen += objlen + sep_eq_len; | ||
| 549 | } | ||
| 550 | /* If the field name is unknown then fix up the DER dump | ||
| 551 | * flag. We might want to limit this further so it will | ||
| 552 | * DER dump on anything other than a few 'standard' fields. | ||
| 553 | */ | ||
| 554 | if ((fn_nid == NID_undef) && | ||
| 555 | (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS)) | ||
| 556 | orflags = ASN1_STRFLGS_DUMP_ALL; | ||
| 557 | else | ||
| 558 | orflags = 0; | ||
| 559 | |||
| 560 | len = do_print_ex(io_ch, arg, flags | orflags, val); | ||
| 561 | if (len < 0) | ||
| 562 | return -1; | ||
| 563 | outlen += len; | ||
| 564 | } | ||
| 565 | return outlen; | ||
| 566 | } | ||
| 567 | |||
| 568 | static int | ||
| 569 | X509_NAME_print(BIO *bio, const X509_NAME *name, int obase) | ||
| 570 | { | ||
| 571 | CBB cbb; | ||
| 572 | uint8_t *buf = NULL; | ||
| 573 | size_t buf_len; | ||
| 574 | const X509_NAME_ENTRY *ne; | ||
| 575 | int i; | ||
| 576 | int started = 0; | ||
| 577 | int ret = 0; | ||
| 578 | |||
| 579 | if (!CBB_init(&cbb, 0)) | ||
| 580 | goto err; | ||
| 581 | |||
| 582 | for (i = 0; i < sk_X509_NAME_ENTRY_num(name->entries); i++) { | ||
| 583 | ne = sk_X509_NAME_ENTRY_value(name->entries, i); | ||
| 584 | |||
| 585 | if (started) { | ||
| 586 | if (!CBB_add_u8(&cbb, ',')) | ||
| 587 | goto err; | ||
| 588 | if (!CBB_add_u8(&cbb, ' ')) | ||
| 589 | goto err; | ||
| 590 | } | ||
| 591 | |||
| 592 | if (!X509_NAME_ENTRY_add_cbb(&cbb, ne)) | ||
| 593 | goto err; | ||
| 594 | |||
| 595 | started = 1; | ||
| 596 | } | ||
| 597 | |||
| 598 | if (!CBB_add_u8(&cbb, '\0')) | ||
| 599 | goto err; | ||
| 600 | |||
| 601 | if (!CBB_finish(&cbb, &buf, &buf_len)) | ||
| 602 | goto err; | ||
| 603 | |||
| 604 | if (BIO_printf(bio, "%s", buf) < 0) | ||
| 605 | goto err; | ||
| 606 | |||
| 607 | ret = 1; | ||
| 608 | |||
| 609 | err: | ||
| 610 | CBB_cleanup(&cbb); | ||
| 611 | free(buf); | ||
| 612 | |||
| 613 | return ret; | ||
| 614 | } | ||
| 615 | |||
| 616 | /* Wrappers round the main functions */ | ||
| 617 | |||
| 618 | int | ||
| 619 | X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent, | ||
| 620 | unsigned long flags) | ||
| 621 | { | ||
| 622 | if (flags == XN_FLAG_COMPAT) | ||
| 623 | return X509_NAME_print(out, nm, indent); | ||
| 624 | return do_name_ex(send_bio_chars, out, nm, indent, flags); | ||
| 625 | } | ||
| 626 | LCRYPTO_ALIAS(X509_NAME_print_ex); | ||
| 627 | |||
| 628 | int | ||
| 629 | X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent, | ||
| 630 | unsigned long flags) | ||
| 631 | { | ||
| 632 | if (flags == XN_FLAG_COMPAT) { | ||
| 633 | BIO *btmp; | ||
| 634 | int ret; | ||
| 635 | btmp = BIO_new_fp(fp, BIO_NOCLOSE); | ||
| 636 | if (!btmp) | ||
| 637 | return -1; | ||
| 638 | ret = X509_NAME_print(btmp, nm, indent); | ||
| 639 | BIO_free(btmp); | ||
| 640 | return ret; | ||
| 641 | } | ||
| 642 | return do_name_ex(send_fp_chars, fp, nm, indent, flags); | ||
| 643 | } | ||
| 644 | LCRYPTO_ALIAS(X509_NAME_print_ex_fp); | ||
| 645 | |||
| 646 | int | ||
| 647 | ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags) | ||
| 648 | { | ||
| 649 | return do_print_ex(send_bio_chars, out, flags, str); | ||
| 650 | } | ||
| 651 | LCRYPTO_ALIAS(ASN1_STRING_print_ex); | ||
| 652 | |||
| 653 | int | ||
| 654 | ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags) | ||
| 655 | { | ||
| 656 | return do_print_ex(send_fp_chars, fp, flags, str); | ||
| 657 | } | ||
| 658 | LCRYPTO_ALIAS(ASN1_STRING_print_ex_fp); | ||
