/src/libressl/crypto/asn1/a_strex.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: a_strex.c,v 1.31 2021/12/25 12:11:57 jsing Exp $ */ |
2 | | /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
3 | | * project 2000. |
4 | | */ |
5 | | /* ==================================================================== |
6 | | * Copyright (c) 2000 The OpenSSL Project. All rights reserved. |
7 | | * |
8 | | * Redistribution and use in source and binary forms, with or without |
9 | | * modification, are permitted provided that the following conditions |
10 | | * are met: |
11 | | * |
12 | | * 1. Redistributions of source code must retain the above copyright |
13 | | * notice, this list of conditions and the following disclaimer. |
14 | | * |
15 | | * 2. Redistributions in binary form must reproduce the above copyright |
16 | | * notice, this list of conditions and the following disclaimer in |
17 | | * the documentation and/or other materials provided with the |
18 | | * distribution. |
19 | | * |
20 | | * 3. All advertising materials mentioning features or use of this |
21 | | * software must display the following acknowledgment: |
22 | | * "This product includes software developed by the OpenSSL Project |
23 | | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
24 | | * |
25 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
26 | | * endorse or promote products derived from this software without |
27 | | * prior written permission. For written permission, please contact |
28 | | * licensing@OpenSSL.org. |
29 | | * |
30 | | * 5. Products derived from this software may not be called "OpenSSL" |
31 | | * nor may "OpenSSL" appear in their names without prior written |
32 | | * permission of the OpenSSL Project. |
33 | | * |
34 | | * 6. Redistributions of any form whatsoever must retain the following |
35 | | * acknowledgment: |
36 | | * "This product includes software developed by the OpenSSL Project |
37 | | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
38 | | * |
39 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
40 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
41 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
42 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
43 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
44 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
45 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
46 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
48 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
49 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
50 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
51 | | * ==================================================================== |
52 | | * |
53 | | * This product includes cryptographic software written by Eric Young |
54 | | * (eay@cryptsoft.com). This product includes software written by Tim |
55 | | * Hudson (tjh@cryptsoft.com). |
56 | | * |
57 | | */ |
58 | | |
59 | | #include <stdio.h> |
60 | | #include <string.h> |
61 | | |
62 | | #include <openssl/asn1.h> |
63 | | #include <openssl/crypto.h> |
64 | | #include <openssl/x509.h> |
65 | | |
66 | | #include "asn1_locl.h" |
67 | | |
68 | | #include "charmap.h" |
69 | | |
70 | | /* ASN1_STRING_print_ex() and X509_NAME_print_ex(). |
71 | | * Enhanced string and name printing routines handling |
72 | | * multibyte characters, RFC2253 and a host of other |
73 | | * options. |
74 | | */ |
75 | | |
76 | 6.94k | #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253) |
77 | | |
78 | 5.06k | #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \ |
79 | 5.06k | ASN1_STRFLGS_ESC_QUOTE | \ |
80 | 5.06k | ASN1_STRFLGS_ESC_CTRL | \ |
81 | 5.06k | ASN1_STRFLGS_ESC_MSB) |
82 | | |
83 | | |
84 | | /* Three IO functions for sending data to memory, a BIO and |
85 | | * and a FILE pointer. |
86 | | */ |
87 | | static int |
88 | | send_bio_chars(void *arg, const void *buf, int len) |
89 | 33.1k | { |
90 | 33.1k | if (!arg) |
91 | 3.51k | return 1; |
92 | 29.6k | if (BIO_write(arg, buf, len) != len) |
93 | 0 | return 0; |
94 | 29.6k | return 1; |
95 | 29.6k | } |
96 | | |
97 | | static int |
98 | | send_fp_chars(void *arg, const void *buf, int len) |
99 | 0 | { |
100 | 0 | if (!arg) |
101 | 0 | return 1; |
102 | 0 | if (fwrite(buf, 1, (size_t)len, arg) != (size_t)len) |
103 | 0 | return 0; |
104 | 0 | return 1; |
105 | 0 | } |
106 | | |
107 | | typedef int char_io(void *arg, const void *buf, int len); |
108 | | |
109 | | /* This function handles display of |
110 | | * strings, one character at a time. |
111 | | * It is passed an unsigned long for each |
112 | | * character because it could come from 2 or even |
113 | | * 4 byte forms. |
114 | | */ |
115 | | |
116 | | static int |
117 | | do_esc_char(unsigned long c, unsigned char flags, char *do_quotes, |
118 | | char_io *io_ch, void *arg) |
119 | 6.94k | { |
120 | 6.94k | unsigned char chflgs, chtmp; |
121 | 6.94k | char tmphex[sizeof(long) * 2 + 3]; |
122 | | |
123 | 6.94k | if (c > 0xffffffffL) |
124 | 0 | return -1; |
125 | 6.94k | if (c > 0xffff) { |
126 | 0 | snprintf(tmphex, sizeof tmphex, "\\W%08lX", c); |
127 | 0 | if (!io_ch(arg, tmphex, 10)) |
128 | 0 | return -1; |
129 | 0 | return 10; |
130 | 0 | } |
131 | 6.94k | if (c > 0xff) { |
132 | 0 | snprintf(tmphex, sizeof tmphex, "\\U%04lX", c); |
133 | 0 | if (!io_ch(arg, tmphex, 6)) |
134 | 0 | return -1; |
135 | 0 | return 6; |
136 | 0 | } |
137 | 6.94k | chtmp = (unsigned char)c; |
138 | 6.94k | if (chtmp > 0x7f) |
139 | 4.98k | chflgs = flags & ASN1_STRFLGS_ESC_MSB; |
140 | 1.95k | else |
141 | 1.95k | chflgs = char_type[chtmp] & flags; |
142 | 6.94k | if (chflgs & CHARTYPE_BS_ESC) { |
143 | | /* If we don't escape with quotes, signal we need quotes */ |
144 | 496 | if (chflgs & ASN1_STRFLGS_ESC_QUOTE) { |
145 | 400 | if (do_quotes) |
146 | 200 | *do_quotes = 1; |
147 | 400 | if (!io_ch(arg, &chtmp, 1)) |
148 | 0 | return -1; |
149 | 400 | return 1; |
150 | 400 | } |
151 | 96 | if (!io_ch(arg, "\\", 1)) |
152 | 0 | return -1; |
153 | 96 | if (!io_ch(arg, &chtmp, 1)) |
154 | 0 | return -1; |
155 | 96 | return 2; |
156 | 96 | } |
157 | 6.44k | if (chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) { |
158 | 5.19k | snprintf(tmphex, sizeof tmphex, "\\%02X", chtmp); |
159 | 5.19k | if (!io_ch(arg, tmphex, 3)) |
160 | 0 | return -1; |
161 | 5.19k | return 3; |
162 | 5.19k | } |
163 | | /* If we get this far and do any escaping at all must escape |
164 | | * the escape character itself: backslash. |
165 | | */ |
166 | 1.25k | if (chtmp == '\\' && flags & ESC_FLAGS) { |
167 | 0 | if (!io_ch(arg, "\\\\", 2)) |
168 | 0 | return -1; |
169 | 0 | return 2; |
170 | 0 | } |
171 | 1.25k | if (!io_ch(arg, &chtmp, 1)) |
172 | 0 | return -1; |
173 | 1.25k | return 1; |
174 | 1.25k | } |
175 | | |
176 | 4.31k | #define BUF_TYPE_WIDTH_MASK 0x7 |
177 | 5.35k | #define BUF_TYPE_CONVUTF8 0x8 |
178 | | |
179 | | /* This function sends each character in a buffer to |
180 | | * do_esc_char(). It interprets the content formats |
181 | | * and converts to or from UTF8 as appropriate. |
182 | | */ |
183 | | |
184 | | static int |
185 | | do_buf(unsigned char *buf, int buflen, int type, unsigned char flags, |
186 | | char *quotes, char_io *io_ch, void *arg) |
187 | 3.22k | { |
188 | 3.22k | int i, outlen, len; |
189 | 3.22k | unsigned char orflags, *p, *q; |
190 | 3.22k | unsigned long c; |
191 | | |
192 | 3.22k | p = buf; |
193 | 3.22k | q = buf + buflen; |
194 | 3.22k | outlen = 0; |
195 | 7.50k | while (p != q) { |
196 | 4.31k | if (p == buf && flags & ASN1_STRFLGS_ESC_2253) |
197 | 1.87k | orflags = CHARTYPE_FIRST_ESC_2253; |
198 | 2.44k | else |
199 | 2.44k | orflags = 0; |
200 | 4.31k | switch (type & BUF_TYPE_WIDTH_MASK) { |
201 | 108 | case 4: |
202 | 108 | c = ((unsigned long)*p++) << 24; |
203 | 108 | c |= ((unsigned long)*p++) << 16; |
204 | 108 | c |= ((unsigned long)*p++) << 8; |
205 | 108 | c |= *p++; |
206 | 108 | if (c > UNICODE_MAX || UNICODE_IS_SURROGATE(c)) |
207 | 0 | return -1; |
208 | 108 | break; |
209 | | |
210 | 330 | case 2: |
211 | 330 | c = ((unsigned long)*p++) << 8; |
212 | 330 | c |= *p++; |
213 | 330 | if (UNICODE_IS_SURROGATE(c)) |
214 | 22 | return -1; |
215 | 308 | break; |
216 | | |
217 | 3.87k | case 1: |
218 | 3.87k | c = *p++; |
219 | 3.87k | break; |
220 | | |
221 | 0 | case 0: |
222 | 0 | i = UTF8_getc(p, q - p, &c); |
223 | 0 | if (i < 0) |
224 | 0 | return -1; /* Invalid UTF8String */ |
225 | 0 | p += i; |
226 | 0 | break; |
227 | 0 | default: |
228 | 0 | return -1; /* invalid width */ |
229 | 4.31k | } |
230 | 4.28k | if (p == q && flags & ASN1_STRFLGS_ESC_2253) |
231 | 1.84k | orflags = CHARTYPE_LAST_ESC_2253; |
232 | 4.28k | if (type & BUF_TYPE_CONVUTF8) { |
233 | 3.98k | unsigned char utfbuf[6]; |
234 | 3.98k | int utflen; |
235 | | |
236 | 3.98k | utflen = UTF8_putc(utfbuf, sizeof utfbuf, c); |
237 | 3.98k | if (utflen < 0) |
238 | 0 | return -1; |
239 | 10.6k | for (i = 0; i < utflen; i++) { |
240 | | /* We don't need to worry about setting orflags correctly |
241 | | * because if utflen==1 its value will be correct anyway |
242 | | * otherwise each character will be > 0x7f and so the |
243 | | * character will never be escaped on first and last. |
244 | | */ |
245 | 6.63k | len = do_esc_char(utfbuf[i], |
246 | 6.63k | (unsigned char)(flags | orflags), quotes, |
247 | 6.63k | io_ch, arg); |
248 | 6.63k | if (len < 0) |
249 | 0 | return -1; |
250 | 6.63k | outlen += len; |
251 | 6.63k | } |
252 | 3.98k | } else { |
253 | 308 | len = do_esc_char(c, (unsigned char)(flags | orflags), |
254 | 308 | quotes, io_ch, arg); |
255 | 308 | if (len < 0) |
256 | 0 | return -1; |
257 | 308 | outlen += len; |
258 | 308 | } |
259 | 4.28k | } |
260 | 3.19k | return outlen; |
261 | 3.22k | } |
262 | | |
263 | | /* This function hex dumps a buffer of characters */ |
264 | | |
265 | | static int |
266 | | do_hex_dump(char_io *io_ch, void *arg, unsigned char *buf, int buflen) |
267 | 3.44k | { |
268 | 3.44k | static const char hexdig[] = "0123456789ABCDEF"; |
269 | 3.44k | unsigned char *p, *q; |
270 | 3.44k | char hextmp[2]; |
271 | 3.44k | if (arg) { |
272 | 3.44k | p = buf; |
273 | 3.44k | q = buf + buflen; |
274 | 15.6k | while (p != q) { |
275 | 12.2k | hextmp[0] = hexdig[*p >> 4]; |
276 | 12.2k | hextmp[1] = hexdig[*p & 0xf]; |
277 | 12.2k | if (!io_ch(arg, hextmp, 2)) |
278 | 0 | return -1; |
279 | 12.2k | p++; |
280 | 12.2k | } |
281 | 3.44k | } |
282 | 3.44k | return buflen << 1; |
283 | 3.44k | } |
284 | | |
285 | | /* "dump" a string. This is done when the type is unknown, |
286 | | * or the flags request it. We can either dump the content |
287 | | * octets or the entire DER encoding. This uses the RFC2253 |
288 | | * #01234 format. |
289 | | */ |
290 | | |
291 | | static int |
292 | | do_dump(unsigned long lflags, char_io *io_ch, void *arg, const ASN1_STRING *str) |
293 | 3.44k | { |
294 | | /* Placing the ASN1_STRING in a temp ASN1_TYPE allows |
295 | | * the DER encoding to readily obtained |
296 | | */ |
297 | 3.44k | ASN1_TYPE t; |
298 | 3.44k | unsigned char *der_buf, *p; |
299 | 3.44k | int outlen, der_len; |
300 | | |
301 | 3.44k | if (!io_ch(arg, "#", 1)) |
302 | 0 | return -1; |
303 | | /* If we don't dump DER encoding just dump content octets */ |
304 | 3.44k | if (!(lflags & ASN1_STRFLGS_DUMP_DER)) { |
305 | 0 | outlen = do_hex_dump(io_ch, arg, str->data, str->length); |
306 | 0 | if (outlen < 0) |
307 | 0 | return -1; |
308 | 0 | return outlen + 1; |
309 | 0 | } |
310 | 3.44k | t.type = str->type; |
311 | 3.44k | t.value.ptr = (char *)str; |
312 | 3.44k | der_len = i2d_ASN1_TYPE(&t, NULL); |
313 | 3.44k | der_buf = malloc(der_len); |
314 | 3.44k | if (!der_buf) |
315 | 0 | return -1; |
316 | 3.44k | p = der_buf; |
317 | 3.44k | i2d_ASN1_TYPE(&t, &p); |
318 | 3.44k | outlen = do_hex_dump(io_ch, arg, der_buf, der_len); |
319 | 3.44k | free(der_buf); |
320 | 3.44k | if (outlen < 0) |
321 | 0 | return -1; |
322 | 3.44k | return outlen + 1; |
323 | 3.44k | } |
324 | | |
325 | | /* This is the main function, print out an |
326 | | * ASN1_STRING taking note of various escape |
327 | | * and display options. Returns number of |
328 | | * characters written or -1 if an error |
329 | | * occurred. |
330 | | */ |
331 | | |
332 | | static int |
333 | | do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, |
334 | | const ASN1_STRING *str) |
335 | 5.06k | { |
336 | 5.06k | int outlen, len; |
337 | 5.06k | int type; |
338 | 5.06k | char quotes; |
339 | 5.06k | unsigned char flags; |
340 | | |
341 | 5.06k | quotes = 0; |
342 | | /* Keep a copy of escape flags */ |
343 | 5.06k | flags = (unsigned char)(lflags & ESC_FLAGS); |
344 | 5.06k | type = str->type; |
345 | 5.06k | outlen = 0; |
346 | | |
347 | 5.06k | if (lflags & ASN1_STRFLGS_SHOW_TYPE) { |
348 | 0 | const char *tagname; |
349 | 0 | tagname = ASN1_tag2str(type); |
350 | 0 | outlen += strlen(tagname); |
351 | 0 | if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1)) |
352 | 0 | return -1; |
353 | 0 | outlen++; |
354 | 0 | } |
355 | | |
356 | | /* Decide what to do with type, either dump content or display it */ |
357 | | |
358 | 5.06k | if (lflags & ASN1_STRFLGS_DUMP_ALL) { |
359 | | /* Dump everything. */ |
360 | 0 | type = -1; |
361 | 5.06k | } else if (lflags & ASN1_STRFLGS_IGNORE_TYPE) { |
362 | | /* Ignore the string type. */ |
363 | 0 | type = 1; |
364 | 5.06k | } else { |
365 | | /* Else determine width based on type. */ |
366 | 5.06k | type = asn1_tag2charwidth(type); |
367 | 5.06k | if (type == -1 && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN)) |
368 | 0 | type = 1; |
369 | 5.06k | } |
370 | | |
371 | 5.06k | if (type == -1) { |
372 | 3.44k | len = do_dump(lflags, io_ch, arg, str); |
373 | 3.44k | if (len < 0) |
374 | 0 | return -1; |
375 | 3.44k | outlen += len; |
376 | 3.44k | return outlen; |
377 | 3.44k | } |
378 | | |
379 | 1.62k | if (lflags & ASN1_STRFLGS_UTF8_CONVERT) { |
380 | | /* Note: if string is UTF8 and we want |
381 | | * to convert to UTF8 then we just interpret |
382 | | * it as 1 byte per character to avoid converting |
383 | | * twice. |
384 | | */ |
385 | 1.62k | if (!type) |
386 | 557 | type = 1; |
387 | 1.06k | else |
388 | 1.06k | type |= BUF_TYPE_CONVUTF8; |
389 | 1.62k | } |
390 | | |
391 | 1.62k | len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL); |
392 | 1.62k | if (len < 0) |
393 | 22 | return -1; |
394 | 1.59k | outlen += len; |
395 | 1.59k | if (quotes) |
396 | 96 | outlen += 2; |
397 | 1.59k | if (!arg) |
398 | 0 | return outlen; |
399 | 1.59k | if (quotes && !io_ch(arg, "\"", 1)) |
400 | 0 | return -1; |
401 | 1.59k | if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0) |
402 | 0 | return -1; |
403 | 1.59k | if (quotes && !io_ch(arg, "\"", 1)) |
404 | 0 | return -1; |
405 | 1.59k | return outlen; |
406 | 1.59k | } |
407 | | |
408 | | /* Used for line indenting: print 'indent' spaces */ |
409 | | |
410 | | static int |
411 | | do_indent(char_io *io_ch, void *arg, int indent) |
412 | 7.89k | { |
413 | 7.89k | int i; |
414 | 7.89k | for (i = 0; i < indent; i++) |
415 | 0 | if (!io_ch(arg, " ", 1)) |
416 | 0 | return 0; |
417 | 7.89k | return 1; |
418 | 7.89k | } |
419 | | |
420 | 0 | #define FN_WIDTH_LN 25 |
421 | 2.23k | #define FN_WIDTH_SN 10 |
422 | | |
423 | | static int |
424 | | do_name_ex(char_io *io_ch, void *arg, const X509_NAME *n, int indent, |
425 | | unsigned long flags) |
426 | 7.77k | { |
427 | 7.77k | int i, prev = -1, orflags, cnt; |
428 | 7.77k | int fn_opt, fn_nid; |
429 | 7.77k | ASN1_OBJECT *fn; |
430 | 7.77k | ASN1_STRING *val; |
431 | 7.77k | X509_NAME_ENTRY *ent; |
432 | 7.77k | char objtmp[80]; |
433 | 7.77k | const char *objbuf; |
434 | 7.77k | int outlen, len; |
435 | 7.77k | char *sep_dn, *sep_mv, *sep_eq; |
436 | 7.77k | int sep_dn_len, sep_mv_len, sep_eq_len; |
437 | | |
438 | 7.77k | if (indent < 0) |
439 | 0 | indent = 0; |
440 | 7.77k | outlen = indent; |
441 | 7.77k | if (!do_indent(io_ch, arg, indent)) |
442 | 0 | return -1; |
443 | | |
444 | 7.77k | switch (flags & XN_FLAG_SEP_MASK) { |
445 | 0 | case XN_FLAG_SEP_MULTILINE: |
446 | 0 | sep_dn = "\n"; |
447 | 0 | sep_dn_len = 1; |
448 | 0 | sep_mv = " + "; |
449 | 0 | sep_mv_len = 3; |
450 | 0 | break; |
451 | | |
452 | 0 | case XN_FLAG_SEP_COMMA_PLUS: |
453 | 0 | sep_dn = ","; |
454 | 0 | sep_dn_len = 1; |
455 | 0 | sep_mv = "+"; |
456 | 0 | sep_mv_len = 1; |
457 | 0 | indent = 0; |
458 | 0 | break; |
459 | | |
460 | 7.77k | case XN_FLAG_SEP_CPLUS_SPC: |
461 | 7.77k | sep_dn = ", "; |
462 | 7.77k | sep_dn_len = 2; |
463 | 7.77k | sep_mv = " + "; |
464 | 7.77k | sep_mv_len = 3; |
465 | 7.77k | indent = 0; |
466 | 7.77k | break; |
467 | | |
468 | 0 | case XN_FLAG_SEP_SPLUS_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 | default: |
477 | 0 | return -1; |
478 | 7.77k | } |
479 | | |
480 | 7.77k | if (flags & XN_FLAG_SPC_EQ) { |
481 | 7.77k | sep_eq = " = "; |
482 | 7.77k | sep_eq_len = 3; |
483 | 7.77k | } else { |
484 | 0 | sep_eq = "="; |
485 | 0 | sep_eq_len = 1; |
486 | 0 | } |
487 | | |
488 | 7.77k | fn_opt = flags & XN_FLAG_FN_MASK; |
489 | | |
490 | 7.77k | cnt = X509_NAME_entry_count(n); |
491 | 12.8k | for (i = 0; i < cnt; i++) { |
492 | 5.06k | if (flags & XN_FLAG_DN_REV) |
493 | 0 | ent = X509_NAME_get_entry(n, cnt - i - 1); |
494 | 5.06k | else |
495 | 5.06k | ent = X509_NAME_get_entry(n, i); |
496 | 5.06k | if (prev != -1) { |
497 | 128 | if (prev == X509_NAME_ENTRY_set(ent)) { |
498 | 13 | if (!io_ch(arg, sep_mv, sep_mv_len)) |
499 | 0 | return -1; |
500 | 13 | outlen += sep_mv_len; |
501 | 115 | } else { |
502 | 115 | if (!io_ch(arg, sep_dn, sep_dn_len)) |
503 | 0 | return -1; |
504 | 115 | outlen += sep_dn_len; |
505 | 115 | if (!do_indent(io_ch, arg, indent)) |
506 | 0 | return -1; |
507 | 115 | outlen += indent; |
508 | 115 | } |
509 | 128 | } |
510 | 5.06k | prev = X509_NAME_ENTRY_set(ent); |
511 | 5.06k | fn = X509_NAME_ENTRY_get_object(ent); |
512 | 5.06k | val = X509_NAME_ENTRY_get_data(ent); |
513 | 5.06k | fn_nid = OBJ_obj2nid(fn); |
514 | 5.06k | if (fn_opt != XN_FLAG_FN_NONE) { |
515 | 5.06k | int objlen, fld_len; |
516 | 5.06k | if ((fn_opt == XN_FLAG_FN_OID) || |
517 | 5.06k | (fn_nid == NID_undef)) { |
518 | 2.83k | OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1); |
519 | 2.83k | fld_len = 0; /* XXX: what should this be? */ |
520 | 2.83k | objbuf = objtmp; |
521 | 2.83k | } else { |
522 | 2.23k | if (fn_opt == XN_FLAG_FN_SN) { |
523 | 2.23k | fld_len = FN_WIDTH_SN; |
524 | 2.23k | objbuf = OBJ_nid2sn(fn_nid); |
525 | 2.23k | } 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 | 2.23k | } |
533 | 5.06k | objlen = strlen(objbuf); |
534 | 5.06k | if (!io_ch(arg, objbuf, objlen)) |
535 | 0 | return -1; |
536 | 5.06k | 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 | 5.06k | if (!io_ch(arg, sep_eq, sep_eq_len)) |
542 | 0 | return -1; |
543 | 5.06k | outlen += objlen + sep_eq_len; |
544 | 5.06k | } |
545 | | /* If the field name is unknown then fix up the DER dump |
546 | | * flag. We might want to limit this further so it will |
547 | | * DER dump on anything other than a few 'standard' fields. |
548 | | */ |
549 | 5.06k | if ((fn_nid == NID_undef) && |
550 | 5.06k | (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS)) |
551 | 0 | orflags = ASN1_STRFLGS_DUMP_ALL; |
552 | 5.06k | else |
553 | 5.06k | orflags = 0; |
554 | | |
555 | 5.06k | len = do_print_ex(io_ch, arg, flags | orflags, val); |
556 | 5.06k | if (len < 0) |
557 | 22 | return -1; |
558 | 5.04k | outlen += len; |
559 | 5.04k | } |
560 | 7.75k | return outlen; |
561 | 7.77k | } |
562 | | |
563 | | /* Wrappers round the main functions */ |
564 | | |
565 | | int |
566 | | X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent, |
567 | | unsigned long flags) |
568 | 25.5k | { |
569 | 25.5k | if (flags == XN_FLAG_COMPAT) |
570 | 17.7k | return X509_NAME_print(out, nm, indent); |
571 | 7.77k | return do_name_ex(send_bio_chars, out, nm, indent, flags); |
572 | 25.5k | } |
573 | | |
574 | | int |
575 | | X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent, |
576 | | unsigned long flags) |
577 | 0 | { |
578 | 0 | if (flags == XN_FLAG_COMPAT) { |
579 | 0 | BIO *btmp; |
580 | 0 | int ret; |
581 | 0 | btmp = BIO_new_fp(fp, BIO_NOCLOSE); |
582 | 0 | if (!btmp) |
583 | 0 | return -1; |
584 | 0 | ret = X509_NAME_print(btmp, nm, indent); |
585 | 0 | BIO_free(btmp); |
586 | 0 | return ret; |
587 | 0 | } |
588 | 0 | return do_name_ex(send_fp_chars, fp, nm, indent, flags); |
589 | 0 | } |
590 | | |
591 | | int |
592 | | ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags) |
593 | 0 | { |
594 | 0 | return do_print_ex(send_bio_chars, out, flags, str); |
595 | 0 | } |
596 | | |
597 | | int |
598 | | ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags) |
599 | 0 | { |
600 | 0 | return do_print_ex(send_fp_chars, fp, flags, str); |
601 | 0 | } |