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