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