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