Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/libressl/src/crypto/asn1/a_strex.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* $OpenBSD: a_strex.c,v 1.28 2018/05/19 10:46:28 tb 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 | #define CHARTYPE_BS_ESC (ASN1_STRFLGS_ESC_2253 | CHARTYPE_FIRST_ESC_2253 | CHARTYPE_LAST_ESC_2253) | - | ||||||||||||||||||
77 | - | |||||||||||||||||||
78 | #define ESC_FLAGS (ASN1_STRFLGS_ESC_2253 | \ | - | ||||||||||||||||||
79 | ASN1_STRFLGS_ESC_QUOTE | \ | - | ||||||||||||||||||
80 | ASN1_STRFLGS_ESC_CTRL | \ | - | ||||||||||||||||||
81 | 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 | { | - | ||||||||||||||||||
90 | if (!arg)
| 0 | ||||||||||||||||||
91 | return 1; never executed: return 1; | 0 | ||||||||||||||||||
92 | if (BIO_write(arg, buf, len) != len)
| 0 | ||||||||||||||||||
93 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
94 | return 1; never executed: return 1; | 0 | ||||||||||||||||||
95 | } | - | ||||||||||||||||||
96 | - | |||||||||||||||||||
97 | static int | - | ||||||||||||||||||
98 | send_fp_chars(void *arg, const void *buf, int len) | - | ||||||||||||||||||
99 | { | - | ||||||||||||||||||
100 | if (!arg)
| 0 | ||||||||||||||||||
101 | return 1; never executed: return 1; | 0 | ||||||||||||||||||
102 | if (fwrite(buf, 1, (size_t)len, arg) != (size_t)len)
| 0 | ||||||||||||||||||
103 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
104 | return 1; never executed: return 1; | 0 | ||||||||||||||||||
105 | } | - | ||||||||||||||||||
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 | { | - | ||||||||||||||||||
120 | unsigned char chflgs, chtmp; | - | ||||||||||||||||||
121 | char tmphex[sizeof(long) * 2 + 3]; | - | ||||||||||||||||||
122 | - | |||||||||||||||||||
123 | if (c > 0xffffffffL)
| 0 | ||||||||||||||||||
124 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
125 | if (c > 0xffff) {
| 0 | ||||||||||||||||||
126 | snprintf(tmphex, sizeof tmphex, "\\W%08lX", c); | - | ||||||||||||||||||
127 | if (!io_ch(arg, tmphex, 10))
| 0 | ||||||||||||||||||
128 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
129 | return 10; never executed: return 10; | 0 | ||||||||||||||||||
130 | } | - | ||||||||||||||||||
131 | if (c > 0xff) {
| 0 | ||||||||||||||||||
132 | snprintf(tmphex, sizeof tmphex, "\\U%04lX", c); | - | ||||||||||||||||||
133 | if (!io_ch(arg, tmphex, 6))
| 0 | ||||||||||||||||||
134 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
135 | return 6; never executed: return 6; | 0 | ||||||||||||||||||
136 | } | - | ||||||||||||||||||
137 | chtmp = (unsigned char)c; | - | ||||||||||||||||||
138 | if (chtmp > 0x7f)
| 0 | ||||||||||||||||||
139 | chflgs = flags & ASN1_STRFLGS_ESC_MSB; never executed: chflgs = flags & 4; | 0 | ||||||||||||||||||
140 | else | - | ||||||||||||||||||
141 | chflgs = char_type[chtmp] & flags; never executed: chflgs = char_type[chtmp] & flags; | 0 | ||||||||||||||||||
142 | if (chflgs & CHARTYPE_BS_ESC) {
| 0 | ||||||||||||||||||
143 | /* If we don't escape with quotes, signal we need quotes */ | - | ||||||||||||||||||
144 | if (chflgs & ASN1_STRFLGS_ESC_QUOTE) {
| 0 | ||||||||||||||||||
145 | if (do_quotes)
| 0 | ||||||||||||||||||
146 | *do_quotes = 1; never executed: *do_quotes = 1; | 0 | ||||||||||||||||||
147 | if (!io_ch(arg, &chtmp, 1))
| 0 | ||||||||||||||||||
148 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
149 | return 1; never executed: return 1; | 0 | ||||||||||||||||||
150 | } | - | ||||||||||||||||||
151 | if (!io_ch(arg, "\\", 1))
| 0 | ||||||||||||||||||
152 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
153 | if (!io_ch(arg, &chtmp, 1))
| 0 | ||||||||||||||||||
154 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
155 | return 2; never executed: return 2; | 0 | ||||||||||||||||||
156 | } | - | ||||||||||||||||||
157 | if (chflgs & (ASN1_STRFLGS_ESC_CTRL|ASN1_STRFLGS_ESC_MSB)) {
| 0 | ||||||||||||||||||
158 | snprintf(tmphex, sizeof tmphex, "\\%02X", chtmp); | - | ||||||||||||||||||
159 | if (!io_ch(arg, tmphex, 3))
| 0 | ||||||||||||||||||
160 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
161 | return 3; never executed: return 3; | 0 | ||||||||||||||||||
162 | } | - | ||||||||||||||||||
163 | /* If we get this far and do any escaping at all must escape | - | ||||||||||||||||||
164 | * the escape character itself: backslash. | - | ||||||||||||||||||
165 | */ | - | ||||||||||||||||||
166 | if (chtmp == '\\' && flags & ESC_FLAGS) {
| 0 | ||||||||||||||||||
167 | if (!io_ch(arg, "\\\\", 2))
| 0 | ||||||||||||||||||
168 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
169 | return 2; never executed: return 2; | 0 | ||||||||||||||||||
170 | } | - | ||||||||||||||||||
171 | if (!io_ch(arg, &chtmp, 1))
| 0 | ||||||||||||||||||
172 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
173 | return 1; never executed: return 1; | 0 | ||||||||||||||||||
174 | } | - | ||||||||||||||||||
175 | - | |||||||||||||||||||
176 | #define BUF_TYPE_WIDTH_MASK 0x7 | - | ||||||||||||||||||
177 | #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 | { | - | ||||||||||||||||||
188 | int i, outlen, len; | - | ||||||||||||||||||
189 | unsigned char orflags, *p, *q; | - | ||||||||||||||||||
190 | unsigned long c; | - | ||||||||||||||||||
191 | - | |||||||||||||||||||
192 | p = buf; | - | ||||||||||||||||||
193 | q = buf + buflen; | - | ||||||||||||||||||
194 | outlen = 0; | - | ||||||||||||||||||
195 | while (p != q) {
| 0 | ||||||||||||||||||
196 | if (p == buf && flags & ASN1_STRFLGS_ESC_2253)
| 0 | ||||||||||||||||||
197 | orflags = CHARTYPE_FIRST_ESC_2253; never executed: orflags = 0x20; | 0 | ||||||||||||||||||
198 | else | - | ||||||||||||||||||
199 | orflags = 0; never executed: orflags = 0; | 0 | ||||||||||||||||||
200 | switch (type & BUF_TYPE_WIDTH_MASK) { | - | ||||||||||||||||||
201 | case 4: never executed: case 4: | 0 | ||||||||||||||||||
202 | c = ((unsigned long)*p++) << 24; | - | ||||||||||||||||||
203 | c |= ((unsigned long)*p++) << 16; | - | ||||||||||||||||||
204 | c |= ((unsigned long)*p++) << 8; | - | ||||||||||||||||||
205 | c |= *p++; | - | ||||||||||||||||||
206 | if (c > UNICODE_MAX || UNICODE_IS_SURROGATE(c))
| 0 | ||||||||||||||||||
207 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
208 | break; never executed: break; | 0 | ||||||||||||||||||
209 | - | |||||||||||||||||||
210 | case 2: never executed: case 2: | 0 | ||||||||||||||||||
211 | c = ((unsigned long)*p++) << 8; | - | ||||||||||||||||||
212 | c |= *p++; | - | ||||||||||||||||||
213 | if (UNICODE_IS_SURROGATE(c))
| 0 | ||||||||||||||||||
214 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
215 | break; never executed: break; | 0 | ||||||||||||||||||
216 | - | |||||||||||||||||||
217 | case 1: never executed: case 1: | 0 | ||||||||||||||||||
218 | c = *p++; | - | ||||||||||||||||||
219 | break; never executed: break; | 0 | ||||||||||||||||||
220 | - | |||||||||||||||||||
221 | case 0: never executed: case 0: | 0 | ||||||||||||||||||
222 | i = UTF8_getc(p, q - p, &c); | - | ||||||||||||||||||
223 | if (i < 0)
| 0 | ||||||||||||||||||
224 | return -1; /* Invalid UTF8String */ never executed: return -1; | 0 | ||||||||||||||||||
225 | p += i; | - | ||||||||||||||||||
226 | break; never executed: break; | 0 | ||||||||||||||||||
227 | default: never executed: default: | 0 | ||||||||||||||||||
228 | return -1; /* invalid width */ never executed: return -1; | 0 | ||||||||||||||||||
229 | } | - | ||||||||||||||||||
230 | if (p == q && flags & ASN1_STRFLGS_ESC_2253)
| 0 | ||||||||||||||||||
231 | orflags = CHARTYPE_LAST_ESC_2253; never executed: orflags = 0x40; | 0 | ||||||||||||||||||
232 | if (type & BUF_TYPE_CONVUTF8) {
| 0 | ||||||||||||||||||
233 | unsigned char utfbuf[6]; | - | ||||||||||||||||||
234 | int utflen; | - | ||||||||||||||||||
235 | - | |||||||||||||||||||
236 | utflen = UTF8_putc(utfbuf, sizeof utfbuf, c); | - | ||||||||||||||||||
237 | if (utflen < 0)
| 0 | ||||||||||||||||||
238 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
239 | for (i = 0; i < utflen; i++) {
| 0 | ||||||||||||||||||
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 | len = do_esc_char(utfbuf[i], | - | ||||||||||||||||||
246 | (unsigned char)(flags | orflags), quotes, | - | ||||||||||||||||||
247 | io_ch, arg); | - | ||||||||||||||||||
248 | if (len < 0)
| 0 | ||||||||||||||||||
249 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
250 | outlen += len; | - | ||||||||||||||||||
251 | } never executed: end of block | 0 | ||||||||||||||||||
252 | } else { never executed: end of block | 0 | ||||||||||||||||||
253 | len = do_esc_char(c, (unsigned char)(flags | orflags), | - | ||||||||||||||||||
254 | quotes, io_ch, arg); | - | ||||||||||||||||||
255 | if (len < 0)
| 0 | ||||||||||||||||||
256 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
257 | outlen += len; | - | ||||||||||||||||||
258 | } never executed: end of block | 0 | ||||||||||||||||||
259 | } | - | ||||||||||||||||||
260 | return outlen; never executed: return outlen; | 0 | ||||||||||||||||||
261 | } | - | ||||||||||||||||||
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 | { | - | ||||||||||||||||||
268 | static const char hexdig[] = "0123456789ABCDEF"; | - | ||||||||||||||||||
269 | unsigned char *p, *q; | - | ||||||||||||||||||
270 | char hextmp[2]; | - | ||||||||||||||||||
271 | if (arg) {
| 0 | ||||||||||||||||||
272 | p = buf; | - | ||||||||||||||||||
273 | q = buf + buflen; | - | ||||||||||||||||||
274 | while (p != q) {
| 0 | ||||||||||||||||||
275 | hextmp[0] = hexdig[*p >> 4]; | - | ||||||||||||||||||
276 | hextmp[1] = hexdig[*p & 0xf]; | - | ||||||||||||||||||
277 | if (!io_ch(arg, hextmp, 2))
| 0 | ||||||||||||||||||
278 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
279 | p++; | - | ||||||||||||||||||
280 | } never executed: end of block | 0 | ||||||||||||||||||
281 | } never executed: end of block | 0 | ||||||||||||||||||
282 | return buflen << 1; never executed: return buflen << 1; | 0 | ||||||||||||||||||
283 | } | - | ||||||||||||||||||
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 | { | - | ||||||||||||||||||
294 | /* Placing the ASN1_STRING in a temp ASN1_TYPE allows | - | ||||||||||||||||||
295 | * the DER encoding to readily obtained | - | ||||||||||||||||||
296 | */ | - | ||||||||||||||||||
297 | ASN1_TYPE t; | - | ||||||||||||||||||
298 | unsigned char *der_buf, *p; | - | ||||||||||||||||||
299 | int outlen, der_len; | - | ||||||||||||||||||
300 | - | |||||||||||||||||||
301 | if (!io_ch(arg, "#", 1))
| 0 | ||||||||||||||||||
302 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
303 | /* If we don't dump DER encoding just dump content octets */ | - | ||||||||||||||||||
304 | if (!(lflags & ASN1_STRFLGS_DUMP_DER)) {
| 0 | ||||||||||||||||||
305 | outlen = do_hex_dump(io_ch, arg, str->data, str->length); | - | ||||||||||||||||||
306 | if (outlen < 0)
| 0 | ||||||||||||||||||
307 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
308 | return outlen + 1; never executed: return outlen + 1; | 0 | ||||||||||||||||||
309 | } | - | ||||||||||||||||||
310 | t.type = str->type; | - | ||||||||||||||||||
311 | t.value.ptr = (char *)str; | - | ||||||||||||||||||
312 | der_len = i2d_ASN1_TYPE(&t, NULL); | - | ||||||||||||||||||
313 | der_buf = malloc(der_len); | - | ||||||||||||||||||
314 | if (!der_buf)
| 0 | ||||||||||||||||||
315 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
316 | p = der_buf; | - | ||||||||||||||||||
317 | i2d_ASN1_TYPE(&t, &p); | - | ||||||||||||||||||
318 | outlen = do_hex_dump(io_ch, arg, der_buf, der_len); | - | ||||||||||||||||||
319 | free(der_buf); | - | ||||||||||||||||||
320 | if (outlen < 0)
| 0 | ||||||||||||||||||
321 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
322 | return outlen + 1; never executed: return outlen + 1; | 0 | ||||||||||||||||||
323 | } | - | ||||||||||||||||||
324 | - | |||||||||||||||||||
325 | /* Lookup table to convert tags to character widths, | - | ||||||||||||||||||
326 | * 0 = UTF8 encoded, -1 is used for non string types | - | ||||||||||||||||||
327 | * otherwise it is the number of bytes per character | - | ||||||||||||||||||
328 | */ | - | ||||||||||||||||||
329 | - | |||||||||||||||||||
330 | static const signed char tag2nbyte[] = { | - | ||||||||||||||||||
331 | -1, -1, -1, -1, -1, /* 0-4 */ | - | ||||||||||||||||||
332 | -1, -1, -1, -1, -1, /* 5-9 */ | - | ||||||||||||||||||
333 | -1, -1, 0, -1, /* 10-13 */ | - | ||||||||||||||||||
334 | -1, -1, -1, -1, /* 15-17 */ | - | ||||||||||||||||||
335 | -1, 1, 1, /* 18-20 */ | - | ||||||||||||||||||
336 | -1, 1, 1, 1, /* 21-24 */ | - | ||||||||||||||||||
337 | -1, 1, -1, /* 25-27 */ | - | ||||||||||||||||||
338 | 4, -1, 2 /* 28-30 */ | - | ||||||||||||||||||
339 | }; | - | ||||||||||||||||||
340 | - | |||||||||||||||||||
341 | /* This is the main function, print out an | - | ||||||||||||||||||
342 | * ASN1_STRING taking note of various escape | - | ||||||||||||||||||
343 | * and display options. Returns number of | - | ||||||||||||||||||
344 | * characters written or -1 if an error | - | ||||||||||||||||||
345 | * occurred. | - | ||||||||||||||||||
346 | */ | - | ||||||||||||||||||
347 | - | |||||||||||||||||||
348 | static int | - | ||||||||||||||||||
349 | do_print_ex(char_io *io_ch, void *arg, unsigned long lflags, | - | ||||||||||||||||||
350 | const ASN1_STRING *str) | - | ||||||||||||||||||
351 | { | - | ||||||||||||||||||
352 | int outlen, len; | - | ||||||||||||||||||
353 | int type; | - | ||||||||||||||||||
354 | char quotes; | - | ||||||||||||||||||
355 | unsigned char flags; | - | ||||||||||||||||||
356 | - | |||||||||||||||||||
357 | quotes = 0; | - | ||||||||||||||||||
358 | /* Keep a copy of escape flags */ | - | ||||||||||||||||||
359 | flags = (unsigned char)(lflags & ESC_FLAGS); | - | ||||||||||||||||||
360 | type = str->type; | - | ||||||||||||||||||
361 | outlen = 0; | - | ||||||||||||||||||
362 | - | |||||||||||||||||||
363 | if (lflags & ASN1_STRFLGS_SHOW_TYPE) {
| 0 | ||||||||||||||||||
364 | const char *tagname; | - | ||||||||||||||||||
365 | tagname = ASN1_tag2str(type); | - | ||||||||||||||||||
366 | outlen += strlen(tagname); | - | ||||||||||||||||||
367 | if (!io_ch(arg, tagname, outlen) || !io_ch(arg, ":", 1))
| 0 | ||||||||||||||||||
368 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
369 | outlen++; | - | ||||||||||||||||||
370 | } never executed: end of block | 0 | ||||||||||||||||||
371 | - | |||||||||||||||||||
372 | /* Decide what to do with type, either dump content or display it */ | - | ||||||||||||||||||
373 | - | |||||||||||||||||||
374 | /* Dump everything */ | - | ||||||||||||||||||
375 | if (lflags & ASN1_STRFLGS_DUMP_ALL)
| 0 | ||||||||||||||||||
376 | type = -1; never executed: type = -1; | 0 | ||||||||||||||||||
377 | /* Ignore the string type */ | - | ||||||||||||||||||
378 | else if (lflags & ASN1_STRFLGS_IGNORE_TYPE)
| 0 | ||||||||||||||||||
379 | type = 1; never executed: type = 1; | 0 | ||||||||||||||||||
380 | else { | - | ||||||||||||||||||
381 | /* Else determine width based on type */ | - | ||||||||||||||||||
382 | if ((type > 0) && (type < 31))
| 0 | ||||||||||||||||||
383 | type = tag2nbyte[type]; never executed: type = tag2nbyte[type]; | 0 | ||||||||||||||||||
384 | else | - | ||||||||||||||||||
385 | type = -1; never executed: type = -1; | 0 | ||||||||||||||||||
386 | if ((type == -1) && !(lflags & ASN1_STRFLGS_DUMP_UNKNOWN))
| 0 | ||||||||||||||||||
387 | type = 1; never executed: type = 1; | 0 | ||||||||||||||||||
388 | } never executed: end of block | 0 | ||||||||||||||||||
389 | - | |||||||||||||||||||
390 | if (type == -1) {
| 0 | ||||||||||||||||||
391 | len = do_dump(lflags, io_ch, arg, str); | - | ||||||||||||||||||
392 | if (len < 0)
| 0 | ||||||||||||||||||
393 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
394 | outlen += len; | - | ||||||||||||||||||
395 | return outlen; never executed: return outlen; | 0 | ||||||||||||||||||
396 | } | - | ||||||||||||||||||
397 | - | |||||||||||||||||||
398 | if (lflags & ASN1_STRFLGS_UTF8_CONVERT) {
| 0 | ||||||||||||||||||
399 | /* Note: if string is UTF8 and we want | - | ||||||||||||||||||
400 | * to convert to UTF8 then we just interpret | - | ||||||||||||||||||
401 | * it as 1 byte per character to avoid converting | - | ||||||||||||||||||
402 | * twice. | - | ||||||||||||||||||
403 | */ | - | ||||||||||||||||||
404 | if (!type)
| 0 | ||||||||||||||||||
405 | type = 1; never executed: type = 1; | 0 | ||||||||||||||||||
406 | else | - | ||||||||||||||||||
407 | type |= BUF_TYPE_CONVUTF8; never executed: type |= 0x8; | 0 | ||||||||||||||||||
408 | } | - | ||||||||||||||||||
409 | - | |||||||||||||||||||
410 | len = do_buf(str->data, str->length, type, flags, "es, io_ch, NULL); | - | ||||||||||||||||||
411 | if (len < 0)
| 0 | ||||||||||||||||||
412 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
413 | outlen += len; | - | ||||||||||||||||||
414 | if (quotes)
| 0 | ||||||||||||||||||
415 | outlen += 2; never executed: outlen += 2; | 0 | ||||||||||||||||||
416 | if (!arg)
| 0 | ||||||||||||||||||
417 | return outlen; never executed: return outlen; | 0 | ||||||||||||||||||
418 | if (quotes && !io_ch(arg, "\"", 1))
| 0 | ||||||||||||||||||
419 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
420 | if (do_buf(str->data, str->length, type, flags, NULL, io_ch, arg) < 0)
| 0 | ||||||||||||||||||
421 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
422 | if (quotes && !io_ch(arg, "\"", 1))
| 0 | ||||||||||||||||||
423 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
424 | return outlen; never executed: return outlen; | 0 | ||||||||||||||||||
425 | } | - | ||||||||||||||||||
426 | - | |||||||||||||||||||
427 | /* Used for line indenting: print 'indent' spaces */ | - | ||||||||||||||||||
428 | - | |||||||||||||||||||
429 | static int | - | ||||||||||||||||||
430 | do_indent(char_io *io_ch, void *arg, int indent) | - | ||||||||||||||||||
431 | { | - | ||||||||||||||||||
432 | int i; | - | ||||||||||||||||||
433 | for (i = 0; i < indent; i++)
| 0 | ||||||||||||||||||
434 | if (!io_ch(arg, " ", 1))
| 0 | ||||||||||||||||||
435 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
436 | return 1; never executed: return 1; | 0 | ||||||||||||||||||
437 | } | - | ||||||||||||||||||
438 | - | |||||||||||||||||||
439 | #define FN_WIDTH_LN 25 | - | ||||||||||||||||||
440 | #define FN_WIDTH_SN 10 | - | ||||||||||||||||||
441 | - | |||||||||||||||||||
442 | static int | - | ||||||||||||||||||
443 | do_name_ex(char_io *io_ch, void *arg, const X509_NAME *n, int indent, | - | ||||||||||||||||||
444 | unsigned long flags) | - | ||||||||||||||||||
445 | { | - | ||||||||||||||||||
446 | int i, prev = -1, orflags, cnt; | - | ||||||||||||||||||
447 | int fn_opt, fn_nid; | - | ||||||||||||||||||
448 | ASN1_OBJECT *fn; | - | ||||||||||||||||||
449 | ASN1_STRING *val; | - | ||||||||||||||||||
450 | X509_NAME_ENTRY *ent; | - | ||||||||||||||||||
451 | char objtmp[80]; | - | ||||||||||||||||||
452 | const char *objbuf; | - | ||||||||||||||||||
453 | int outlen, len; | - | ||||||||||||||||||
454 | char *sep_dn, *sep_mv, *sep_eq; | - | ||||||||||||||||||
455 | int sep_dn_len, sep_mv_len, sep_eq_len; | - | ||||||||||||||||||
456 | - | |||||||||||||||||||
457 | if (indent < 0)
| 0 | ||||||||||||||||||
458 | indent = 0; never executed: indent = 0; | 0 | ||||||||||||||||||
459 | outlen = indent; | - | ||||||||||||||||||
460 | if (!do_indent(io_ch, arg, indent))
| 0 | ||||||||||||||||||
461 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
462 | - | |||||||||||||||||||
463 | switch (flags & XN_FLAG_SEP_MASK) { | - | ||||||||||||||||||
464 | case XN_FLAG_SEP_MULTILINE: never executed: case (4 << 16): | 0 | ||||||||||||||||||
465 | sep_dn = "\n"; | - | ||||||||||||||||||
466 | sep_dn_len = 1; | - | ||||||||||||||||||
467 | sep_mv = " + "; | - | ||||||||||||||||||
468 | sep_mv_len = 3; | - | ||||||||||||||||||
469 | break; never executed: break; | 0 | ||||||||||||||||||
470 | - | |||||||||||||||||||
471 | case XN_FLAG_SEP_COMMA_PLUS: never executed: case (1 << 16): | 0 | ||||||||||||||||||
472 | sep_dn = ","; | - | ||||||||||||||||||
473 | sep_dn_len = 1; | - | ||||||||||||||||||
474 | sep_mv = "+"; | - | ||||||||||||||||||
475 | sep_mv_len = 1; | - | ||||||||||||||||||
476 | indent = 0; | - | ||||||||||||||||||
477 | break; never executed: break; | 0 | ||||||||||||||||||
478 | - | |||||||||||||||||||
479 | case XN_FLAG_SEP_CPLUS_SPC: never executed: case (2 << 16): | 0 | ||||||||||||||||||
480 | sep_dn = ", "; | - | ||||||||||||||||||
481 | sep_dn_len = 2; | - | ||||||||||||||||||
482 | sep_mv = " + "; | - | ||||||||||||||||||
483 | sep_mv_len = 3; | - | ||||||||||||||||||
484 | indent = 0; | - | ||||||||||||||||||
485 | break; never executed: break; | 0 | ||||||||||||||||||
486 | - | |||||||||||||||||||
487 | case XN_FLAG_SEP_SPLUS_SPC: never executed: case (3 << 16): | 0 | ||||||||||||||||||
488 | sep_dn = "; "; | - | ||||||||||||||||||
489 | sep_dn_len = 2; | - | ||||||||||||||||||
490 | sep_mv = " + "; | - | ||||||||||||||||||
491 | sep_mv_len = 3; | - | ||||||||||||||||||
492 | indent = 0; | - | ||||||||||||||||||
493 | break; never executed: break; | 0 | ||||||||||||||||||
494 | - | |||||||||||||||||||
495 | default: never executed: default: | 0 | ||||||||||||||||||
496 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
497 | } | - | ||||||||||||||||||
498 | - | |||||||||||||||||||
499 | if (flags & XN_FLAG_SPC_EQ) {
| 0 | ||||||||||||||||||
500 | sep_eq = " = "; | - | ||||||||||||||||||
501 | sep_eq_len = 3; | - | ||||||||||||||||||
502 | } else { never executed: end of block | 0 | ||||||||||||||||||
503 | sep_eq = "="; | - | ||||||||||||||||||
504 | sep_eq_len = 1; | - | ||||||||||||||||||
505 | } never executed: end of block | 0 | ||||||||||||||||||
506 | - | |||||||||||||||||||
507 | fn_opt = flags & XN_FLAG_FN_MASK; | - | ||||||||||||||||||
508 | - | |||||||||||||||||||
509 | cnt = X509_NAME_entry_count(n); | - | ||||||||||||||||||
510 | for (i = 0; i < cnt; i++) {
| 0 | ||||||||||||||||||
511 | if (flags & XN_FLAG_DN_REV)
| 0 | ||||||||||||||||||
512 | ent = X509_NAME_get_entry(n, cnt - i - 1); never executed: ent = X509_NAME_get_entry(n, cnt - i - 1); | 0 | ||||||||||||||||||
513 | else | - | ||||||||||||||||||
514 | ent = X509_NAME_get_entry(n, i); never executed: ent = X509_NAME_get_entry(n, i); | 0 | ||||||||||||||||||
515 | if (prev != -1) {
| 0 | ||||||||||||||||||
516 | if (prev == ent->set) {
| 0 | ||||||||||||||||||
517 | if (!io_ch(arg, sep_mv, sep_mv_len))
| 0 | ||||||||||||||||||
518 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
519 | outlen += sep_mv_len; | - | ||||||||||||||||||
520 | } else { never executed: end of block | 0 | ||||||||||||||||||
521 | if (!io_ch(arg, sep_dn, sep_dn_len))
| 0 | ||||||||||||||||||
522 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
523 | outlen += sep_dn_len; | - | ||||||||||||||||||
524 | if (!do_indent(io_ch, arg, indent))
| 0 | ||||||||||||||||||
525 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
526 | outlen += indent; | - | ||||||||||||||||||
527 | } never executed: end of block | 0 | ||||||||||||||||||
528 | } | - | ||||||||||||||||||
529 | prev = ent->set; | - | ||||||||||||||||||
530 | fn = X509_NAME_ENTRY_get_object(ent); | - | ||||||||||||||||||
531 | val = X509_NAME_ENTRY_get_data(ent); | - | ||||||||||||||||||
532 | fn_nid = OBJ_obj2nid(fn); | - | ||||||||||||||||||
533 | if (fn_opt != XN_FLAG_FN_NONE) {
| 0 | ||||||||||||||||||
534 | int objlen, fld_len; | - | ||||||||||||||||||
535 | if ((fn_opt == XN_FLAG_FN_OID) ||
| 0 | ||||||||||||||||||
536 | (fn_nid == NID_undef)) {
| 0 | ||||||||||||||||||
537 | OBJ_obj2txt(objtmp, sizeof objtmp, fn, 1); | - | ||||||||||||||||||
538 | fld_len = 0; /* XXX: what should this be? */ | - | ||||||||||||||||||
539 | objbuf = objtmp; | - | ||||||||||||||||||
540 | } else { never executed: end of block | 0 | ||||||||||||||||||
541 | if (fn_opt == XN_FLAG_FN_SN) {
| 0 | ||||||||||||||||||
542 | fld_len = FN_WIDTH_SN; | - | ||||||||||||||||||
543 | objbuf = OBJ_nid2sn(fn_nid); | - | ||||||||||||||||||
544 | } else if (fn_opt == XN_FLAG_FN_LN) { never executed: end of block
| 0 | ||||||||||||||||||
545 | fld_len = FN_WIDTH_LN; | - | ||||||||||||||||||
546 | objbuf = OBJ_nid2ln(fn_nid); | - | ||||||||||||||||||
547 | } else { never executed: end of block | 0 | ||||||||||||||||||
548 | fld_len = 0; /* XXX: what should this be? */ | - | ||||||||||||||||||
549 | objbuf = ""; | - | ||||||||||||||||||
550 | } never executed: end of block | 0 | ||||||||||||||||||
551 | } | - | ||||||||||||||||||
552 | objlen = strlen(objbuf); | - | ||||||||||||||||||
553 | if (!io_ch(arg, objbuf, objlen))
| 0 | ||||||||||||||||||
554 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
555 | if ((objlen < fld_len) && (flags & XN_FLAG_FN_ALIGN)) {
| 0 | ||||||||||||||||||
556 | if (!do_indent(io_ch, arg, fld_len - objlen))
| 0 | ||||||||||||||||||
557 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
558 | outlen += fld_len - objlen; | - | ||||||||||||||||||
559 | } never executed: end of block | 0 | ||||||||||||||||||
560 | if (!io_ch(arg, sep_eq, sep_eq_len))
| 0 | ||||||||||||||||||
561 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
562 | outlen += objlen + sep_eq_len; | - | ||||||||||||||||||
563 | } never executed: end of block | 0 | ||||||||||||||||||
564 | /* If the field name is unknown then fix up the DER dump | - | ||||||||||||||||||
565 | * flag. We might want to limit this further so it will | - | ||||||||||||||||||
566 | * DER dump on anything other than a few 'standard' fields. | - | ||||||||||||||||||
567 | */ | - | ||||||||||||||||||
568 | if ((fn_nid == NID_undef) &&
| 0 | ||||||||||||||||||
569 | (flags & XN_FLAG_DUMP_UNKNOWN_FIELDS))
| 0 | ||||||||||||||||||
570 | orflags = ASN1_STRFLGS_DUMP_ALL; never executed: orflags = 0x80; | 0 | ||||||||||||||||||
571 | else | - | ||||||||||||||||||
572 | orflags = 0; never executed: orflags = 0; | 0 | ||||||||||||||||||
573 | - | |||||||||||||||||||
574 | len = do_print_ex(io_ch, arg, flags | orflags, val); | - | ||||||||||||||||||
575 | if (len < 0)
| 0 | ||||||||||||||||||
576 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
577 | outlen += len; | - | ||||||||||||||||||
578 | } never executed: end of block | 0 | ||||||||||||||||||
579 | return outlen; never executed: return outlen; | 0 | ||||||||||||||||||
580 | } | - | ||||||||||||||||||
581 | - | |||||||||||||||||||
582 | /* Wrappers round the main functions */ | - | ||||||||||||||||||
583 | - | |||||||||||||||||||
584 | int | - | ||||||||||||||||||
585 | X509_NAME_print_ex(BIO *out, const X509_NAME *nm, int indent, | - | ||||||||||||||||||
586 | unsigned long flags) | - | ||||||||||||||||||
587 | { | - | ||||||||||||||||||
588 | if (flags == XN_FLAG_COMPAT)
| 0-6 | ||||||||||||||||||
589 | return X509_NAME_print(out, nm, indent); executed 6 times by 1 test: return X509_NAME_print(out, nm, indent); Executed by:
| 6 | ||||||||||||||||||
590 | return do_name_ex(send_bio_chars, out, nm, indent, flags); never executed: return do_name_ex(send_bio_chars, out, nm, indent, flags); | 0 | ||||||||||||||||||
591 | } | - | ||||||||||||||||||
592 | - | |||||||||||||||||||
593 | int | - | ||||||||||||||||||
594 | X509_NAME_print_ex_fp(FILE *fp, const X509_NAME *nm, int indent, | - | ||||||||||||||||||
595 | unsigned long flags) | - | ||||||||||||||||||
596 | { | - | ||||||||||||||||||
597 | if (flags == XN_FLAG_COMPAT) {
| 0 | ||||||||||||||||||
598 | BIO *btmp; | - | ||||||||||||||||||
599 | int ret; | - | ||||||||||||||||||
600 | btmp = BIO_new_fp(fp, BIO_NOCLOSE); | - | ||||||||||||||||||
601 | if (!btmp)
| 0 | ||||||||||||||||||
602 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
603 | ret = X509_NAME_print(btmp, nm, indent); | - | ||||||||||||||||||
604 | BIO_free(btmp); | - | ||||||||||||||||||
605 | return ret; never executed: return ret; | 0 | ||||||||||||||||||
606 | } | - | ||||||||||||||||||
607 | return do_name_ex(send_fp_chars, fp, nm, indent, flags); never executed: return do_name_ex(send_fp_chars, fp, nm, indent, flags); | 0 | ||||||||||||||||||
608 | } | - | ||||||||||||||||||
609 | - | |||||||||||||||||||
610 | int | - | ||||||||||||||||||
611 | ASN1_STRING_print_ex(BIO *out, const ASN1_STRING *str, unsigned long flags) | - | ||||||||||||||||||
612 | { | - | ||||||||||||||||||
613 | return do_print_ex(send_bio_chars, out, flags, str); never executed: return do_print_ex(send_bio_chars, out, flags, str); | 0 | ||||||||||||||||||
614 | } | - | ||||||||||||||||||
615 | - | |||||||||||||||||||
616 | int | - | ||||||||||||||||||
617 | ASN1_STRING_print_ex_fp(FILE *fp, const ASN1_STRING *str, unsigned long flags) | - | ||||||||||||||||||
618 | { | - | ||||||||||||||||||
619 | return do_print_ex(send_fp_chars, fp, flags, str); never executed: return do_print_ex(send_fp_chars, fp, flags, str); | 0 | ||||||||||||||||||
620 | } | - | ||||||||||||||||||
621 | - | |||||||||||||||||||
622 | /* Utility function: convert any string type to UTF8, returns number of bytes | - | ||||||||||||||||||
623 | * in output string or a negative error code | - | ||||||||||||||||||
624 | */ | - | ||||||||||||||||||
625 | - | |||||||||||||||||||
626 | int | - | ||||||||||||||||||
627 | ASN1_STRING_to_UTF8(unsigned char **out, const ASN1_STRING *in) | - | ||||||||||||||||||
628 | { | - | ||||||||||||||||||
629 | ASN1_STRING stmp, *str = &stmp; | - | ||||||||||||||||||
630 | int mbflag, type, ret; | - | ||||||||||||||||||
631 | - | |||||||||||||||||||
632 | if (!in)
| 0-1838 | ||||||||||||||||||
633 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
634 | type = in->type; | - | ||||||||||||||||||
635 | if ((type < 0) || (type > 30))
| 0-1838 | ||||||||||||||||||
636 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
637 | mbflag = tag2nbyte[type]; | - | ||||||||||||||||||
638 | if (mbflag == -1)
| 0-1838 | ||||||||||||||||||
639 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
640 | mbflag |= MBSTRING_FLAG; | - | ||||||||||||||||||
641 | stmp.data = NULL; | - | ||||||||||||||||||
642 | stmp.length = 0; | - | ||||||||||||||||||
643 | ret = ASN1_mbstring_copy(&str, in->data, in->length, mbflag, | - | ||||||||||||||||||
644 | B_ASN1_UTF8STRING); | - | ||||||||||||||||||
645 | if (ret < 0)
| 0-1838 | ||||||||||||||||||
646 | return ret; never executed: return ret; | 0 | ||||||||||||||||||
647 | *out = stmp.data; | - | ||||||||||||||||||
648 | return stmp.length; executed 1838 times by 8 tests: return stmp.length; Executed by:
| 1838 | ||||||||||||||||||
649 | } | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |