diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/a_strex.c')
| -rw-r--r-- | src/lib/libcrypto/asn1/a_strex.c | 350 |
1 files changed, 216 insertions, 134 deletions
diff --git a/src/lib/libcrypto/asn1/a_strex.c b/src/lib/libcrypto/asn1/a_strex.c index e92c166303..248eac28f6 100644 --- a/src/lib/libcrypto/asn1/a_strex.c +++ b/src/lib/libcrypto/asn1/a_strex.c | |||
| @@ -10,7 +10,7 @@ | |||
| 10 | * are met: | 10 | * are met: |
| 11 | * | 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * | 14 | * |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright | 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in | 16 | * notice, this list of conditions and the following disclaimer in |
| @@ -71,7 +71,6 @@ | |||
| 71 | * options. | 71 | * options. |
| 72 | */ | 72 | */ |
| 73 | 73 | ||
| 74 | |||
| 75 | #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253) | 74 | #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253) |
| 76 | 75 | ||
| 77 | #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \ | 76 | #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \ |
| @@ -84,27 +83,36 @@ | |||
| 84 | * and a FILE pointer. | 83 | * and a FILE pointer. |
| 85 | */ | 84 | */ |
| 86 | #if 0 /* never used */ | 85 | #if 0 /* never used */ |
| 87 | static int send_mem_chars(void *arg, const void *buf, int len) | 86 | static int |
| 87 | send_mem_chars(void *arg, const void *buf, int len) | ||
| 88 | { | 88 | { |
| 89 | unsigned char **out = arg; | 89 | unsigned char **out = arg; |
| 90 | if(!out) return 1; | 90 | |
| 91 | if (!out) | ||
| 92 | return 1; | ||
| 91 | memcpy(*out, buf, len); | 93 | memcpy(*out, buf, len); |
| 92 | *out += len; | 94 | *out += len; |
| 93 | return 1; | 95 | return 1; |
| 94 | } | 96 | } |
| 95 | #endif | 97 | #endif |
| 96 | 98 | ||
| 97 | static int send_bio_chars(void *arg, const void *buf, int len) | 99 | static int |
| 100 | send_bio_chars(void *arg, const void *buf, int len) | ||
| 98 | { | 101 | { |
| 99 | if(!arg) return 1; | 102 | if (!arg) |
| 100 | if(BIO_write(arg, buf, len) != len) return 0; | 103 | return 1; |
| 104 | if (BIO_write(arg, buf, len) != len) | ||
| 105 | return 0; | ||
| 101 | return 1; | 106 | return 1; |
| 102 | } | 107 | } |
| 103 | 108 | ||
| 104 | static int send_fp_chars(void *arg, const void *buf, int len) | 109 | static int |
| 110 | send_fp_chars(void *arg, const void *buf, int len) | ||
| 105 | { | 111 | { |
| 106 | if(!arg) return 1; | 112 | if (!arg) |
| 107 | if(fwrite(buf, 1, len, arg) != (unsigned int)len) return 0; | 113 | return 1; |
| 114 | if (fwrite(buf, 1, len, arg) != (unsigned int)len) | ||
| 115 | return 0; | ||
| 108 | return 1; | 116 | return 1; |
| 109 | } | 117 | } |
| 110 | 118 | ||
| @@ -117,50 +125,63 @@ typedef int char_io(void *arg, const void *buf, int len); | |||
| 117 | * 4 byte forms. | 125 | * 4 byte forms. |
| 118 | */ | 126 | */ |
| 119 | 127 | ||
| 120 | static int do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, char_io *io_ch, void *arg) | 128 | static int |
| 129 | do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, | ||
| 130 | char_io *io_ch, void *arg) | ||
| 121 | { | 131 | { |
| 122 | unsigned char chflgs, chtmp; | 132 | unsigned char chflgs, chtmp; |
| 123 | char tmphex[HEX_SIZE(long)+3]; | 133 | char tmphex[HEX_SIZE(long) + 3]; |
| 124 | 134 | ||
| 125 | if(c > 0xffffffffL) | 135 | if (c > 0xffffffffL) |
| 126 | return -1; | 136 | return -1; |
| 127 | if(c > 0xffff) { | 137 | if (c > 0xffff) { |
| 128 | snprintf(tmphex, sizeof tmphex, "\\W%08lX", c); | 138 | snprintf(tmphex, sizeof tmphex, "\\W%08lX", c); |
| 129 | if(!io_ch(arg, tmphex, 10)) return -1; | 139 | if (!io_ch(arg, tmphex, 10)) |
| 140 | return -1; | ||
| 130 | return 10; | 141 | return 10; |
| 131 | } | 142 | } |
| 132 | if(c > 0xff) { | 143 | if (c > 0xff) { |
| 133 | snprintf(tmphex, sizeof tmphex, "\\U%04lX", c); | 144 | snprintf(tmphex, sizeof tmphex, "\\U%04lX", c); |
| 134 | if(!io_ch(arg, tmphex, 6)) return -1; | 145 | if (!io_ch(arg, tmphex, 6)) |
| 146 | return -1; | ||
| 135 | return 6; | 147 | return 6; |
| 136 | } | 148 | } |
| 137 | chtmp = (unsigned char)c; | 149 | chtmp = (unsigned char)c; |
| 138 | if(chtmp > 0x7f) chflgs = flags & ASN1_STRFLGS_ESC_MSB; | 150 | if (chtmp > 0x7f) |
| 139 | else chflgs = char_type[chtmp] & flags; | 151 | chflgs = flags & ASN1_STRFLGS_ESC_MSB; |
| 140 | if(chflgs & CHARTYPE_BS_ESC) { | 152 | else |
| 153 | chflgs = char_type[chtmp] & flags; | ||
| 154 | if (chflgs & CHARTYPE_BS_ESC) { | ||
| 141 | /* If we don't escape with quotes, signal we need quotes */ | 155 | /* If we don't escape with quotes, signal we need quotes */ |
| 142 | if(chflgs & ASN1_STRFLGS_ESC_QUOTE) { | 156 | if (chflgs & ASN1_STRFLGS_ESC_QUOTE) { |
| 143 | if(do_quotes) *do_quotes = 1; | 157 | if (do_quotes) |
| 144 | if(!io_ch(arg, &chtmp, 1)) return -1; | 158 | *do_quotes = 1; |
| 159 | if (!io_ch(arg, &chtmp, 1)) | ||
| 160 | return -1; | ||
| 145 | return 1; | 161 | return 1; |
| 146 | } | 162 | } |
| 147 | if(!io_ch(arg, "\\", 1)) return -1; | 163 | if (!io_ch(arg, "\\", 1)) |
| 148 | if(!io_ch(arg, &chtmp, 1)) return -1; | 164 | return -1; |
| 165 | if (!io_ch(arg, &chtmp, 1)) | ||
| 166 | return -1; | ||
| 149 | return 2; | 167 | return 2; |
| 150 | } | 168 | } |
| 151 | if(chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) { | 169 | if (chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) { |
| 152 | snprintf(tmphex, sizeof tmphex, "\\%02X", chtmp); | 170 | snprintf(tmphex, sizeof tmphex, "\\%02X", chtmp); |
| 153 | if(!io_ch(arg, tmphex, 3)) return -1; | 171 | if (!io_ch(arg, tmphex, 3)) |
| 172 | return -1; | ||
| 154 | return 3; | 173 | return 3; |
| 155 | } | 174 | } |
| 156 | /* If we get this far and do any escaping at all must escape | 175 | /* If we get this far and do any escaping at all must escape |
| 157 | * the escape character itself: backslash. | 176 | * the escape character itself: backslash. |
| 158 | */ | 177 | */ |
| 159 | if (chtmp == '\\' && flags & ESC_FLAGS) { | 178 | if (chtmp == '\\' && flags & ESC_FLAGS) { |
| 160 | if(!io_ch(arg, "\\\\", 2)) return -1; | 179 | if (!io_ch(arg, "\\\\", 2)) |
| 180 | return -1; | ||
| 161 | return 2; | 181 | return 2; |
| 162 | } | 182 | } |
| 163 | if(!io_ch(arg, &chtmp, 1)) return -1; | 183 | if (!io_ch(arg, &chtmp, 1)) |
| 184 | return -1; | ||
| 164 | return 1; | 185 | return 1; |
| 165 | } | 186 | } |
| 166 | 187 | ||
| @@ -172,61 +193,72 @@ static int do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, ch | |||
| 172 | * and converts to or from UTF8 as appropriate. | 193 | * and converts to or from UTF8 as appropriate. |
| 173 | */ | 194 | */ |
| 174 | 195 | ||
| 175 | static int do_buf(unsigned char *buf, int buflen, | 196 | static int |
| 176 | int type, unsigned char flags, char *quotes, char_io *io_ch, void *arg) | 197 | do_buf(unsigned char *buf, int buflen, int type, unsigned char flags, |
| 198 | char *quotes, char_io *io_ch, void *arg) | ||
| 177 | { | 199 | { |
| 178 | int i, outlen, len; | 200 | int i, outlen, len; |
| 179 | unsigned char orflags, *p, *q; | 201 | unsigned char orflags, *p, *q; |
| 180 | unsigned long c; | 202 | unsigned long c; |
| 203 | |||
| 181 | p = buf; | 204 | p = buf; |
| 182 | q = buf + buflen; | 205 | q = buf + buflen; |
| 183 | outlen = 0; | 206 | outlen = 0; |
| 184 | while(p != q) { | 207 | while (p != q) { |
| 185 | if(p == buf && flags & ASN1_STRFLGS_ESC_2253) orflags = CHARTYPE_FIRST_ESC_2253; | 208 | if (p == buf && flags & ASN1_STRFLGS_ESC_2253) |
| 186 | else orflags = 0; | 209 | orflags = CHARTYPE_FIRST_ESC_2253; |
| 187 | switch(type & BUF_TYPE_WIDTH_MASK) { | 210 | else |
| 188 | case 4: | 211 | orflags = 0; |
| 212 | switch (type & BUF_TYPE_WIDTH_MASK) { | ||
| 213 | case 4: | ||
| 189 | c = ((unsigned long)*p++) << 24; | 214 | c = ((unsigned long)*p++) << 24; |
| 190 | c |= ((unsigned long)*p++) << 16; | 215 | c |= ((unsigned long)*p++) << 16; |
| 191 | c |= ((unsigned long)*p++) << 8; | 216 | c |= ((unsigned long)*p++) << 8; |
| 192 | c |= *p++; | 217 | c |= *p++; |
| 193 | break; | 218 | break; |
| 194 | 219 | ||
| 195 | case 2: | 220 | case 2: |
| 196 | c = ((unsigned long)*p++) << 8; | 221 | c = ((unsigned long)*p++) << 8; |
| 197 | c |= *p++; | 222 | c |= *p++; |
| 198 | break; | 223 | break; |
| 199 | 224 | ||
| 200 | case 1: | 225 | case 1: |
| 201 | c = *p++; | 226 | c = *p++; |
| 202 | break; | 227 | break; |
| 203 | 228 | ||
| 204 | case 0: | 229 | case 0: |
| 205 | i = UTF8_getc(p, buflen, &c); | 230 | i = UTF8_getc(p, buflen, &c); |
| 206 | if(i < 0) return -1; /* Invalid UTF8String */ | 231 | if (i < 0) |
| 232 | return -1; /* Invalid UTF8String */ | ||
| 207 | p += i; | 233 | p += i; |
| 208 | break; | 234 | break; |
| 209 | default: | 235 | default: |
| 210 | return -1; /* invalid width */ | 236 | return -1; /* invalid width */ |
| 211 | } | 237 | } |
| 212 | if (p == q && flags & ASN1_STRFLGS_ESC_2253) orflags = CHARTYPE_LAST_ESC_2253; | 238 | if (p == q && flags & ASN1_STRFLGS_ESC_2253) |
| 213 | if(type & BUF_TYPE_CONVUTF8) { | 239 | orflags = CHARTYPE_LAST_ESC_2253; |
| 240 | if (type & BUF_TYPE_CONVUTF8) { | ||
| 214 | unsigned char utfbuf[6]; | 241 | unsigned char utfbuf[6]; |
| 215 | int utflen; | 242 | int utflen; |
| 216 | utflen = UTF8_putc(utfbuf, sizeof utfbuf, c); | 243 | utflen = UTF8_putc(utfbuf, sizeof utfbuf, c); |
| 217 | for(i = 0; i < utflen; i++) { | 244 | for (i = 0; i < utflen; i++) { |
| 218 | /* We don't need to worry about setting orflags correctly | 245 | /* We don't need to worry about setting orflags correctly |
| 219 | * because if utflen==1 its value will be correct anyway | 246 | * because if utflen==1 its value will be correct anyway |
| 220 | * otherwise each character will be > 0x7f and so the | 247 | * otherwise each character will be > 0x7f and so the |
| 221 | * character will never be escaped on first and last. | 248 | * character will never be escaped on first and last. |
| 222 | */ | 249 | */ |
| 223 | len = do_esc_char(utfbuf[i], (unsigned char)(flags | orflags), quotes, io_ch, arg); | 250 | len = do_esc_char(utfbuf[i], |
| 224 | if(len < 0) return -1; | 251 | (unsigned char)(flags | orflags), quotes, |
| 252 | io_ch, arg); | ||
| 253 | if (len < 0) | ||
| 254 | return -1; | ||
| 225 | outlen += len; | 255 | outlen += len; |
| 226 | } | 256 | } |
| 227 | } else { | 257 | } else { |
| 228 | len = do_esc_char(c, (unsigned char)(flags | orflags), quotes, io_ch, arg); | 258 | len = do_esc_char(c, (unsigned char)(flags | orflags), |
| 229 | if(len < 0) return -1; | 259 | quotes, io_ch, arg); |
| 260 | if (len < 0) | ||
| 261 | return -1; | ||
| 230 | outlen += len; | 262 | outlen += len; |
| 231 | } | 263 | } |
| 232 | } | 264 | } |
| @@ -235,18 +267,20 @@ static int do_buf(unsigned char *buf, int buflen, | |||
| 235 | 267 | ||
| 236 | /* This function hex dumps a buffer of characters */ | 268 | /* This function hex dumps a buffer of characters */ |
| 237 | 269 | ||
| 238 | static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, int buflen) | 270 | static int |
| 271 | do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, int buflen) | ||
| 239 | { | 272 | { |
| 240 | static const char hexdig[] = "0123456789ABCDEF"; | 273 | static const char hexdig[] = "0123456789ABCDEF"; |
| 241 | unsigned char *p, *q; | 274 | unsigned char *p, *q; |
| 242 | char hextmp[2]; | 275 | char hextmp[2]; |
| 243 | if(arg) { | 276 | if (arg) { |
| 244 | p = buf; | 277 | p = buf; |
| 245 | q = buf + buflen; | 278 | q = buf + buflen; |
| 246 | while(p != q) { | 279 | while (p != q) { |
| 247 | hextmp[0] = hexdig[*p >> 4]; | 280 | hextmp[0] = hexdig[*p >> 4]; |
| 248 | hextmp[1] = hexdig[*p & 0xf]; | 281 | hextmp[1] = hexdig[*p & 0xf]; |
| 249 | if(!io_ch(arg, hextmp, 2)) return -1; | 282 | if (!io_ch(arg, hextmp, 2)) |
| 283 | return -1; | ||
| 250 | p++; | 284 | p++; |
| 251 | } | 285 | } |
| 252 | } | 286 | } |
| @@ -259,7 +293,8 @@ static int do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, int buflen | |||
| 259 | * #01234 format. | 293 | * #01234 format. |
| 260 | */ | 294 | */ |
| 261 | 295 | ||
| 262 | static int do_dump(unsigned long lflags, char_io *io_ch, void *arg, ASN1_STRING *str) | 296 | static int |
| 297 | do_dump(unsigned long lflags, char_io *io_ch, void *arg, ASN1_STRING *str) | ||
| 263 | { | 298 | { |
| 264 | /* Placing the ASN1_STRING in a temp ASN1_TYPE allows | 299 | /* Placing the ASN1_STRING in a temp ASN1_TYPE allows |
| 265 | * the DER encoding to readily obtained | 300 | * the DER encoding to readily obtained |
| @@ -268,23 +303,27 @@ static int do_dump(unsigned long lflags, char_io *io_ch, void *arg, ASN1_STRING | |||
| 268 | unsigned char *der_buf, *p; | 303 | unsigned char *der_buf, *p; |
| 269 | int outlen, der_len; | 304 | int outlen, der_len; |
| 270 | 305 | ||
| 271 | if(!io_ch(arg, "#", 1)) return -1; | 306 | if (!io_ch(arg, "#", 1)) |
| 307 | return -1; | ||
| 272 | /* If we don't dump DER encoding just dump content octets */ | 308 | /* If we don't dump DER encoding just dump content octets */ |
| 273 | if(!(lflags & ASN1_STRFLGS_DUMP_DER)) { | 309 | if (!(lflags & ASN1_STRFLGS_DUMP_DER)) { |
| 274 | outlen = do_hex_dump(io_ch, arg, str->data, str->length); | 310 | outlen = do_hex_dump(io_ch, arg, str->data, str->length); |
| 275 | if(outlen < 0) return -1; | 311 | if (outlen < 0) |
| 312 | return -1; | ||
| 276 | return outlen + 1; | 313 | return outlen + 1; |
| 277 | } | 314 | } |
| 278 | t.type = str->type; | 315 | t.type = str->type; |
| 279 | t.value.ptr = (char *)str; | 316 | t.value.ptr = (char *)str; |
| 280 | der_len = i2d_ASN1_TYPE(&t, NULL); | 317 | der_len = i2d_ASN1_TYPE(&t, NULL); |
| 281 | der_buf = malloc(der_len); | 318 | der_buf = malloc(der_len); |
| 282 | if(!der_buf) return -1; | 319 | if (!der_buf) |
| 320 | return -1; | ||
| 283 | p = der_buf; | 321 | p = der_buf; |
| 284 | i2d_ASN1_TYPE(&t, &p); | 322 | i2d_ASN1_TYPE(&t, &p); |
| 285 | outlen = do_hex_dump(io_ch, arg, der_buf, der_len); | 323 | outlen = do_hex_dump(io_ch, arg, der_buf, der_len); |
| 286 | free(der_buf); | 324 | free(der_buf); |
| 287 | if(outlen < 0) return -1; | 325 | if (outlen < 0) |
| 326 | return -1; | ||
| 288 | return outlen + 1; | 327 | return outlen + 1; |
| 289 | } | 328 | } |
| 290 | 329 | ||
| @@ -311,86 +350,102 @@ static const signed char tag2nbyte[] = { | |||
| 311 | * occurred. | 350 | * occurred. |
| 312 | */ | 351 | */ |
| 313 | 352 | ||
| 314 | static int do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, ASN1_STRING *str) | 353 | static int |
| 354 | do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, ASN1_STRING *str) | ||
| 315 | { | 355 | { |
| 316 | int outlen, len; | 356 | int outlen, len; |
| 317 | int type; | 357 | int type; |
| 318 | char quotes; | 358 | char quotes; |
| 319 | unsigned char flags; | 359 | unsigned char flags; |
| 360 | |||
| 320 | quotes = 0; | 361 | quotes = 0; |
| 321 | /* Keep a copy of escape flags */ | 362 | /* Keep a copy of escape flags */ |
| 322 | flags = (unsigned char)(lflags & ESC_FLAGS); | 363 | flags = (unsigned char)(lflags & ESC_FLAGS); |
| 323 | |||
| 324 | type = str->type; | 364 | type = str->type; |
| 325 | |||
| 326 | outlen = 0; | 365 | outlen = 0; |
| 327 | 366 | ||
| 328 | 367 | if (lflags & ASN1_STRFLGS_SHOW_TYPE) { | |
| 329 | if(lflags & ASN1_STRFLGS_SHOW_TYPE) { | ||
| 330 | const char *tagname; | 368 | const char *tagname; |
| 331 | tagname = ASN1_tag2str(type); | 369 | tagname = ASN1_tag2str(type); |
| 332 | outlen += strlen(tagname); | 370 | outlen += strlen(tagname); |
| 333 | if(!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1)) return -1; | 371 | if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1)) |
| 372 | return -1; | ||
| 334 | outlen++; | 373 | outlen++; |
| 335 | } | 374 | } |
| 336 | 375 | ||
| 337 | /* Decide what to do with type, either dump content or display it */ | 376 | /* Decide what to do with type, either dump content or display it */ |
| 338 | 377 | ||
| 339 | /* Dump everything */ | 378 | /* Dump everything */ |
| 340 | if(lflags & ASN1_STRFLGS_DUMP_ALL) type = -1; | 379 | if (lflags & ASN1_STRFLGS_DUMP_ALL) |
| 380 | type = -1; | ||
| 341 | /* Ignore the string type */ | 381 | /* Ignore the string type */ |
| 342 | else if(lflags & ASN1_STRFLGS_IGNORE_TYPE) type = 1; | 382 | else if (lflags & ASN1_STRFLGS_IGNORE_TYPE) |
| 383 | type = 1; | ||
| 343 | else { | 384 | else { |
| 344 | /* Else determine width based on type */ | 385 | /* Else determine width based on type */ |
| 345 | if((type > 0) && (type < 31)) type = tag2nbyte[type]; | 386 | if ((type > 0) && (type < 31)) |
| 346 | else type = -1; | 387 | type = tag2nbyte[type]; |
| 347 | if((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN)) type = 1; | 388 | else |
| 389 | type = -1; | ||
| 390 | if ((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN)) | ||
| 391 | type = 1; | ||
| 348 | } | 392 | } |
| 349 | 393 | ||
| 350 | if(type == -1) { | 394 | if (type == -1) { |
| 351 | len = do_dump(lflags, io_ch, arg, str); | 395 | len = do_dump(lflags, io_ch, arg, str); |
| 352 | if(len < 0) return -1; | 396 | if (len < 0) |
| 397 | return -1; | ||
| 353 | outlen += len; | 398 | outlen += len; |
| 354 | return outlen; | 399 | return outlen; |
| 355 | } | 400 | } |
| 356 | 401 | ||
| 357 | if(lflags & ASN1_STRFLGS_UTF8_CONVERT) { | 402 | if (lflags & ASN1_STRFLGS_UTF8_CONVERT) { |
| 358 | /* Note: if string is UTF8 and we want | 403 | /* Note: if string is UTF8 and we want |
| 359 | * to convert to UTF8 then we just interpret | 404 | * to convert to UTF8 then we just interpret |
| 360 | * it as 1 byte per character to avoid converting | 405 | * it as 1 byte per character to avoid converting |
| 361 | * twice. | 406 | * twice. |
| 362 | */ | 407 | */ |
| 363 | if(!type) type = 1; | 408 | if (!type) |
| 364 | else type |= BUF_TYPE_CONVUTF8; | 409 | type = 1; |
| 410 | else | ||
| 411 | type |= BUF_TYPE_CONVUTF8; | ||
| 365 | } | 412 | } |
| 366 | 413 | ||
| 367 | len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL); | 414 | len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL); |
| 368 | if(len < 0) return -1; | 415 | if (len < 0) |
| 416 | return -1; | ||
| 369 | outlen += len; | 417 | outlen += len; |
| 370 | if(quotes) outlen += 2; | 418 | if (quotes) |
| 371 | if(!arg) return outlen; | 419 | outlen += 2; |
| 372 | if(quotes && !io_ch(arg, "\"", 1)) return -1; | 420 | if (!arg) |
| 373 | if(do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0) | 421 | return outlen; |
| 422 | if (quotes && !io_ch(arg, "\"", 1)) | ||
| 423 | return -1; | ||
| 424 | if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0) | ||
| 425 | return -1; | ||
| 426 | if (quotes && !io_ch(arg, "\"", 1)) | ||
| 374 | return -1; | 427 | return -1; |
| 375 | if(quotes && !io_ch(arg, "\"", 1)) return -1; | ||
| 376 | return outlen; | 428 | return outlen; |
| 377 | } | 429 | } |
| 378 | 430 | ||
| 379 | /* Used for line indenting: print 'indent' spaces */ | 431 | /* Used for line indenting: print 'indent' spaces */ |
| 380 | 432 | ||
| 381 | static int do_indent(char_io *io_ch, void *arg, int indent) | 433 | static int |
| 434 | do_indent(char_io *io_ch, void *arg, int indent) | ||
| 382 | { | 435 | { |
| 383 | int i; | 436 | int i; |
| 384 | for(i = 0; i < indent; i++) | 437 | for (i = 0; i < indent; i++) |
| 385 | if(!io_ch(arg, " ", 1)) return 0; | 438 | if (!io_ch(arg, " ", 1)) |
| 439 | return 0; | ||
| 386 | return 1; | 440 | return 1; |
| 387 | } | 441 | } |
| 388 | 442 | ||
| 389 | #define FN_WIDTH_LN 25 | 443 | #define FN_WIDTH_LN 25 |
| 390 | #define FN_WIDTH_SN 10 | 444 | #define FN_WIDTH_SN 10 |
| 391 | 445 | ||
| 392 | static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n, | 446 | static int |
| 393 | int indent, unsigned long flags) | 447 | do_name_ex(char_io *io_ch, void *arg, X509_NAME *n, int indent, |
| 448 | unsigned long flags) | ||
| 394 | { | 449 | { |
| 395 | int i, prev = -1, orflags, cnt; | 450 | int i, prev = -1, orflags, cnt; |
| 396 | int fn_opt, fn_nid; | 451 | int fn_opt, fn_nid; |
| @@ -402,18 +457,22 @@ static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n, | |||
| 402 | int outlen, len; | 457 | int outlen, len; |
| 403 | char *sep_dn, *sep_mv, *sep_eq; | 458 | char *sep_dn, *sep_mv, *sep_eq; |
| 404 | int sep_dn_len, sep_mv_len, sep_eq_len; | 459 | int sep_dn_len, sep_mv_len, sep_eq_len; |
| 405 | if(indent < 0) indent = 0; | 460 | |
| 461 | if (indent < 0) | ||
| 462 | indent = 0; | ||
| 406 | outlen = indent; | 463 | outlen = indent; |
| 407 | if(!do_indent(io_ch, arg, indent)) return -1; | 464 | if (!do_indent(io_ch, arg, indent)) |
| 465 | return -1; | ||
| 466 | |||
| 408 | switch (flags & XN_FLAG_SEP_MASK) { | 467 | switch (flags & XN_FLAG_SEP_MASK) { |
| 409 | case XN_FLAG_SEP_MULTILINE: | 468 | case XN_FLAG_SEP_MULTILINE: |
| 410 | sep_dn = "\n"; | 469 | sep_dn = "\n"; |
| 411 | sep_dn_len = 1; | 470 | sep_dn_len = 1; |
| 412 | sep_mv = " + "; | 471 | sep_mv = " + "; |
| 413 | sep_mv_len = 3; | 472 | sep_mv_len = 3; |
| 414 | break; | 473 | break; |
| 415 | 474 | ||
| 416 | case XN_FLAG_SEP_COMMA_PLUS: | 475 | case XN_FLAG_SEP_COMMA_PLUS: |
| 417 | sep_dn = ","; | 476 | sep_dn = ","; |
| 418 | sep_dn_len = 1; | 477 | sep_dn_len = 1; |
| 419 | sep_mv = "+"; | 478 | sep_mv = "+"; |
| @@ -421,7 +480,7 @@ static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n, | |||
| 421 | indent = 0; | 480 | indent = 0; |
| 422 | break; | 481 | break; |
| 423 | 482 | ||
| 424 | case XN_FLAG_SEP_CPLUS_SPC: | 483 | case XN_FLAG_SEP_CPLUS_SPC: |
| 425 | sep_dn = ", "; | 484 | sep_dn = ", "; |
| 426 | sep_dn_len = 2; | 485 | sep_dn_len = 2; |
| 427 | sep_mv = " + "; | 486 | sep_mv = " + "; |
| @@ -429,7 +488,7 @@ static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n, | |||
| 429 | indent = 0; | 488 | indent = 0; |
| 430 | break; | 489 | break; |
| 431 | 490 | ||
| 432 | case XN_FLAG_SEP_SPLUS_SPC: | 491 | case XN_FLAG_SEP_SPLUS_SPC: |
| 433 | sep_dn = "; "; | 492 | sep_dn = "; "; |
| 434 | sep_dn_len = 2; | 493 | sep_dn_len = 2; |
| 435 | sep_mv = " + "; | 494 | sep_mv = " + "; |
| @@ -437,11 +496,11 @@ static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n, | |||
| 437 | indent = 0; | 496 | indent = 0; |
| 438 | break; | 497 | break; |
| 439 | 498 | ||
| 440 | default: | 499 | default: |
| 441 | return -1; | 500 | return -1; |
| 442 | } | 501 | } |
| 443 | 502 | ||
| 444 | if(flags & XN_FLAG_SPC_EQ) { | 503 | if (flags & XN_FLAG_SPC_EQ) { |
| 445 | sep_eq = " = "; | 504 | sep_eq = " = "; |
| 446 | sep_eq_len = 3; | 505 | sep_eq_len = 3; |
| 447 | } else { | 506 | } else { |
| @@ -451,19 +510,23 @@ static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n, | |||
| 451 | 510 | ||
| 452 | fn_opt = flags & XN_FLAG_FN_MASK; | 511 | fn_opt = flags & XN_FLAG_FN_MASK; |
| 453 | 512 | ||
| 454 | cnt = X509_NAME_entry_count(n); | 513 | cnt = X509_NAME_entry_count(n); |
| 455 | for(i = 0; i < cnt; i++) { | 514 | for (i = 0; i < cnt; i++) { |
| 456 | if(flags & XN_FLAG_DN_REV) | 515 | if (flags & XN_FLAG_DN_REV) |
| 457 | ent = X509_NAME_get_entry(n, cnt - i - 1); | 516 | ent = X509_NAME_get_entry(n, cnt - i - 1); |
| 458 | else ent = X509_NAME_get_entry(n, i); | 517 | else |
| 459 | if(prev != -1) { | 518 | ent = X509_NAME_get_entry(n, i); |
| 460 | if(prev == ent->set) { | 519 | if (prev != -1) { |
| 461 | if(!io_ch(arg, sep_mv, sep_mv_len)) return -1; | 520 | if (prev == ent->set) { |
| 521 | if (!io_ch(arg, sep_mv, sep_mv_len)) | ||
| 522 | return -1; | ||
| 462 | outlen += sep_mv_len; | 523 | outlen += sep_mv_len; |
| 463 | } else { | 524 | } else { |
| 464 | if(!io_ch(arg, sep_dn, sep_dn_len)) return -1; | 525 | if (!io_ch(arg, sep_dn, sep_dn_len)) |
| 526 | return -1; | ||
| 465 | outlen += sep_dn_len; | 527 | outlen += sep_dn_len; |
| 466 | if(!do_indent(io_ch, arg, indent)) return -1; | 528 | if (!do_indent(io_ch, arg, indent)) |
| 529 | return -1; | ||
| 467 | outlen += indent; | 530 | outlen += indent; |
| 468 | } | 531 | } |
| 469 | } | 532 | } |
| @@ -471,17 +534,18 @@ static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n, | |||
| 471 | fn = X509_NAME_ENTRY_get_object(ent); | 534 | fn = X509_NAME_ENTRY_get_object(ent); |
| 472 | val = X509_NAME_ENTRY_get_data(ent); | 535 | val = X509_NAME_ENTRY_get_data(ent); |
| 473 | fn_nid = OBJ_obj2nid(fn); | 536 | fn_nid = OBJ_obj2nid(fn); |
| 474 | if(fn_opt != XN_FLAG_FN_NONE) { | 537 | if (fn_opt != XN_FLAG_FN_NONE) { |
| 475 | int objlen, fld_len; | 538 | int objlen, fld_len; |
| 476 | if((fn_opt == XN_FLAG_FN_OID) || (fn_nid==NID_undef) ) { | 539 | if ((fn_opt == XN_FLAG_FN_OID) || |
| 540 | (fn_nid == NID_undef)) { | ||
| 477 | OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1); | 541 | OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1); |
| 478 | fld_len = 0; /* XXX: what should this be? */ | 542 | fld_len = 0; /* XXX: what should this be? */ |
| 479 | objbuf = objtmp; | 543 | objbuf = objtmp; |
| 480 | } else { | 544 | } else { |
| 481 | if(fn_opt == XN_FLAG_FN_SN) { | 545 | if (fn_opt == XN_FLAG_FN_SN) { |
| 482 | fld_len = FN_WIDTH_SN; | 546 | fld_len = FN_WIDTH_SN; |
| 483 | objbuf = OBJ_nid2sn(fn_nid); | 547 | objbuf = OBJ_nid2sn(fn_nid); |
| 484 | } else if(fn_opt == XN_FLAG_FN_LN) { | 548 | } else if (fn_opt == XN_FLAG_FN_LN) { |
| 485 | fld_len = FN_WIDTH_LN; | 549 | fld_len = FN_WIDTH_LN; |
| 486 | objbuf = OBJ_nid2ln(fn_nid); | 550 | objbuf = OBJ_nid2ln(fn_nid); |
| 487 | } else { | 551 | } else { |
| @@ -490,24 +554,30 @@ static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n, | |||
| 490 | } | 554 | } |
| 491 | } | 555 | } |
| 492 | objlen = strlen(objbuf); | 556 | objlen = strlen(objbuf); |
| 493 | if(!io_ch(arg, objbuf, objlen)) return -1; | 557 | if (!io_ch(arg, objbuf, objlen)) |
| 558 | return -1; | ||
| 494 | if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) { | 559 | if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) { |
| 495 | if (!do_indent(io_ch, arg, fld_len - objlen)) return -1; | 560 | if (!do_indent(io_ch, arg, fld_len - objlen)) |
| 561 | return -1; | ||
| 496 | outlen += fld_len - objlen; | 562 | outlen += fld_len - objlen; |
| 497 | } | 563 | } |
| 498 | if(!io_ch(arg, sep_eq, sep_eq_len)) return -1; | 564 | if (!io_ch(arg, sep_eq, sep_eq_len)) |
| 565 | return -1; | ||
| 499 | outlen += objlen + sep_eq_len; | 566 | outlen += objlen + sep_eq_len; |
| 500 | } | 567 | } |
| 501 | /* If the field name is unknown then fix up the DER dump | 568 | /* If the field name is unknown then fix up the DER dump |
| 502 | * flag. We might want to limit this further so it will | 569 | * flag. We might want to limit this further so it will |
| 503 | * DER dump on anything other than a few 'standard' fields. | 570 | * DER dump on anything other than a few 'standard' fields. |
| 504 | */ | 571 | */ |
| 505 | if((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS)) | 572 | if ((fn_nid == NID_undef) && |
| 506 | orflags = ASN1_STRFLGS_DUMP_ALL; | 573 | (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS)) |
| 507 | else orflags = 0; | 574 | orflags = ASN1_STRFLGS_DUMP_ALL; |
| 508 | 575 | else | |
| 576 | orflags = 0; | ||
| 577 | |||
| 509 | len = do_print_ex(io_ch, arg, flags | orflags, val); | 578 | len = do_print_ex(io_ch, arg, flags | orflags, val); |
| 510 | if(len < 0) return -1; | 579 | if (len < 0) |
| 580 | return -1; | ||
| 511 | outlen += len; | 581 | outlen += len; |
| 512 | } | 582 | } |
| 513 | return outlen; | 583 | return outlen; |
| @@ -515,36 +585,41 @@ static int do_name_ex(char_io *io_ch, void *arg, X509_NAME *n, | |||
| 515 | 585 | ||
| 516 | /* Wrappers round the main functions */ | 586 | /* Wrappers round the main functions */ |
| 517 | 587 | ||
| 518 | int X509_NAME_print_ex(BIO *out, X509_NAME *nm, int indent, unsigned long flags) | 588 | int |
| 589 | X509_NAME_print_ex(BIO *out, X509_NAME *nm, int indent, unsigned long flags) | ||
| 519 | { | 590 | { |
| 520 | if(flags == XN_FLAG_COMPAT) | 591 | if (flags == XN_FLAG_COMPAT) |
| 521 | return X509_NAME_print(out, nm, indent); | 592 | return X509_NAME_print(out, nm, indent); |
| 522 | return do_name_ex(send_bio_chars, out, nm, indent, flags); | 593 | return do_name_ex(send_bio_chars, out, nm, indent, flags); |
| 523 | } | 594 | } |
| 524 | 595 | ||
| 525 | #ifndef OPENSSL_NO_FP_API | 596 | #ifndef OPENSSL_NO_FP_API |
| 526 | int X509_NAME_print_ex_fp(FILE *fp, X509_NAME *nm, int indent, unsigned long flags) | 597 | int |
| 598 | X509_NAME_print_ex_fp(FILE *fp, X509_NAME *nm, int indent, unsigned long flags) | ||
| 527 | { | 599 | { |
| 528 | if(flags == XN_FLAG_COMPAT) { | 600 | if (flags == XN_FLAG_COMPAT) { |
| 529 | BIO *btmp; | 601 | BIO *btmp; |
| 530 | int ret; | 602 | int ret; |
| 531 | btmp = BIO_new_fp(fp, BIO_NOCLOSE); | 603 | btmp = BIO_new_fp(fp, BIO_NOCLOSE); |
| 532 | if(!btmp) return -1; | 604 | if (!btmp) |
| 605 | return -1; | ||
| 533 | ret = X509_NAME_print(btmp, nm, indent); | 606 | ret = X509_NAME_print(btmp, nm, indent); |
| 534 | BIO_free(btmp); | 607 | BIO_free(btmp); |
| 535 | return ret; | 608 | return ret; |
| 536 | } | 609 | } |
| 537 | return do_name_ex(send_fp_chars, fp, nm, indent, flags); | 610 | return do_name_ex(send_fp_chars, fp, nm, indent, flags); |
| 538 | } | 611 | } |
| 539 | #endif | 612 | #endif |
| 540 | 613 | ||
| 541 | int ASN1_STRING_print_ex(BIO *out, ASN1_STRING *str, unsigned long flags) | 614 | int |
| 615 | ASN1_STRING_print_ex(BIO *out, ASN1_STRING *str, unsigned long flags) | ||
| 542 | { | 616 | { |
| 543 | return do_print_ex(send_bio_chars, out, flags, str); | 617 | return do_print_ex(send_bio_chars, out, flags, str); |
| 544 | } | 618 | } |
| 545 | 619 | ||
| 546 | #ifndef OPENSSL_NO_FP_API | 620 | #ifndef OPENSSL_NO_FP_API |
| 547 | int ASN1_STRING_print_ex_fp(FILE *fp, ASN1_STRING *str, unsigned long flags) | 621 | int |
| 622 | ASN1_STRING_print_ex_fp(FILE *fp, ASN1_STRING *str, unsigned long flags) | ||
| 548 | { | 623 | { |
| 549 | return do_print_ex(send_fp_chars, fp, flags, str); | 624 | return do_print_ex(send_fp_chars, fp, flags, str); |
| 550 | } | 625 | } |
| @@ -554,20 +629,27 @@ int ASN1_STRING_print_ex_fp(FILE *fp, ASN1_STRING *str, unsigned long flags) | |||
| 554 | * in output string or a negative error code | 629 | * in output string or a negative error code |
| 555 | */ | 630 | */ |
| 556 | 631 | ||
| 557 | int ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in) | 632 | int |
| 633 | ASN1_STRING_to_UTF8(unsigned char **out, ASN1_STRING *in) | ||
| 558 | { | 634 | { |
| 559 | ASN1_STRING stmp, *str = &stmp; | 635 | ASN1_STRING stmp, *str = &stmp; |
| 560 | int mbflag, type, ret; | 636 | int mbflag, type, ret; |
| 561 | if(!in) return -1; | 637 | |
| 638 | if (!in) | ||
| 639 | return -1; | ||
| 562 | type = in->type; | 640 | type = in->type; |
| 563 | if((type < 0) || (type > 30)) return -1; | 641 | if ((type < 0) || (type > 30)) |
| 642 | return -1; | ||
| 564 | mbflag = tag2nbyte[type]; | 643 | mbflag = tag2nbyte[type]; |
| 565 | if(mbflag == -1) return -1; | 644 | if (mbflag == -1) |
| 645 | return -1; | ||
| 566 | mbflag |= MBSTRING_FLAG; | 646 | mbflag |= MBSTRING_FLAG; |
| 567 | stmp.data = NULL; | 647 | stmp.data = NULL; |
| 568 | stmp.length = 0; | 648 | stmp.length = 0; |
| 569 | ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag, B_ASN1_UTF8STRING); | 649 | ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag, |
| 570 | if(ret < 0) return ret; | 650 | B_ASN1_UTF8STRING); |
| 651 | if (ret < 0) | ||
| 652 | return ret; | ||
| 571 | *out = stmp.data; | 653 | *out = stmp.data; |
| 572 | return stmp.length; | 654 | return stmp.length; |
| 573 | } | 655 | } |
