sl@0
|
1 |
/* v3_prn.c */
|
sl@0
|
2 |
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
|
sl@0
|
3 |
* project 1999.
|
sl@0
|
4 |
*/
|
sl@0
|
5 |
/* ====================================================================
|
sl@0
|
6 |
* Copyright (c) 1999 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 |
/* X509 v3 extension utilities */
|
sl@0
|
59 |
|
sl@0
|
60 |
#include <stdio.h>
|
sl@0
|
61 |
#include "cryptlib.h"
|
sl@0
|
62 |
#include <openssl/conf.h>
|
sl@0
|
63 |
#include <openssl/x509v3.h>
|
sl@0
|
64 |
|
sl@0
|
65 |
/* Extension printing routines */
|
sl@0
|
66 |
|
sl@0
|
67 |
static int unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent, int supported);
|
sl@0
|
68 |
|
sl@0
|
69 |
/* Print out a name+value stack */
|
sl@0
|
70 |
|
sl@0
|
71 |
EXPORT_C void X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent, int ml)
|
sl@0
|
72 |
{
|
sl@0
|
73 |
int i;
|
sl@0
|
74 |
CONF_VALUE *nval;
|
sl@0
|
75 |
if(!val) return;
|
sl@0
|
76 |
if(!ml || !sk_CONF_VALUE_num(val)) {
|
sl@0
|
77 |
BIO_printf(out, "%*s", indent, "");
|
sl@0
|
78 |
if(!sk_CONF_VALUE_num(val)) BIO_puts(out, "<EMPTY>\n");
|
sl@0
|
79 |
}
|
sl@0
|
80 |
for(i = 0; i < sk_CONF_VALUE_num(val); i++) {
|
sl@0
|
81 |
if(ml) BIO_printf(out, "%*s", indent, "");
|
sl@0
|
82 |
else if(i > 0) BIO_printf(out, ", ");
|
sl@0
|
83 |
nval = sk_CONF_VALUE_value(val, i);
|
sl@0
|
84 |
if(!nval->name) BIO_puts(out, nval->value);
|
sl@0
|
85 |
else if(!nval->value) BIO_puts(out, nval->name);
|
sl@0
|
86 |
#ifndef CHARSET_EBCDIC
|
sl@0
|
87 |
else BIO_printf(out, "%s:%s", nval->name, nval->value);
|
sl@0
|
88 |
#else
|
sl@0
|
89 |
else {
|
sl@0
|
90 |
int len;
|
sl@0
|
91 |
char *tmp;
|
sl@0
|
92 |
len = strlen(nval->value)+1;
|
sl@0
|
93 |
tmp = OPENSSL_malloc(len);
|
sl@0
|
94 |
if (tmp)
|
sl@0
|
95 |
{
|
sl@0
|
96 |
ascii2ebcdic(tmp, nval->value, len);
|
sl@0
|
97 |
BIO_printf(out, "%s:%s", nval->name, tmp);
|
sl@0
|
98 |
OPENSSL_free(tmp);
|
sl@0
|
99 |
}
|
sl@0
|
100 |
}
|
sl@0
|
101 |
#endif
|
sl@0
|
102 |
if(ml) BIO_puts(out, "\n");
|
sl@0
|
103 |
}
|
sl@0
|
104 |
}
|
sl@0
|
105 |
|
sl@0
|
106 |
/* Main routine: print out a general extension */
|
sl@0
|
107 |
|
sl@0
|
108 |
EXPORT_C int X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent)
|
sl@0
|
109 |
{
|
sl@0
|
110 |
void *ext_str = NULL;
|
sl@0
|
111 |
char *value = NULL;
|
sl@0
|
112 |
const unsigned char *p;
|
sl@0
|
113 |
X509V3_EXT_METHOD *method;
|
sl@0
|
114 |
STACK_OF(CONF_VALUE) *nval = NULL;
|
sl@0
|
115 |
int ok = 1;
|
sl@0
|
116 |
|
sl@0
|
117 |
if(!(method = X509V3_EXT_get(ext)))
|
sl@0
|
118 |
return unknown_ext_print(out, ext, flag, indent, 0);
|
sl@0
|
119 |
p = ext->value->data;
|
sl@0
|
120 |
if(method->it) ext_str = ASN1_item_d2i(NULL, &p, ext->value->length, ASN1_ITEM_ptr(method->it));
|
sl@0
|
121 |
else ext_str = method->d2i(NULL, &p, ext->value->length);
|
sl@0
|
122 |
|
sl@0
|
123 |
if(!ext_str) return unknown_ext_print(out, ext, flag, indent, 1);
|
sl@0
|
124 |
|
sl@0
|
125 |
if(method->i2s) {
|
sl@0
|
126 |
if(!(value = method->i2s(method, ext_str))) {
|
sl@0
|
127 |
ok = 0;
|
sl@0
|
128 |
goto err;
|
sl@0
|
129 |
}
|
sl@0
|
130 |
#ifndef CHARSET_EBCDIC
|
sl@0
|
131 |
BIO_printf(out, "%*s%s", indent, "", value);
|
sl@0
|
132 |
#else
|
sl@0
|
133 |
{
|
sl@0
|
134 |
int len;
|
sl@0
|
135 |
char *tmp;
|
sl@0
|
136 |
len = strlen(value)+1;
|
sl@0
|
137 |
tmp = OPENSSL_malloc(len);
|
sl@0
|
138 |
if (tmp)
|
sl@0
|
139 |
{
|
sl@0
|
140 |
ascii2ebcdic(tmp, value, len);
|
sl@0
|
141 |
BIO_printf(out, "%*s%s", indent, "", tmp);
|
sl@0
|
142 |
OPENSSL_free(tmp);
|
sl@0
|
143 |
}
|
sl@0
|
144 |
}
|
sl@0
|
145 |
#endif
|
sl@0
|
146 |
} else if(method->i2v) {
|
sl@0
|
147 |
if(!(nval = method->i2v(method, ext_str, NULL))) {
|
sl@0
|
148 |
ok = 0;
|
sl@0
|
149 |
goto err;
|
sl@0
|
150 |
}
|
sl@0
|
151 |
X509V3_EXT_val_prn(out, nval, indent,
|
sl@0
|
152 |
method->ext_flags & X509V3_EXT_MULTILINE);
|
sl@0
|
153 |
} else if(method->i2r) {
|
sl@0
|
154 |
if(!method->i2r(method, ext_str, out, indent)) ok = 0;
|
sl@0
|
155 |
} else ok = 0;
|
sl@0
|
156 |
|
sl@0
|
157 |
err:
|
sl@0
|
158 |
sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
|
sl@0
|
159 |
if(value) OPENSSL_free(value);
|
sl@0
|
160 |
if(method->it) ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it));
|
sl@0
|
161 |
else method->ext_free(ext_str);
|
sl@0
|
162 |
return ok;
|
sl@0
|
163 |
}
|
sl@0
|
164 |
|
sl@0
|
165 |
EXPORT_C int X509V3_extensions_print(BIO *bp, char *title, STACK_OF(X509_EXTENSION) *exts, unsigned long flag, int indent)
|
sl@0
|
166 |
{
|
sl@0
|
167 |
int i, j;
|
sl@0
|
168 |
|
sl@0
|
169 |
if(sk_X509_EXTENSION_num(exts) <= 0) return 1;
|
sl@0
|
170 |
|
sl@0
|
171 |
if(title)
|
sl@0
|
172 |
{
|
sl@0
|
173 |
BIO_printf(bp,"%*s%s:\n",indent, "", title);
|
sl@0
|
174 |
indent += 4;
|
sl@0
|
175 |
}
|
sl@0
|
176 |
|
sl@0
|
177 |
for (i=0; i<sk_X509_EXTENSION_num(exts); i++)
|
sl@0
|
178 |
{
|
sl@0
|
179 |
ASN1_OBJECT *obj;
|
sl@0
|
180 |
X509_EXTENSION *ex;
|
sl@0
|
181 |
ex=sk_X509_EXTENSION_value(exts, i);
|
sl@0
|
182 |
if (indent && BIO_printf(bp,"%*s",indent, "") <= 0) return 0;
|
sl@0
|
183 |
obj=X509_EXTENSION_get_object(ex);
|
sl@0
|
184 |
i2a_ASN1_OBJECT(bp,obj);
|
sl@0
|
185 |
j=X509_EXTENSION_get_critical(ex);
|
sl@0
|
186 |
if (BIO_printf(bp,": %s\n",j?"critical":"") <= 0)
|
sl@0
|
187 |
return 0;
|
sl@0
|
188 |
if(!X509V3_EXT_print(bp, ex, flag, indent + 4))
|
sl@0
|
189 |
{
|
sl@0
|
190 |
BIO_printf(bp, "%*s", indent + 4, "");
|
sl@0
|
191 |
M_ASN1_OCTET_STRING_print(bp,ex->value);
|
sl@0
|
192 |
}
|
sl@0
|
193 |
if (BIO_write(bp,"\n",1) <= 0) return 0;
|
sl@0
|
194 |
}
|
sl@0
|
195 |
return 1;
|
sl@0
|
196 |
}
|
sl@0
|
197 |
|
sl@0
|
198 |
static int unknown_ext_print(BIO *out, X509_EXTENSION *ext, unsigned long flag, int indent, int supported)
|
sl@0
|
199 |
{
|
sl@0
|
200 |
switch(flag & X509V3_EXT_UNKNOWN_MASK) {
|
sl@0
|
201 |
|
sl@0
|
202 |
case X509V3_EXT_DEFAULT:
|
sl@0
|
203 |
return 0;
|
sl@0
|
204 |
|
sl@0
|
205 |
case X509V3_EXT_ERROR_UNKNOWN:
|
sl@0
|
206 |
if(supported)
|
sl@0
|
207 |
BIO_printf(out, "%*s<Parse Error>", indent, "");
|
sl@0
|
208 |
else
|
sl@0
|
209 |
BIO_printf(out, "%*s<Not Supported>", indent, "");
|
sl@0
|
210 |
return 1;
|
sl@0
|
211 |
|
sl@0
|
212 |
case X509V3_EXT_PARSE_UNKNOWN:
|
sl@0
|
213 |
return ASN1_parse_dump(out,
|
sl@0
|
214 |
ext->value->data, ext->value->length, indent, -1);
|
sl@0
|
215 |
case X509V3_EXT_DUMP_UNKNOWN:
|
sl@0
|
216 |
return BIO_dump_indent(out, (char *)ext->value->data, ext->value->length, indent);
|
sl@0
|
217 |
|
sl@0
|
218 |
default:
|
sl@0
|
219 |
return 1;
|
sl@0
|
220 |
}
|
sl@0
|
221 |
}
|
sl@0
|
222 |
|
sl@0
|
223 |
|
sl@0
|
224 |
#ifndef OPENSSL_NO_FP_API
|
sl@0
|
225 |
EXPORT_C int X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
|
sl@0
|
226 |
{
|
sl@0
|
227 |
BIO *bio_tmp;
|
sl@0
|
228 |
int ret;
|
sl@0
|
229 |
if(!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE))) return 0;
|
sl@0
|
230 |
ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
|
sl@0
|
231 |
BIO_free(bio_tmp);
|
sl@0
|
232 |
return ret;
|
sl@0
|
233 |
}
|
sl@0
|
234 |
#endif
|