4b2ad2685b74f34ef55bca85da2842c389259d20
[openssl.git] / crypto / x509 / v3_prn.c
1 /*
2  * Copyright 1999-2020 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 /* X509 v3 extension utilities */
11
12 #include <stdio.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/conf.h>
15 #include <openssl/x509v3.h>
16
17 DEFINE_STACK_OF(CONF_VALUE)
18 DEFINE_STACK_OF(X509_EXTENSION)
19
20 /* Extension printing routines */
21
22 static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
23                              unsigned long flag, int indent, int supported);
24
25 /* Print out a name+value stack */
26
27 void X509V3_EXT_val_prn(BIO *out, STACK_OF(CONF_VALUE) *val, int indent,
28                         int ml)
29 {
30     int i;
31     CONF_VALUE *nval;
32     if (!val)
33         return;
34     if (!ml || !sk_CONF_VALUE_num(val)) {
35         BIO_printf(out, "%*s", indent, "");
36         if (!sk_CONF_VALUE_num(val))
37             BIO_puts(out, "<EMPTY>\n");
38     }
39     for (i = 0; i < sk_CONF_VALUE_num(val); i++) {
40         if (ml) {
41             if (i > 0)
42                 BIO_printf(out, "\n");
43             BIO_printf(out, "%*s", indent, "");
44         }
45         else if (i > 0)
46             BIO_printf(out, ", ");
47         nval = sk_CONF_VALUE_value(val, i);
48         if (!nval->name)
49             BIO_puts(out, nval->value);
50         else if (!nval->value)
51             BIO_puts(out, nval->name);
52 #ifndef CHARSET_EBCDIC
53         else
54             BIO_printf(out, "%s:%s", nval->name, nval->value);
55 #else
56         else {
57             int len;
58             char *tmp;
59             len = strlen(nval->value) + 1;
60             tmp = OPENSSL_malloc(len);
61             if (tmp != NULL) {
62                 ascii2ebcdic(tmp, nval->value, len);
63                 BIO_printf(out, "%s:%s", nval->name, tmp);
64                 OPENSSL_free(tmp);
65             }
66         }
67 #endif
68     }
69 }
70
71 /* Main routine: print out a general extension */
72
73 int X509V3_EXT_print(BIO *out, X509_EXTENSION *ext, unsigned long flag,
74                      int indent)
75 {
76     void *ext_str = NULL;
77     char *value = NULL;
78     ASN1_OCTET_STRING *extoct;
79     const unsigned char *p;
80     int extlen;
81     const X509V3_EXT_METHOD *method;
82     STACK_OF(CONF_VALUE) *nval = NULL;
83     int ok = 1;
84
85     extoct = X509_EXTENSION_get_data(ext);
86     p = ASN1_STRING_get0_data(extoct);
87     extlen = ASN1_STRING_length(extoct);
88
89     if ((method = X509V3_EXT_get(ext)) == NULL)
90         return unknown_ext_print(out, p, extlen, flag, indent, 0);
91     if (method->it)
92         ext_str = ASN1_item_d2i(NULL, &p, extlen, ASN1_ITEM_ptr(method->it));
93     else
94         ext_str = method->d2i(NULL, &p, extlen);
95
96     if (!ext_str)
97         return unknown_ext_print(out, p, extlen, flag, indent, 1);
98
99     if (method->i2s) {
100         if ((value = method->i2s(method, ext_str)) == NULL) {
101             ok = 0;
102             goto err;
103         }
104 #ifndef CHARSET_EBCDIC
105         BIO_printf(out, "%*s%s", indent, "", value);
106 #else
107         {
108             int len;
109             char *tmp;
110             len = strlen(value) + 1;
111             tmp = OPENSSL_malloc(len);
112             if (tmp != NULL) {
113                 ascii2ebcdic(tmp, value, len);
114                 BIO_printf(out, "%*s%s", indent, "", tmp);
115                 OPENSSL_free(tmp);
116             }
117         }
118 #endif
119     } else if (method->i2v) {
120         if ((nval = method->i2v(method, ext_str, NULL)) == NULL) {
121             ok = 0;
122             goto err;
123         }
124         X509V3_EXT_val_prn(out, nval, indent,
125                            method->ext_flags & X509V3_EXT_MULTILINE);
126     } else if (method->i2r) {
127         if (!method->i2r(method, ext_str, out, indent))
128             ok = 0;
129     } else
130         ok = 0;
131
132  err:
133     sk_CONF_VALUE_pop_free(nval, X509V3_conf_free);
134     OPENSSL_free(value);
135     if (method->it)
136         ASN1_item_free(ext_str, ASN1_ITEM_ptr(method->it));
137     else
138         method->ext_free(ext_str);
139     return ok;
140 }
141
142 int X509V3_extensions_print(BIO *bp, const char *title,
143                             const STACK_OF(X509_EXTENSION) *exts,
144                             unsigned long flag, int indent)
145 {
146     int i, j;
147
148     if (sk_X509_EXTENSION_num(exts) <= 0)
149         return 1;
150
151     if (title) {
152         BIO_printf(bp, "%*s%s:\n", indent, "", title);
153         indent += 4;
154     }
155
156     for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) {
157         ASN1_OBJECT *obj;
158         X509_EXTENSION *ex;
159
160         ex = sk_X509_EXTENSION_value(exts, i);
161         obj = X509_EXTENSION_get_object(ex);
162         if ((flag & X509_FLAG_EXTENSIONS_ONLY_KID) != 0
163                 && OBJ_obj2nid(obj) != NID_subject_key_identifier
164                 && OBJ_obj2nid(obj) != NID_authority_key_identifier)
165             continue;
166         if (indent && BIO_printf(bp, "%*s", indent, "") <= 0)
167             return 0;
168         i2a_ASN1_OBJECT(bp, obj);
169         j = X509_EXTENSION_get_critical(ex);
170         if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0)
171             return 0;
172         if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) {
173             BIO_printf(bp, "%*s", indent + 4, "");
174             ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex));
175         }
176         if (BIO_write(bp, "\n", 1) <= 0)
177             return 0;
178     }
179     return 1;
180 }
181
182 static int unknown_ext_print(BIO *out, const unsigned char *ext, int extlen,
183                              unsigned long flag, int indent, int supported)
184 {
185     switch (flag & X509V3_EXT_UNKNOWN_MASK) {
186
187     case X509V3_EXT_DEFAULT:
188         return 0;
189
190     case X509V3_EXT_ERROR_UNKNOWN:
191         if (supported)
192             BIO_printf(out, "%*s<Parse Error>", indent, "");
193         else
194             BIO_printf(out, "%*s<Not Supported>", indent, "");
195         return 1;
196
197     case X509V3_EXT_PARSE_UNKNOWN:
198         return ASN1_parse_dump(out, ext, extlen, indent, -1);
199     case X509V3_EXT_DUMP_UNKNOWN:
200         return BIO_dump_indent(out, (const char *)ext, extlen, indent);
201
202     default:
203         return 1;
204     }
205 }
206
207 #ifndef OPENSSL_NO_STDIO
208 int X509V3_EXT_print_fp(FILE *fp, X509_EXTENSION *ext, int flag, int indent)
209 {
210     BIO *bio_tmp;
211     int ret;
212
213     if ((bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL)
214         return 0;
215     ret = X509V3_EXT_print(bio_tmp, ext, flag, indent);
216     BIO_free(bio_tmp);
217     return ret;
218 }
219 #endif