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