/src/openssl/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  | 0  | #define CHARTYPE_BS_ESC         (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253)  | 
28  |  |  | 
29  | 0  | #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \  | 
30  | 0  |                   ASN1_STRFLGS_ESC_2254 | \  | 
31  | 0  |                   ASN1_STRFLGS_ESC_QUOTE | \  | 
32  | 0  |                   ASN1_STRFLGS_ESC_CTRL | \  | 
33  | 0  |                   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  | 0  | { | 
41  | 0  |     if (!arg)  | 
42  | 0  |         return 1;  | 
43  | 0  |     if (BIO_write(arg, buf, len) != len)  | 
44  | 0  |         return 0;  | 
45  | 0  |     return 1;  | 
46  | 0  | }  | 
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  | 0  | { | 
70  | 0  |     unsigned short chflgs;  | 
71  | 0  |     unsigned char chtmp;  | 
72  | 0  |     char tmphex[HEX_SIZE(long) + 3];  | 
73  |  | 
  | 
74  | 0  |     if (c > 0xffffffffL)  | 
75  | 0  |         return -1;  | 
76  | 0  |     if (c > 0xffff) { | 
77  | 0  |         BIO_snprintf(tmphex, sizeof(tmphex), "\\W%08lX", c);  | 
78  | 0  |         if (!io_ch(arg, tmphex, 10))  | 
79  | 0  |             return -1;  | 
80  | 0  |         return 10;  | 
81  | 0  |     }  | 
82  | 0  |     if (c > 0xff) { | 
83  | 0  |         BIO_snprintf(tmphex, sizeof(tmphex), "\\U%04lX", c);  | 
84  | 0  |         if (!io_ch(arg, tmphex, 6))  | 
85  | 0  |             return -1;  | 
86  | 0  |         return 6;  | 
87  | 0  |     }  | 
88  | 0  |     chtmp = (unsigned char)c;  | 
89  | 0  |     if (chtmp > 0x7f)  | 
90  | 0  |         chflgs = flags & ASN1_STRFLGS_ESC_MSB;  | 
91  | 0  |     else  | 
92  | 0  |         chflgs = char_type[chtmp] & flags;  | 
93  | 0  |     if (chflgs & CHARTYPE_BS_ESC) { | 
94  |  |         /* If we don't escape with quotes, signal we need quotes */  | 
95  | 0  |         if (chflgs & ASN1_STRFLGS_ESC_QUOTE) { | 
96  | 0  |             if (do_quotes)  | 
97  | 0  |                 *do_quotes = 1;  | 
98  | 0  |             if (!io_ch(arg, &chtmp, 1))  | 
99  | 0  |                 return -1;  | 
100  | 0  |             return 1;  | 
101  | 0  |         }  | 
102  | 0  |         if (!io_ch(arg, "\\", 1))  | 
103  | 0  |             return -1;  | 
104  | 0  |         if (!io_ch(arg, &chtmp, 1))  | 
105  | 0  |             return -1;  | 
106  | 0  |         return 2;  | 
107  | 0  |     }  | 
108  | 0  |     if (chflgs & (ASN1_STRFLGS_ESC_CTRL  | 
109  | 0  |                   | ASN1_STRFLGS_ESC_MSB  | 
110  | 0  |                   | 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(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  |     unsigned char *p, *q;  | 
146  | 0  |     unsigned long c;  | 
147  |  | 
  | 
148  | 0  |     p = buf;  | 
149  | 0  |     q = buf + buflen;  | 
150  | 0  |     outlen = 0;  | 
151  | 0  |     charwidth = type & BUF_TYPE_WIDTH_MASK;  | 
152  |  | 
  | 
153  | 0  |     switch (charwidth) { | 
154  | 0  |     case 4:  | 
155  | 0  |         if (buflen & 3) { | 
156  | 0  |             ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);  | 
157  | 0  |             return -1;  | 
158  | 0  |         }  | 
159  | 0  |         break;  | 
160  | 0  |     case 2:  | 
161  | 0  |         if (buflen & 1) { | 
162  | 0  |             ERR_raise(ERR_LIB_ASN1, ASN1_R_INVALID_BMPSTRING_LENGTH);  | 
163  | 0  |             return -1;  | 
164  | 0  |         }  | 
165  | 0  |         break;  | 
166  | 0  |     default:  | 
167  | 0  |         break;  | 
168  | 0  |     }  | 
169  |  |  | 
170  | 0  |     while (p != q) { | 
171  | 0  |         if (p == buf && flags & ASN1_STRFLGS_ESC_2253)  | 
172  | 0  |             orflags = CHARTYPE_FIRST_ESC_2253;  | 
173  | 0  |         else  | 
174  | 0  |             orflags = 0;  | 
175  |  | 
  | 
176  | 0  |         switch (charwidth) { | 
177  | 0  |         case 4:  | 
178  | 0  |             c = ((unsigned long)*p++) << 24;  | 
179  | 0  |             c |= ((unsigned long)*p++) << 16;  | 
180  | 0  |             c |= ((unsigned long)*p++) << 8;  | 
181  | 0  |             c |= *p++;  | 
182  | 0  |             break;  | 
183  |  |  | 
184  | 0  |         case 2:  | 
185  | 0  |             c = ((unsigned long)*p++) << 8;  | 
186  | 0  |             c |= *p++;  | 
187  | 0  |             break;  | 
188  |  |  | 
189  | 0  |         case 1:  | 
190  | 0  |             c = *p++;  | 
191  | 0  |             break;  | 
192  |  |  | 
193  | 0  |         case 0:  | 
194  | 0  |             i = UTF8_getc(p, buflen, &c);  | 
195  | 0  |             if (i < 0)  | 
196  | 0  |                 return -1;      /* Invalid UTF8String */  | 
197  | 0  |             buflen -= i;  | 
198  | 0  |             p += i;  | 
199  | 0  |             break;  | 
200  | 0  |         default:  | 
201  | 0  |             return -1;          /* invalid width */  | 
202  | 0  |         }  | 
203  | 0  |         if (p == q && flags & ASN1_STRFLGS_ESC_2253)  | 
204  | 0  |             orflags = CHARTYPE_LAST_ESC_2253;  | 
205  | 0  |         if (type & BUF_TYPE_CONVUTF8) { | 
206  | 0  |             unsigned char utfbuf[6];  | 
207  | 0  |             int utflen;  | 
208  | 0  |             utflen = UTF8_putc(utfbuf, sizeof(utfbuf), c);  | 
209  | 0  |             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  | 0  |                 len = do_esc_char(utfbuf[i], flags | orflags, quotes,  | 
217  | 0  |                                   io_ch, arg);  | 
218  | 0  |                 if (len < 0)  | 
219  | 0  |                     return -1;  | 
220  | 0  |                 outlen += len;  | 
221  | 0  |             }  | 
222  | 0  |         } else { | 
223  | 0  |             len = do_esc_char(c, flags | orflags, quotes,  | 
224  | 0  |                               io_ch, arg);  | 
225  | 0  |             if (len < 0)  | 
226  | 0  |                 return -1;  | 
227  | 0  |             outlen += len;  | 
228  | 0  |         }  | 
229  | 0  |     }  | 
230  | 0  |     return outlen;  | 
231  | 0  | }  | 
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  | 0  | { | 
238  | 0  |     unsigned char *p, *q;  | 
239  | 0  |     char hextmp[2];  | 
240  |  | 
  | 
241  | 0  |     if (arg) { | 
242  | 0  |         p = buf;  | 
243  | 0  |         q = buf + buflen;  | 
244  | 0  |         while (p != q) { | 
245  | 0  |             ossl_to_hex(hextmp, *p);  | 
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  |  |     /*  | 
264  |  |      * Placing the ASN1_STRING in a temp ASN1_TYPE allows the DER encoding to  | 
265  |  |      * readily obtained  | 
266  |  |      */  | 
267  | 0  |     ASN1_TYPE t;  | 
268  | 0  |     unsigned char *der_buf, *p;  | 
269  | 0  |     int outlen, der_len;  | 
270  |  | 
  | 
271  | 0  |     if (!io_ch(arg, "#", 1))  | 
272  | 0  |         return -1;  | 
273  |  |     /* 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_len <= 0)  | 
284  | 0  |         return -1;  | 
285  | 0  |     if ((der_buf = OPENSSL_malloc(der_len)) == NULL)  | 
286  | 0  |         return -1;  | 
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  |  |     /* Keep a copy of escape flags */  | 
338  | 0  |     flags = (unsigned short)(lflags & ESC_FLAGS);  | 
339  |  | 
  | 
340  | 0  |     type = str->type;  | 
341  |  | 
  | 
342  | 0  |     outlen = 0;  | 
343  |  | 
  | 
344  | 0  |     if (lflags & ASN1_STRFLGS_SHOW_TYPE) { | 
345  | 0  |         const char *tagname;  | 
346  |  | 
  | 
347  | 0  |         tagname = ASN1_tag2str(type);  | 
348  |  |         /* We can directly cast here as tagname will never be too large. */  | 
349  | 0  |         outlen += (int)strlen(tagname);  | 
350  | 0  |         if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1))  | 
351  | 0  |             return -1;  | 
352  | 0  |         outlen++;  | 
353  | 0  |     }  | 
354  |  |  | 
355  |  |     /* Decide what to do with type, either dump content or display it */  | 
356  |  |  | 
357  |  |     /* Dump everything */  | 
358  | 0  |     if (lflags & ASN1_STRFLGS_DUMP_ALL)  | 
359  | 0  |         type = -1;  | 
360  |  |     /* Ignore the string type */  | 
361  | 0  |     else if (lflags & ASN1_STRFLGS_IGNORE_TYPE)  | 
362  | 0  |         type = 1;  | 
363  | 0  |     else { | 
364  |  |         /* Else determine width based on type */  | 
365  | 0  |         if ((type > 0) && (type < 31))  | 
366  | 0  |             type = tag2nbyte[type];  | 
367  | 0  |         else  | 
368  | 0  |             type = -1;  | 
369  | 0  |         if ((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN))  | 
370  | 0  |             type = 1;  | 
371  | 0  |     }  | 
372  |  | 
  | 
373  | 0  |     if (type == -1) { | 
374  | 0  |         len = do_dump(lflags, io_ch, arg, str);  | 
375  | 0  |         if (len < 0 || len > INT_MAX - outlen)  | 
376  | 0  |             return -1;  | 
377  | 0  |         outlen += len;  | 
378  | 0  |         return outlen;  | 
379  | 0  |     }  | 
380  |  |  | 
381  | 0  |     if (lflags & ASN1_STRFLGS_UTF8_CONVERT) { | 
382  |  |         /*  | 
383  |  |          * Note: if string is UTF8 and we want to convert to UTF8 then we  | 
384  |  |          * just interpret it as 1 byte per character to avoid converting  | 
385  |  |          * twice.  | 
386  |  |          */  | 
387  | 0  |         if (!type)  | 
388  | 0  |             type = 1;  | 
389  | 0  |         else  | 
390  | 0  |             type |= BUF_TYPE_CONVUTF8;  | 
391  | 0  |     }  | 
392  |  | 
  | 
393  | 0  |     len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL);  | 
394  | 0  |     if (len < 0 || len > INT_MAX - 2 - outlen)  | 
395  | 0  |         return -1;  | 
396  | 0  |     outlen += len;  | 
397  | 0  |     if (quotes)  | 
398  | 0  |         outlen += 2;  | 
399  | 0  |     if (!arg)  | 
400  | 0  |         return outlen;  | 
401  | 0  |     if (quotes && !io_ch(arg, "\"", 1))  | 
402  | 0  |         return -1;  | 
403  | 0  |     if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)  | 
404  | 0  |         return -1;  | 
405  | 0  |     if (quotes && !io_ch(arg, "\"", 1))  | 
406  | 0  |         return -1;  | 
407  | 0  |     return outlen;  | 
408  | 0  | }  | 
409  |  |  | 
410  |  | /* Used for line indenting: print 'indent' spaces */  | 
411  |  |  | 
412  |  | static int do_indent(char_io *io_ch, void *arg, int indent)  | 
413  | 0  | { | 
414  | 0  |     int i;  | 
415  | 0  |     for (i = 0; i < indent; i++)  | 
416  | 0  |         if (!io_ch(arg, " ", 1))  | 
417  | 0  |             return 0;  | 
418  | 0  |     return 1;  | 
419  | 0  | }  | 
420  |  |  | 
421  | 0  | #define FN_WIDTH_LN     25  | 
422  | 0  | #define FN_WIDTH_SN     10  | 
423  |  |  | 
424  |  | static int do_name_ex(char_io *io_ch, void *arg, const X509_NAME *n,  | 
425  |  |                       int indent, unsigned long flags)  | 
426  | 0  | { | 
427  | 0  |     int i, prev = -1, orflags, cnt;  | 
428  | 0  |     int fn_opt, fn_nid;  | 
429  | 0  |     ASN1_OBJECT *fn;  | 
430  | 0  |     const ASN1_STRING *val;  | 
431  | 0  |     const X509_NAME_ENTRY *ent;  | 
432  | 0  |     char objtmp[80];  | 
433  | 0  |     const char *objbuf;  | 
434  | 0  |     int outlen, len;  | 
435  | 0  |     char *sep_dn, *sep_mv, *sep_eq;  | 
436  | 0  |     int sep_dn_len, sep_mv_len, sep_eq_len;  | 
437  | 0  |     if (indent < 0)  | 
438  | 0  |         indent = 0;  | 
439  | 0  |     outlen = indent;  | 
440  | 0  |     if (!do_indent(io_ch, arg, indent))  | 
441  | 0  |         return -1;  | 
442  | 0  |     switch (flags & XN_FLAG_SEP_MASK) { | 
443  | 0  |     case XN_FLAG_SEP_MULTILINE:  | 
444  | 0  |         sep_dn = "\n";  | 
445  | 0  |         sep_dn_len = 1;  | 
446  | 0  |         sep_mv = " + ";  | 
447  | 0  |         sep_mv_len = 3;  | 
448  | 0  |         break;  | 
449  |  |  | 
450  | 0  |     case XN_FLAG_SEP_COMMA_PLUS:  | 
451  | 0  |         sep_dn = ",";  | 
452  | 0  |         sep_dn_len = 1;  | 
453  | 0  |         sep_mv = "+";  | 
454  | 0  |         sep_mv_len = 1;  | 
455  | 0  |         indent = 0;  | 
456  | 0  |         break;  | 
457  |  |  | 
458  | 0  |     case XN_FLAG_SEP_CPLUS_SPC:  | 
459  | 0  |         sep_dn = ", ";  | 
460  | 0  |         sep_dn_len = 2;  | 
461  | 0  |         sep_mv = " + ";  | 
462  | 0  |         sep_mv_len = 3;  | 
463  | 0  |         indent = 0;  | 
464  | 0  |         break;  | 
465  |  |  | 
466  | 0  |     case XN_FLAG_SEP_SPLUS_SPC:  | 
467  | 0  |         sep_dn = "; ";  | 
468  | 0  |         sep_dn_len = 2;  | 
469  | 0  |         sep_mv = " + ";  | 
470  | 0  |         sep_mv_len = 3;  | 
471  | 0  |         indent = 0;  | 
472  | 0  |         break;  | 
473  |  |  | 
474  | 0  |     default:  | 
475  | 0  |         return -1;  | 
476  | 0  |     }  | 
477  |  |  | 
478  | 0  |     if (flags & XN_FLAG_SPC_EQ) { | 
479  | 0  |         sep_eq = " = ";  | 
480  | 0  |         sep_eq_len = 3;  | 
481  | 0  |     } else { | 
482  | 0  |         sep_eq = "=";  | 
483  | 0  |         sep_eq_len = 1;  | 
484  | 0  |     }  | 
485  |  | 
  | 
486  | 0  |     fn_opt = flags & XN_FLAG_FN_MASK;  | 
487  |  | 
  | 
488  | 0  |     cnt = X509_NAME_entry_count(n);  | 
489  | 0  |     for (i = 0; i < cnt; i++) { | 
490  | 0  |         if (flags & XN_FLAG_DN_REV)  | 
491  | 0  |             ent = X509_NAME_get_entry(n, cnt - i - 1);  | 
492  | 0  |         else  | 
493  | 0  |             ent = X509_NAME_get_entry(n, i);  | 
494  | 0  |         if (prev != -1) { | 
495  | 0  |             if (prev == X509_NAME_ENTRY_set(ent)) { | 
496  | 0  |                 if (!io_ch(arg, sep_mv, sep_mv_len))  | 
497  | 0  |                     return -1;  | 
498  | 0  |                 outlen += sep_mv_len;  | 
499  | 0  |             } else { | 
500  | 0  |                 if (!io_ch(arg, sep_dn, sep_dn_len))  | 
501  | 0  |                     return -1;  | 
502  | 0  |                 outlen += sep_dn_len;  | 
503  | 0  |                 if (!do_indent(io_ch, arg, indent))  | 
504  | 0  |                     return -1;  | 
505  | 0  |                 outlen += indent;  | 
506  | 0  |             }  | 
507  | 0  |         }  | 
508  | 0  |         prev = X509_NAME_ENTRY_set(ent);  | 
509  | 0  |         fn = X509_NAME_ENTRY_get_object(ent);  | 
510  | 0  |         val = X509_NAME_ENTRY_get_data(ent);  | 
511  | 0  |         fn_nid = OBJ_obj2nid(fn);  | 
512  | 0  |         if (fn_opt != XN_FLAG_FN_NONE) { | 
513  | 0  |             int objlen, fld_len;  | 
514  | 0  |             if ((fn_opt == XN_FLAG_FN_OID) || (fn_nid == NID_undef)) { | 
515  | 0  |                 OBJ_obj2txt(objtmp, sizeof(objtmp), fn, 1);  | 
516  | 0  |                 fld_len = 0;    /* XXX: what should this be? */  | 
517  | 0  |                 objbuf = objtmp;  | 
518  | 0  |             } else { | 
519  | 0  |                 if (fn_opt == XN_FLAG_FN_SN) { | 
520  | 0  |                     fld_len = FN_WIDTH_SN;  | 
521  | 0  |                     objbuf = OBJ_nid2sn(fn_nid);  | 
522  | 0  |                 } else if (fn_opt == XN_FLAG_FN_LN) { | 
523  | 0  |                     fld_len = FN_WIDTH_LN;  | 
524  | 0  |                     objbuf = OBJ_nid2ln(fn_nid);  | 
525  | 0  |                 } else { | 
526  | 0  |                     fld_len = 0; /* XXX: what should this be? */  | 
527  | 0  |                     objbuf = "";  | 
528  | 0  |                 }  | 
529  | 0  |             }  | 
530  | 0  |             objlen = strlen(objbuf);  | 
531  | 0  |             if (!io_ch(arg, objbuf, objlen))  | 
532  | 0  |                 return -1;  | 
533  | 0  |             if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) { | 
534  | 0  |                 if (!do_indent(io_ch, arg, fld_len - objlen))  | 
535  | 0  |                     return -1;  | 
536  | 0  |                 outlen += fld_len - objlen;  | 
537  | 0  |             }  | 
538  | 0  |             if (!io_ch(arg, sep_eq, sep_eq_len))  | 
539  | 0  |                 return -1;  | 
540  | 0  |             outlen += objlen + sep_eq_len;  | 
541  | 0  |         }  | 
542  |  |         /*  | 
543  |  |          * If the field name is unknown then fix up the DER dump flag. We  | 
544  |  |          * might want to limit this further so it will DER dump on anything  | 
545  |  |          * other than a few 'standard' fields.  | 
546  |  |          */  | 
547  | 0  |         if ((fn_nid == NID_undef) && (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))  | 
548  | 0  |             orflags = ASN1_STRFLGS_DUMP_ALL;  | 
549  | 0  |         else  | 
550  | 0  |             orflags = 0;  | 
551  |  | 
  | 
552  | 0  |         len = do_print_ex(io_ch, arg, flags | orflags, val);  | 
553  | 0  |         if (len < 0)  | 
554  | 0  |             return -1;  | 
555  | 0  |         outlen += len;  | 
556  | 0  |     }  | 
557  | 0  |     return outlen;  | 
558  | 0  | }  | 
559  |  |  | 
560  |  | /* Wrappers round the main functions */  | 
561  |  |  | 
562  |  | int X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent,  | 
563  |  |                        unsigned long flags)  | 
564  | 0  | { | 
565  | 0  |     if (flags == XN_FLAG_COMPAT)  | 
566  | 0  |         return X509_NAME_print(out, nm, indent);  | 
567  | 0  |     return do_name_ex(send_bio_chars, out, nm, indent, flags);  | 
568  | 0  | }  | 
569  |  |  | 
570  |  | #ifndef OPENSSL_NO_STDIO  | 
571  |  | int X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent,  | 
572  |  |                           unsigned long flags)  | 
573  | 0  | { | 
574  | 0  |     if (flags == XN_FLAG_COMPAT) { | 
575  | 0  |         BIO *btmp;  | 
576  | 0  |         int ret;  | 
577  | 0  |         btmp = BIO_new_fp(fp, BIO_NOCLOSE);  | 
578  | 0  |         if (!btmp)  | 
579  | 0  |             return -1;  | 
580  | 0  |         ret = X509_NAME_print(btmp, nm, indent);  | 
581  | 0  |         BIO_free(btmp);  | 
582  | 0  |         return ret;  | 
583  | 0  |     }  | 
584  | 0  |     return do_name_ex(send_fp_chars, fp, nm, indent, flags);  | 
585  | 0  | }  | 
586  |  | #endif  | 
587  |  |  | 
588  |  | int ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags)  | 
589  | 0  | { | 
590  | 0  |     return do_print_ex(send_bio_chars, out, flags, str);  | 
591  | 0  | }  | 
592  |  |  | 
593  |  | #ifndef OPENSSL_NO_STDIO  | 
594  |  | int ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags)  | 
595  | 0  | { | 
596  | 0  |     return do_print_ex(send_fp_chars, fp, flags, str);  | 
597  | 0  | }  | 
598  |  | #endif  | 
599  |  |  | 
600  |  | /*  | 
601  |  |  * Utility function: convert any string type to UTF8, returns number of bytes  | 
602  |  |  * in output string or a negative error code  | 
603  |  |  */  | 
604  |  |  | 
605  |  | int ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in)  | 
606  | 0  | { | 
607  | 0  |     ASN1_STRING stmp, *str = &stmp;  | 
608  | 0  |     int mbflag, type, ret;  | 
609  | 0  |     if (!in)  | 
610  | 0  |         return -1;  | 
611  | 0  |     type = in->type;  | 
612  | 0  |     if ((type < 0) || (type > 30))  | 
613  | 0  |         return -1;  | 
614  | 0  |     mbflag = tag2nbyte[type];  | 
615  | 0  |     if (mbflag == -1)  | 
616  | 0  |         return -1;  | 
617  | 0  |     mbflag |= MBSTRING_FLAG;  | 
618  | 0  |     stmp.data = NULL;  | 
619  | 0  |     stmp.length = 0;  | 
620  | 0  |     stmp.flags = 0;  | 
621  | 0  |     ret =  | 
622  | 0  |         ASN1_mbstring_copy(&str, in->data, in->length, mbflag,  | 
623  | 0  |                            B_ASN1_UTF8STRING);  | 
624  | 0  |     if (ret < 0)  | 
625  | 0  |         return ret;  | 
626  | 0  |     *out = stmp.data;  | 
627  | 0  |     return stmp.length;  | 
628  | 0  | }  |