| 1 |
// Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 1999. |
| 2 |
// Copyright (c) 1999 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
// X509 v3 extension utilities |
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
|
| 9 |
#include <openssl/bio.h> |
| 10 |
#include <openssl/conf.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
#include <openssl/x509.h> |
| 13 |
|
| 14 |
#include "internal.h" |
| 15 |
|
| 16 |
// Extension printing routines |
| 17 |
|
| 18 |
static int unknown_ext_print(BIO *out, const X509_EXTENSION *ext, |
| 19 |
unsigned long flag, int indent, int supported); |
| 20 |
|
| 21 |
// Print out a name+value stack |
| 22 |
static void X509V3_EXT_val_prn(BIO *out, const STACK_OF(CONF_VALUE) *val, |
| 23 |
int indent, int ml) { |
| 24 |
if (!val) { |
| 25 |
return; |
| 26 |
} |
| 27 |
if (!ml || !sk_CONF_VALUE_num(val)) { |
| 28 |
BIO_printf(out, "%*s", indent, ""); |
| 29 |
if (!sk_CONF_VALUE_num(val)) { |
| 30 |
BIO_puts(out, "<EMPTY>\n"); |
| 31 |
} |
| 32 |
} |
| 33 |
for (size_t i = 0; i < sk_CONF_VALUE_num(val); i++) { |
| 34 |
if (ml) { |
| 35 |
BIO_printf(out, "%*s", indent, ""); |
| 36 |
} else if (i > 0) { |
| 37 |
BIO_printf(out, ", "); |
| 38 |
} |
| 39 |
const CONF_VALUE *nval = sk_CONF_VALUE_value(val, i); |
| 40 |
if (!nval->name) { |
| 41 |
BIO_puts(out, nval->value); |
| 42 |
} else if (!nval->value) { |
| 43 |
BIO_puts(out, nval->name); |
| 44 |
} else { |
| 45 |
BIO_printf(out, "%s:%s", nval->name, nval->value); |
| 46 |
} |
| 47 |
if (ml) { |
| 48 |
BIO_puts(out, "\n"); |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Main routine: print out a general extension |
| 54 |
|
| 55 |
int X509V3_EXT_print(BIO *out, const X509_EXTENSION *ext, unsigned long flag, |
| 56 |
int indent) { |
| 57 |
void *ext_str = NULL; |
| 58 |
const X509V3_EXT_METHOD *method = X509V3_EXT_get(ext); |
| 59 |
if (method == NULL) { |
| 60 |
return unknown_ext_print(out, ext, flag, indent, 0); |
| 61 |
} |
| 62 |
const ASN1_STRING *ext_data = X509_EXTENSION_get_data(ext); |
| 63 |
const unsigned char *p = ASN1_STRING_get0_data(ext_data); |
| 64 |
if (method->it) { |
| 65 |
ext_str = ASN1_item_d2i(NULL, &p, ASN1_STRING_length(ext_data), |
| 66 |
ASN1_ITEM_ptr(method->it)); |
| 67 |
} else if (method->ext_nid == NID_id_pkix_OCSP_Nonce && method->d2i != NULL) { |
| 68 |
// |NID_id_pkix_OCSP_Nonce| is the only extension using the "old-style" |
| 69 |
// ASN.1 callbacks for backwards compatibility reasons. |
| 70 |
// Note: See |v3_ext_method| under "include/openssl/x509.h". |
| 71 |
ext_str = method->d2i(NULL, &p, ASN1_STRING_length(ext_data)); |
| 72 |
} else { |
| 73 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_OPERATION_NOT_DEFINED); |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
if (!ext_str) { |
| 78 |
return unknown_ext_print(out, ext, flag, indent, 1); |
| 79 |
} |
| 80 |
|
| 81 |
char *value = NULL; |
| 82 |
STACK_OF(CONF_VALUE) *nval = NULL; |
| 83 |
int ok = 0; |
| 84 |
if (method->i2s) { |
| 85 |
if (!(value = method->i2s(method, ext_str))) { |
| 86 |
goto err; |
| 87 |
} |
| 88 |
BIO_printf(out, "%*s%s", indent, "", value); |
| 89 |
} else if (method->i2v) { |
| 90 |
if (!(nval = method->i2v(method, ext_str, NULL))) { |
| 91 |
goto err; |
| 92 |
} |
| 93 |
X509V3_EXT_val_prn(out, nval, indent, |
| 94 |
method->ext_flags & X509V3_EXT_MULTILINE); |
| 95 |
} else if (method->i2r) { |
| 96 |
if (!method->i2r(method, ext_str, out, indent)) { |
| 97 |
goto err; |
| 98 |
} |
| 99 |
} else { |
| 100 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_OPERATION_NOT_DEFINED); |
| 101 |
goto err; |
| 102 |
} |
| 103 |
|
| 104 |
ok = 1; |
| 105 |
|
| 106 |
err: |
| 107 |
sk_CONF_VALUE_pop_free(nval, X509V3_conf_free); |
| 108 |
OPENSSL_free(value); |
| 109 |
x509v3_ext_free_with_method(method, ext_str); |
| 110 |
return ok; |
| 111 |
} |
| 112 |
|
| 113 |
int X509V3_extensions_print(BIO *bp, const char *title, |
| 114 |
const STACK_OF(X509_EXTENSION) *exts, |
| 115 |
unsigned long flag, int indent) { |
| 116 |
size_t i; |
| 117 |
int j; |
| 118 |
|
| 119 |
if (sk_X509_EXTENSION_num(exts) <= 0) { |
| 120 |
return 1; |
| 121 |
} |
| 122 |
|
| 123 |
if (title) { |
| 124 |
BIO_printf(bp, "%*s%s:\n", indent, "", title); |
| 125 |
indent += 4; |
| 126 |
} |
| 127 |
|
| 128 |
for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { |
| 129 |
const X509_EXTENSION *ex = sk_X509_EXTENSION_value(exts, i); |
| 130 |
if (indent && BIO_printf(bp, "%*s", indent, "") <= 0) { |
| 131 |
return 0; |
| 132 |
} |
| 133 |
const ASN1_OBJECT *obj = X509_EXTENSION_get_object(ex); |
| 134 |
i2a_ASN1_OBJECT(bp, obj); |
| 135 |
j = X509_EXTENSION_get_critical(ex); |
| 136 |
if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0) { |
| 137 |
return 0; |
| 138 |
} |
| 139 |
if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) { |
| 140 |
BIO_printf(bp, "%*s", indent + 4, ""); |
| 141 |
ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex)); |
| 142 |
} |
| 143 |
if (BIO_write(bp, "\n", 1) <= 0) { |
| 144 |
return 0; |
| 145 |
} |
| 146 |
} |
| 147 |
return 1; |
| 148 |
} |
| 149 |
|
| 150 |
static int unknown_ext_print(BIO *out, const X509_EXTENSION *ext, |
| 151 |
unsigned long flag, int indent, int supported) { |
| 152 |
switch (flag & X509V3_EXT_UNKNOWN_MASK) { |
| 153 |
case X509V3_EXT_DEFAULT: |
| 154 |
return 0; |
| 155 |
|
| 156 |
case X509V3_EXT_ERROR_UNKNOWN: |
| 157 |
if (supported) { |
| 158 |
BIO_printf(out, "%*s<Parse Error>", indent, ""); |
| 159 |
} else { |
| 160 |
BIO_printf(out, "%*s<Not Supported>", indent, ""); |
| 161 |
} |
| 162 |
return 1; |
| 163 |
|
| 164 |
case X509V3_EXT_PARSE_UNKNOWN: |
| 165 |
case X509V3_EXT_DUMP_UNKNOWN: { |
| 166 |
const ASN1_STRING *data = X509_EXTENSION_get_data(ext); |
| 167 |
return BIO_hexdump(out, ASN1_STRING_get0_data(data), |
| 168 |
ASN1_STRING_length(data), indent); |
| 169 |
} |
| 170 |
|
| 171 |
default: |
| 172 |
return 1; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
int X509V3_EXT_print_fp(FILE *fp, const X509_EXTENSION *ext, int flag, |
| 177 |
int indent) { |
| 178 |
BIO *bio_tmp; |
| 179 |
int ret; |
| 180 |
if (!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE))) { |
| 181 |
return 0; |
| 182 |
} |
| 183 |
ret = X509V3_EXT_print(bio_tmp, ext, flag, indent); |
| 184 |
BIO_free(bio_tmp); |
| 185 |
return ret; |
| 186 |
} |