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