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