/src/openssl30/crypto/asn1/a_strex.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | #include <stdio.h> | 
| 11 |  | #include <string.h> | 
| 12 |  | #include "internal/cryptlib.h" | 
| 13 |  | #include "crypto/asn1.h" | 
| 14 |  | #include <openssl/crypto.h> | 
| 15 |  | #include <openssl/x509.h> | 
| 16 |  | #include <openssl/asn1.h> | 
| 17 |  |  | 
| 18 |  | #include "charmap.h" | 
| 19 |  |  | 
| 20 |  | /* | 
| 21 |  |  * ASN1_STRING_print_ex() and X509_NAME_print_ex(). Enhanced string and name | 
| 22 |  |  * printing routines handling multibyte characters, RFC2253 and a host of | 
| 23 |  |  * other options. | 
| 24 |  |  */ | 
| 25 |  |  | 
| 26 | 4.03M | #define CHARTYPE_BS_ESC         (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253) | 
| 27 |  |  | 
| 28 | 192k | #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \ | 
| 29 | 192k |                   ASN1_STRFLGS_ESC_2254 | \ | 
| 30 | 192k |                   ASN1_STRFLGS_ESC_QUOTE | \ | 
| 31 | 192k |                   ASN1_STRFLGS_ESC_CTRL | \ | 
| 32 | 192k |                   ASN1_STRFLGS_ESC_MSB) | 
| 33 |  |  | 
| 34 |  | /* | 
| 35 |  |  * Three IO functions for sending data to memory, a BIO and a FILE | 
| 36 |  |  * pointer. | 
| 37 |  |  */ | 
| 38 |  | static int send_bio_chars(void *arg, const void *buf, int len) | 
| 39 | 19.9M | { | 
| 40 | 19.9M |     if (!arg) | 
| 41 | 2.07M |         return 1; | 
| 42 | 17.8M |     if (BIO_write(arg, buf, len) != len) | 
| 43 | 0 |         return 0; | 
| 44 | 17.8M |     return 1; | 
| 45 | 17.8M | } | 
| 46 |  |  | 
| 47 |  | #ifndef OPENSSL_NO_STDIO | 
| 48 |  | static int send_fp_chars(void *arg, const void *buf, int len) | 
| 49 | 0 | { | 
| 50 | 0 |     if (!arg) | 
| 51 | 0 |         return 1; | 
| 52 | 0 |     if (fwrite(buf, 1, len, arg) != (unsigned int)len) | 
| 53 | 0 |         return 0; | 
| 54 | 0 |     return 1; | 
| 55 | 0 | } | 
| 56 |  | #endif | 
| 57 |  |  | 
| 58 |  | typedef int char_io (void *arg, const void *buf, int len); | 
| 59 |  |  | 
| 60 |  | /* | 
| 61 |  |  * This function handles display of strings, one character at a time. It is | 
| 62 |  |  * passed an unsigned long for each character because it could come from 2 or | 
| 63 |  |  * even 4 byte forms. | 
| 64 |  |  */ | 
| 65 |  |  | 
| 66 |  | static int do_esc_char(unsigned long c, unsigned short flags, char *do_quotes, | 
| 67 |  |                        char_io *io_ch, void *arg) | 
| 68 | 4.06M | { | 
| 69 | 4.06M |     unsigned short chflgs; | 
| 70 | 4.06M |     unsigned char chtmp; | 
| 71 | 4.06M |     char tmphex[HEX_SIZE(long) + 3]; | 
| 72 |  |  | 
| 73 | 4.06M |     if (c > 0xffffffffL) | 
| 74 | 0 |         return -1; | 
| 75 | 4.06M |     if (c > 0xffff) { | 
| 76 | 22.5k |         BIO_snprintf(tmphex, sizeof(tmphex), "\\W%08lX", c); | 
| 77 | 22.5k |         if (!io_ch(arg, tmphex, 10)) | 
| 78 | 0 |             return -1; | 
| 79 | 22.5k |         return 10; | 
| 80 | 22.5k |     } | 
| 81 | 4.03M |     if (c > 0xff) { | 
| 82 | 8.97k |         BIO_snprintf(tmphex, sizeof(tmphex), "\\U%04lX", c); | 
| 83 | 8.97k |         if (!io_ch(arg, tmphex, 6)) | 
| 84 | 0 |             return -1; | 
| 85 | 8.97k |         return 6; | 
| 86 | 8.97k |     } | 
| 87 | 4.03M |     chtmp = (unsigned char)c; | 
| 88 | 4.03M |     if (chtmp > 0x7f) | 
| 89 | 1.22M |         chflgs = flags & ASN1_STRFLGS_ESC_MSB; | 
| 90 | 2.80M |     else | 
| 91 | 2.80M |         chflgs = char_type[chtmp] & flags; | 
| 92 | 4.03M |     if (chflgs & CHARTYPE_BS_ESC) { | 
| 93 |  |         /* If we don't escape with quotes, signal we need quotes */ | 
| 94 | 14.4k |         if (chflgs & ASN1_STRFLGS_ESC_QUOTE) { | 
| 95 | 12.2k |             if (do_quotes) | 
| 96 | 6.11k |                 *do_quotes = 1; | 
| 97 | 12.2k |             if (!io_ch(arg, &chtmp, 1)) | 
| 98 | 0 |                 return -1; | 
| 99 | 12.2k |             return 1; | 
| 100 | 12.2k |         } | 
| 101 | 2.16k |         if (!io_ch(arg, "\\", 1)) | 
| 102 | 0 |             return -1; | 
| 103 | 2.16k |         if (!io_ch(arg, &chtmp, 1)) | 
| 104 | 0 |             return -1; | 
| 105 | 2.16k |         return 2; | 
| 106 | 2.16k |     } | 
| 107 | 4.01M |     if (chflgs & (ASN1_STRFLGS_ESC_CTRL | 
| 108 | 4.01M |                   | ASN1_STRFLGS_ESC_MSB | 
| 109 | 4.01M |                   | ASN1_STRFLGS_ESC_2254)) { | 
| 110 | 569k |         BIO_snprintf(tmphex, 11, "\\%02X", chtmp); | 
| 111 | 569k |         if (!io_ch(arg, tmphex, 3)) | 
| 112 | 0 |             return -1; | 
| 113 | 569k |         return 3; | 
| 114 | 569k |     } | 
| 115 |  |     /* | 
| 116 |  |      * If we get this far and do any escaping at all must escape the escape | 
| 117 |  |      * character itself: backslash. | 
| 118 |  |      */ | 
| 119 | 3.44M |     if (chtmp == '\\' && (flags & ESC_FLAGS)) { | 
| 120 | 0 |         if (!io_ch(arg, "\\\\", 2)) | 
| 121 | 0 |             return -1; | 
| 122 | 0 |         return 2; | 
| 123 | 0 |     } | 
| 124 | 3.44M |     if (!io_ch(arg, &chtmp, 1)) | 
| 125 | 0 |         return -1; | 
| 126 | 3.44M |     return 1; | 
| 127 | 3.44M | } | 
| 128 |  |  | 
| 129 | 116k | #define BUF_TYPE_WIDTH_MASK     0x7 | 
| 130 | 3.75M | #define BUF_TYPE_CONVUTF8       0x8 | 
| 131 |  |  | 
| 132 |  | /* | 
| 133 |  |  * This function sends each character in a buffer to do_esc_char(). It | 
| 134 |  |  * interprets the content formats and converts to or from UTF8 as | 
| 135 |  |  * appropriate. | 
| 136 |  |  */ | 
| 137 |  |  | 
| 138 |  | static int do_buf(unsigned char *buf, int buflen, | 
| 139 |  |                   int type, unsigned short flags, char *quotes, char_io *io_ch, | 
| 140 |  |                   void *arg) | 
| 141 | 116k | { | 
| 142 | 116k |     int i, outlen, len, charwidth; | 
| 143 | 116k |     unsigned short orflags; | 
| 144 | 116k |     unsigned char *p, *q; | 
| 145 | 116k |     unsigned long c; | 
| 146 |  |  | 
| 147 | 116k |     p = buf; | 
| 148 | 116k |     q = buf + buflen; | 
| 149 | 116k |     outlen = 0; | 
| 150 | 116k |     charwidth = type & BUF_TYPE_WIDTH_MASK; | 
| 151 |  |  | 
| 152 | 116k |     switch (charwidth) { | 
| 153 | 13.7k |     case 4: | 
| 154 | 13.7k |         if (buflen & 3) { | 
| 155 | 0 |             ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH); | 
| 156 | 0 |             return -1; | 
| 157 | 0 |         } | 
| 158 | 13.7k |         break; | 
| 159 | 29.3k |     case 2: | 
| 160 | 29.3k |         if (buflen & 1) { | 
| 161 | 0 |             ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH); | 
| 162 | 0 |             return -1; | 
| 163 | 0 |         } | 
| 164 | 29.3k |         break; | 
| 165 | 73.2k |     default: | 
| 166 | 73.2k |         break; | 
| 167 | 116k |     } | 
| 168 |  |  | 
| 169 | 3.84M |     while (p != q) { | 
| 170 | 3.74M |         if (p == buf && flags & ASN1_STRFLGS_ESC_2253) | 
| 171 | 42.7k |             orflags = CHARTYPE_FIRST_ESC_2253; | 
| 172 | 3.70M |         else | 
| 173 | 3.70M |             orflags = 0; | 
| 174 |  |  | 
| 175 | 3.74M |         switch (charwidth) { | 
| 176 | 25.5k |         case 4: | 
| 177 | 25.5k |             c = ((unsigned long)*p++) << 24; | 
| 178 | 25.5k |             c |= ((unsigned long)*p++) << 16; | 
| 179 | 25.5k |             c |= ((unsigned long)*p++) << 8; | 
| 180 | 25.5k |             c |= *p++; | 
| 181 | 25.5k |             break; | 
| 182 |  |  | 
| 183 | 169k |         case 2: | 
| 184 | 169k |             c = ((unsigned long)*p++) << 8; | 
| 185 | 169k |             c |= *p++; | 
| 186 | 169k |             break; | 
| 187 |  |  | 
| 188 | 3.29M |         case 1: | 
| 189 | 3.29M |             c = *p++; | 
| 190 | 3.29M |             break; | 
| 191 |  |  | 
| 192 | 254k |         case 0: | 
| 193 | 254k |             i = UTF8_getc(p, buflen, &c); | 
| 194 | 254k |             if (i < 0) | 
| 195 | 22.5k |                 return -1;      /* Invalid UTF8String */ | 
| 196 | 232k |             buflen -= i; | 
| 197 | 232k |             p += i; | 
| 198 | 232k |             break; | 
| 199 | 0 |         default: | 
| 200 | 0 |             return -1;          /* invalid width */ | 
| 201 | 3.74M |         } | 
| 202 | 3.72M |         if (p == q && flags & ASN1_STRFLGS_ESC_2253) | 
| 203 | 42.7k |             orflags = CHARTYPE_LAST_ESC_2253; | 
| 204 | 3.72M |         if (type & BUF_TYPE_CONVUTF8) { | 
| 205 | 228k |             unsigned char utfbuf[6]; | 
| 206 | 228k |             int utflen; | 
| 207 | 228k |             utflen = UTF8_putc(utfbuf, sizeof(utfbuf), c); | 
| 208 | 793k |             for (i = 0; i < utflen; i++) { | 
| 209 |  |                 /* | 
| 210 |  |                  * We don't need to worry about setting orflags correctly | 
| 211 |  |                  * because if utflen==1 its value will be correct anyway | 
| 212 |  |                  * otherwise each character will be > 0x7f and so the | 
| 213 |  |                  * character will never be escaped on first and last. | 
| 214 |  |                  */ | 
| 215 | 565k |                 len = do_esc_char(utfbuf[i], flags | orflags, quotes, | 
| 216 | 565k |                                   io_ch, arg); | 
| 217 | 565k |                 if (len < 0) | 
| 218 | 0 |                     return -1; | 
| 219 | 565k |                 outlen += len; | 
| 220 | 565k |             } | 
| 221 | 3.49M |         } else { | 
| 222 | 3.49M |             len = do_esc_char(c, flags | orflags, quotes, | 
| 223 | 3.49M |                               io_ch, arg); | 
| 224 | 3.49M |             if (len < 0) | 
| 225 | 0 |                 return -1; | 
| 226 | 3.49M |             outlen += len; | 
| 227 | 3.49M |         } | 
| 228 | 3.72M |     } | 
| 229 | 93.7k |     return outlen; | 
| 230 | 116k | } | 
| 231 |  |  | 
| 232 |  | /* This function hex dumps a buffer of characters */ | 
| 233 |  |  | 
| 234 |  | static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, | 
| 235 |  |                        int buflen) | 
| 236 | 120k | { | 
| 237 | 120k |     static const char hexdig[] = "0123456789ABCDEF"; | 
| 238 | 120k |     unsigned char *p, *q; | 
| 239 | 120k |     char hextmp[2]; | 
| 240 | 120k |     if (arg) { | 
| 241 | 120k |         p = buf; | 
| 242 | 120k |         q = buf + buflen; | 
| 243 | 15.5M |         while (p != q) { | 
| 244 | 15.3M |             hextmp[0] = hexdig[*p >> 4]; | 
| 245 | 15.3M |             hextmp[1] = hexdig[*p & 0xf]; | 
| 246 | 15.3M |             if (!io_ch(arg, hextmp, 2)) | 
| 247 | 0 |                 return -1; | 
| 248 | 15.3M |             p++; | 
| 249 | 15.3M |         } | 
| 250 | 120k |     } | 
| 251 | 120k |     return buflen << 1; | 
| 252 | 120k | } | 
| 253 |  |  | 
| 254 |  | /* | 
| 255 |  |  * "dump" a string. This is done when the type is unknown, or the flags | 
| 256 |  |  * request it. We can either dump the content octets or the entire DER | 
| 257 |  |  * encoding. This uses the RFC2253 #01234 format. | 
| 258 |  |  */ | 
| 259 |  |  | 
| 260 |  | static int do_dump(unsigned long lflags, char_io *io_ch, void *arg, | 
| 261 |  |                    const ASN1_STRING *str) | 
| 262 | 120k | { | 
| 263 |  |     /* | 
| 264 |  |      * Placing the ASN1_STRING in a temp ASN1_TYPE allows the DER encoding to | 
| 265 |  |      * readily obtained | 
| 266 |  |      */ | 
| 267 | 120k |     ASN1_TYPE t; | 
| 268 | 120k |     unsigned char *der_buf, *p; | 
| 269 | 120k |     int outlen, der_len; | 
| 270 |  |  | 
| 271 | 120k |     if (!io_ch(arg, "#", 1)) | 
| 272 | 0 |         return -1; | 
| 273 |  |     /* If we don't dump DER encoding just dump content octets */ | 
| 274 | 120k |     if (!(lflags & ASN1_STRFLGS_DUMP_DER)) { | 
| 275 | 100k |         outlen = do_hex_dump(io_ch, arg, str->data, str->length); | 
| 276 | 100k |         if (outlen < 0) | 
| 277 | 0 |             return -1; | 
| 278 | 100k |         return outlen + 1; | 
| 279 | 100k |     } | 
| 280 | 20.2k |     t.type = str->type; | 
| 281 | 20.2k |     t.value.ptr = (char *)str; | 
| 282 | 20.2k |     der_len = i2d_ASN1_TYPE(&t, NULL); | 
| 283 | 20.2k |     if (der_len <= 0) | 
| 284 | 0 |         return -1; | 
| 285 | 20.2k |     if ((der_buf = OPENSSL_malloc(der_len)) == NULL) { | 
| 286 | 0 |         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); | 
| 287 | 0 |         return -1; | 
| 288 | 0 |     } | 
| 289 | 20.2k |     p = der_buf; | 
| 290 | 20.2k |     i2d_ASN1_TYPE(&t, &p); | 
| 291 | 20.2k |     outlen = do_hex_dump(io_ch, arg, der_buf, der_len); | 
| 292 | 20.2k |     OPENSSL_free(der_buf); | 
| 293 | 20.2k |     if (outlen < 0) | 
| 294 | 0 |         return -1; | 
| 295 | 20.2k |     return outlen + 1; | 
| 296 | 20.2k | } | 
| 297 |  |  | 
| 298 |  | /* | 
| 299 |  |  * Lookup table to convert tags to character widths, 0 = UTF8 encoded, -1 is | 
| 300 |  |  * used for non string types otherwise it is the number of bytes per | 
| 301 |  |  * character | 
| 302 |  |  */ | 
| 303 |  |  | 
| 304 |  | static const signed char tag2nbyte[] = { | 
| 305 |  |     -1, -1, -1, -1, -1,         /* 0-4 */ | 
| 306 |  |     -1, -1, -1, -1, -1,         /* 5-9 */ | 
| 307 |  |     -1, -1,                     /* 10-11 */ | 
| 308 |  |      0,                         /* 12 V_ASN1_UTF8STRING */ | 
| 309 |  |     -1, -1, -1, -1, -1,         /* 13-17 */ | 
| 310 |  |      1,                         /* 18 V_ASN1_NUMERICSTRING */ | 
| 311 |  |      1,                         /* 19 V_ASN1_PRINTABLESTRING */ | 
| 312 |  |      1,                         /* 20 V_ASN1_T61STRING */ | 
| 313 |  |     -1,                         /* 21 */ | 
| 314 |  |      1,                         /* 22 V_ASN1_IA5STRING */ | 
| 315 |  |      1,                         /* 23 V_ASN1_UTCTIME */ | 
| 316 |  |      1,                         /* 24 V_ASN1_GENERALIZEDTIME */ | 
| 317 |  |     -1,                         /* 25 */ | 
| 318 |  |      1,                         /* 26 V_ASN1_ISO64STRING */ | 
| 319 |  |     -1,                         /* 27 */ | 
| 320 |  |      4,                         /* 28 V_ASN1_UNIVERSALSTRING */ | 
| 321 |  |     -1,                         /* 29 */ | 
| 322 |  |      2                          /* 30 V_ASN1_BMPSTRING */ | 
| 323 |  | }; | 
| 324 |  |  | 
| 325 |  | /* | 
| 326 |  |  * This is the main function, print out an ASN1_STRING taking note of various | 
| 327 |  |  * escape and display options. Returns number of characters written or -1 if | 
| 328 |  |  * an error occurred. | 
| 329 |  |  */ | 
| 330 |  |  | 
| 331 |  | static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, | 
| 332 |  |                        const ASN1_STRING *str) | 
| 333 | 190k | { | 
| 334 | 190k |     int outlen, len; | 
| 335 | 190k |     int type; | 
| 336 | 190k |     char quotes; | 
| 337 | 190k |     unsigned short flags; | 
| 338 | 190k |     quotes = 0; | 
| 339 |  |     /* Keep a copy of escape flags */ | 
| 340 | 190k |     flags = (unsigned short)(lflags & ESC_FLAGS); | 
| 341 |  |  | 
| 342 | 190k |     type = str->type; | 
| 343 |  |  | 
| 344 | 190k |     outlen = 0; | 
| 345 |  |  | 
| 346 | 190k |     if (lflags & ASN1_STRFLGS_SHOW_TYPE) { | 
| 347 | 100k |         const char *tagname; | 
| 348 | 100k |         tagname = ASN1_tag2str(type); | 
| 349 | 100k |         outlen += strlen(tagname); | 
| 350 | 100k |         if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1)) | 
| 351 | 0 |             return -1; | 
| 352 | 100k |         outlen++; | 
| 353 | 100k |     } | 
| 354 |  |  | 
| 355 |  |     /* Decide what to do with type, either dump content or display it */ | 
| 356 |  |  | 
| 357 |  |     /* Dump everything */ | 
| 358 | 190k |     if (lflags & ASN1_STRFLGS_DUMP_ALL) | 
| 359 | 100k |         type = -1; | 
| 360 |  |     /* Ignore the string type */ | 
| 361 | 89.6k |     else if (lflags & ASN1_STRFLGS_IGNORE_TYPE) | 
| 362 | 0 |         type = 1; | 
| 363 | 89.6k |     else { | 
| 364 |  |         /* Else determine width based on type */ | 
| 365 | 89.6k |         if ((type > 0) && (type < 31)) | 
| 366 | 88.0k |             type = tag2nbyte[type]; | 
| 367 | 1.63k |         else | 
| 368 | 1.63k |             type = -1; | 
| 369 | 89.6k |         if ((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN)) | 
| 370 | 867 |             type = 1; | 
| 371 | 89.6k |     } | 
| 372 |  |  | 
| 373 | 190k |     if (type == -1) { | 
| 374 | 120k |         len = do_dump(lflags, io_ch, arg, str); | 
| 375 | 120k |         if (len < 0) | 
| 376 | 0 |             return -1; | 
| 377 | 120k |         outlen += len; | 
| 378 | 120k |         return outlen; | 
| 379 | 120k |     } | 
| 380 |  |  | 
| 381 | 69.4k |     if (lflags & ASN1_STRFLGS_UTF8_CONVERT) { | 
| 382 |  |         /* | 
| 383 |  |          * Note: if string is UTF8 and we want to convert to UTF8 then we | 
| 384 |  |          * just interpret it as 1 byte per character to avoid converting | 
| 385 |  |          * twice. | 
| 386 |  |          */ | 
| 387 | 35.6k |         if (!type) | 
| 388 | 8.54k |             type = 1; | 
| 389 | 27.1k |         else | 
| 390 | 27.1k |             type |= BUF_TYPE_CONVUTF8; | 
| 391 | 35.6k |     } | 
| 392 |  |  | 
| 393 | 69.4k |     len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL); | 
| 394 | 69.4k |     if (len < 0) | 
| 395 | 22.5k |         return -1; | 
| 396 | 46.8k |     outlen += len; | 
| 397 | 46.8k |     if (quotes) | 
| 398 | 4.91k |         outlen += 2; | 
| 399 | 46.8k |     if (!arg) | 
| 400 | 0 |         return outlen; | 
| 401 | 46.8k |     if (quotes && !io_ch(arg, "\"", 1)) | 
| 402 | 0 |         return -1; | 
| 403 | 46.8k |     if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0) | 
| 404 | 0 |         return -1; | 
| 405 | 46.8k |     if (quotes && !io_ch(arg, "\"", 1)) | 
| 406 | 0 |         return -1; | 
| 407 | 46.8k |     return outlen; | 
| 408 | 46.8k | } | 
| 409 |  |  | 
| 410 |  | /* Used for line indenting: print 'indent' spaces */ | 
| 411 |  |  | 
| 412 |  | static int do_indent(char_io *io_ch, void *arg, int indent) | 
| 413 | 43.6k | { | 
| 414 | 43.6k |     int i; | 
| 415 | 43.6k |     for (i = 0; i < indent; i++) | 
| 416 | 0 |         if (!io_ch(arg, " ", 1)) | 
| 417 | 0 |             return 0; | 
| 418 | 43.6k |     return 1; | 
| 419 | 43.6k | } | 
| 420 |  |  | 
| 421 | 0 | #define FN_WIDTH_LN     25 | 
| 422 | 9.31k | #define FN_WIDTH_SN     10 | 
| 423 |  |  | 
| 424 |  | static int do_name_ex(char_io *io_ch, void *arg, const X509_NAME *n, | 
| 425 |  |                       int indent, unsigned long flags) | 
| 426 | 36.6k | { | 
| 427 | 36.6k |     int i, prev = -1, orflags, cnt; | 
| 428 | 36.6k |     int fn_opt, fn_nid; | 
| 429 | 36.6k |     ASN1_OBJECT *fn; | 
| 430 | 36.6k |     const ASN1_STRING *val; | 
| 431 | 36.6k |     const X509_NAME_ENTRY *ent; | 
| 432 | 36.6k |     char objtmp[80]; | 
| 433 | 36.6k |     const char *objbuf; | 
| 434 | 36.6k |     int outlen, len; | 
| 435 | 36.6k |     char *sep_dn, *sep_mv, *sep_eq; | 
| 436 | 36.6k |     int sep_dn_len, sep_mv_len, sep_eq_len; | 
| 437 | 36.6k |     if (indent < 0) | 
| 438 | 0 |         indent = 0; | 
| 439 | 36.6k |     outlen = indent; | 
| 440 | 36.6k |     if (!do_indent(io_ch, arg, indent)) | 
| 441 | 0 |         return -1; | 
| 442 | 36.6k |     switch (flags & XN_FLAG_SEP_MASK) { | 
| 443 | 0 |     case XN_FLAG_SEP_MULTILINE: | 
| 444 | 0 |         sep_dn = "\n"; | 
| 445 | 0 |         sep_dn_len = 1; | 
| 446 | 0 |         sep_mv = " + "; | 
| 447 | 0 |         sep_mv_len = 3; | 
| 448 | 0 |         break; | 
| 449 |  |  | 
| 450 | 0 |     case XN_FLAG_SEP_COMMA_PLUS: | 
| 451 | 0 |         sep_dn = ","; | 
| 452 | 0 |         sep_dn_len = 1; | 
| 453 | 0 |         sep_mv = "+"; | 
| 454 | 0 |         sep_mv_len = 1; | 
| 455 | 0 |         indent = 0; | 
| 456 | 0 |         break; | 
| 457 |  |  | 
| 458 | 36.6k |     case XN_FLAG_SEP_CPLUS_SPC: | 
| 459 | 36.6k |         sep_dn = ", "; | 
| 460 | 36.6k |         sep_dn_len = 2; | 
| 461 | 36.6k |         sep_mv = " + "; | 
| 462 | 36.6k |         sep_mv_len = 3; | 
| 463 | 36.6k |         indent = 0; | 
| 464 | 36.6k |         break; | 
| 465 |  |  | 
| 466 | 0 |     case XN_FLAG_SEP_SPLUS_SPC: | 
| 467 | 0 |         sep_dn = "; "; | 
| 468 | 0 |         sep_dn_len = 2; | 
| 469 | 0 |         sep_mv = " + "; | 
| 470 | 0 |         sep_mv_len = 3; | 
| 471 | 0 |         indent = 0; | 
| 472 | 0 |         break; | 
| 473 |  |  | 
| 474 | 0 |     default: | 
| 475 | 0 |         return -1; | 
| 476 | 36.6k |     } | 
| 477 |  |  | 
| 478 | 36.6k |     if (flags & XN_FLAG_SPC_EQ) { | 
| 479 | 36.6k |         sep_eq = " = "; | 
| 480 | 36.6k |         sep_eq_len = 3; | 
| 481 | 36.6k |     } else { | 
| 482 | 0 |         sep_eq = "="; | 
| 483 | 0 |         sep_eq_len = 1; | 
| 484 | 0 |     } | 
| 485 |  |  | 
| 486 | 36.6k |     fn_opt = flags & XN_FLAG_FN_MASK; | 
| 487 |  |  | 
| 488 | 36.6k |     cnt = X509_NAME_entry_count(n); | 
| 489 | 92.6k |     for (i = 0; i < cnt; i++) { | 
| 490 | 55.9k |         if (flags & XN_FLAG_DN_REV) | 
| 491 | 0 |             ent = X509_NAME_get_entry(n, cnt - i - 1); | 
| 492 | 55.9k |         else | 
| 493 | 55.9k |             ent = X509_NAME_get_entry(n, i); | 
| 494 | 55.9k |         if (prev != -1) { | 
| 495 | 31.3k |             if (prev == X509_NAME_ENTRY_set(ent)) { | 
| 496 | 24.3k |                 if (!io_ch(arg, sep_mv, sep_mv_len)) | 
| 497 | 0 |                     return -1; | 
| 498 | 24.3k |                 outlen += sep_mv_len; | 
| 499 | 24.3k |             } else { | 
| 500 | 6.93k |                 if (!io_ch(arg, sep_dn, sep_dn_len)) | 
| 501 | 0 |                     return -1; | 
| 502 | 6.93k |                 outlen += sep_dn_len; | 
| 503 | 6.93k |                 if (!do_indent(io_ch, arg, indent)) | 
| 504 | 0 |                     return -1; | 
| 505 | 6.93k |                 outlen += indent; | 
| 506 | 6.93k |             } | 
| 507 | 31.3k |         } | 
| 508 | 55.9k |         prev = X509_NAME_ENTRY_set(ent); | 
| 509 | 55.9k |         fn = X509_NAME_ENTRY_get_object(ent); | 
| 510 | 55.9k |         val = X509_NAME_ENTRY_get_data(ent); | 
| 511 | 55.9k |         fn_nid = OBJ_obj2nid(fn); | 
| 512 | 55.9k |         if (fn_opt != XN_FLAG_FN_NONE) { | 
| 513 | 55.9k |             int objlen, fld_len; | 
| 514 | 55.9k |             if ((fn_opt == XN_FLAG_FN_OID) || (fn_nid == NID_undef)) { | 
| 515 | 46.6k |                 OBJ_obj2txt(objtmp, sizeof(objtmp), fn, 1); | 
| 516 | 46.6k |                 fld_len = 0;    /* XXX: what should this be? */ | 
| 517 | 46.6k |                 objbuf = objtmp; | 
| 518 | 46.6k |             } else { | 
| 519 | 9.31k |                 if (fn_opt == XN_FLAG_FN_SN) { | 
| 520 | 9.31k |                     fld_len = FN_WIDTH_SN; | 
| 521 | 9.31k |                     objbuf = OBJ_nid2sn(fn_nid); | 
| 522 | 9.31k |                 } else if (fn_opt == XN_FLAG_FN_LN) { | 
| 523 | 0 |                     fld_len = FN_WIDTH_LN; | 
| 524 | 0 |                     objbuf = OBJ_nid2ln(fn_nid); | 
| 525 | 0 |                 } else { | 
| 526 | 0 |                     fld_len = 0; /* XXX: what should this be? */ | 
| 527 | 0 |                     objbuf = ""; | 
| 528 | 0 |                 } | 
| 529 | 9.31k |             } | 
| 530 | 55.9k |             objlen = strlen(objbuf); | 
| 531 | 55.9k |             if (!io_ch(arg, objbuf, objlen)) | 
| 532 | 0 |                 return -1; | 
| 533 | 55.9k |             if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) { | 
| 534 | 0 |                 if (!do_indent(io_ch, arg, fld_len - objlen)) | 
| 535 | 0 |                     return -1; | 
| 536 | 0 |                 outlen += fld_len - objlen; | 
| 537 | 0 |             } | 
| 538 | 55.9k |             if (!io_ch(arg, sep_eq, sep_eq_len)) | 
| 539 | 0 |                 return -1; | 
| 540 | 55.9k |             outlen += objlen + sep_eq_len; | 
| 541 | 55.9k |         } | 
| 542 |  |         /* | 
| 543 |  |          * If the field name is unknown then fix up the DER dump flag. We | 
| 544 |  |          * might want to limit this further so it will DER dump on anything | 
| 545 |  |          * other than a few 'standard' fields. | 
| 546 |  |          */ | 
| 547 | 55.9k |         if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS)) | 
| 548 | 0 |             orflags = ASN1_STRFLGS_DUMP_ALL; | 
| 549 | 55.9k |         else | 
| 550 | 55.9k |             orflags = 0; | 
| 551 |  |  | 
| 552 | 55.9k |         len = do_print_ex(io_ch, arg, flags | orflags, val); | 
| 553 | 55.9k |         if (len < 0) | 
| 554 | 0 |             return -1; | 
| 555 | 55.9k |         outlen += len; | 
| 556 | 55.9k |     } | 
| 557 | 36.6k |     return outlen; | 
| 558 | 36.6k | } | 
| 559 |  |  | 
| 560 |  | /* Wrappers round the main functions */ | 
| 561 |  |  | 
| 562 |  | int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent, | 
| 563 |  |                        unsigned long flags) | 
| 564 | 106k | { | 
| 565 | 106k |     if (flags == XN_FLAG_COMPAT) | 
| 566 | 69.7k |         return X509_NAME_print(out, nm, indent); | 
| 567 | 36.6k |     return do_name_ex(send_bio_chars, out, nm, indent, flags); | 
| 568 | 106k | } | 
| 569 |  |  | 
| 570 |  | #ifndef OPENSSL_NO_STDIO | 
| 571 |  | int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent, | 
| 572 |  |                           unsigned long flags) | 
| 573 | 0 | { | 
| 574 | 0 |     if (flags == XN_FLAG_COMPAT) { | 
| 575 | 0 |         BIO *btmp; | 
| 576 | 0 |         int ret; | 
| 577 | 0 |         btmp = BIO_new_fp(fp, BIO_NOCLOSE); | 
| 578 | 0 |         if (!btmp) | 
| 579 | 0 |             return -1; | 
| 580 | 0 |         ret = X509_NAME_print(btmp, nm, indent); | 
| 581 | 0 |         BIO_free(btmp); | 
| 582 | 0 |         return ret; | 
| 583 | 0 |     } | 
| 584 | 0 |     return do_name_ex(send_fp_chars, fp, nm, indent, flags); | 
| 585 | 0 | } | 
| 586 |  | #endif | 
| 587 |  |  | 
| 588 |  | int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags) | 
| 589 | 134k | { | 
| 590 | 134k |     return do_print_ex(send_bio_chars, out, flags, str); | 
| 591 | 134k | } | 
| 592 |  |  | 
| 593 |  | #ifndef OPENSSL_NO_STDIO | 
| 594 |  | int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags) | 
| 595 | 0 | { | 
| 596 | 0 |     return do_print_ex(send_fp_chars, fp, flags, str); | 
| 597 | 0 | } | 
| 598 |  | #endif | 
| 599 |  |  | 
| 600 |  | /* | 
| 601 |  |  * Utility function: convert any string type to UTF8, returns number of bytes | 
| 602 |  |  * in output string or a negative error code | 
| 603 |  |  */ | 
| 604 |  |  | 
| 605 |  | int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in) | 
| 606 | 5.07M | { | 
| 607 | 5.07M |     ASN1_STRING stmp, *str = &stmp; | 
| 608 | 5.07M |     int mbflag, type, ret; | 
| 609 | 5.07M |     if (!in) | 
| 610 | 0 |         return -1; | 
| 611 | 5.07M |     type = in->type; | 
| 612 | 5.07M |     if ((type < 0) || (type > 30)) | 
| 613 | 0 |         return -1; | 
| 614 | 5.07M |     mbflag = tag2nbyte[type]; | 
| 615 | 5.07M |     if (mbflag == -1) | 
| 616 | 1 |         return -1; | 
| 617 | 5.07M |     mbflag |= MBSTRING_FLAG; | 
| 618 | 5.07M |     stmp.data = NULL; | 
| 619 | 5.07M |     stmp.length = 0; | 
| 620 | 5.07M |     stmp.flags = 0; | 
| 621 | 5.07M |     ret = | 
| 622 | 5.07M |         ASN1_mbstring_copy(&str, in->data, in->length, mbflag, | 
| 623 | 5.07M |                            B_ASN1_UTF8STRING); | 
| 624 | 5.07M |     if (ret < 0) | 
| 625 | 34.3k |         return ret; | 
| 626 | 5.03M |     *out = stmp.data; | 
| 627 | 5.03M |     return stmp.length; | 
| 628 | 5.07M | } |