| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | // X509 v3 extension utilities |
| 16 |
| 17 | #include <stdio.h> |
| 18 |
| 19 | #include <openssl/bio.h> |
| 20 | #include <openssl/conf.h> |
| 21 | #include <openssl/mem.h> |
| 22 | #include <openssl/x509.h> |
| 23 |
| 24 | // Extension printing routines |
| 25 |
| 26 | static int unknown_ext_print(BIO *out, const X509_EXTENSION *ext, |
| 27 | unsigned long flag, int indent, int supported); |
| 28 |
| 29 | // Print out a name+value stack |
| 30 | static void X509V3_EXT_val_prn(BIO *out, const STACK_OF(CONF_VALUE) *val, |
| 31 | int indent, int ml) { |
| 32 | if (!val) { |
| 33 | return; |
| 34 | } |
| 35 | if (!ml || !sk_CONF_VALUE_num(val)) { |
| 36 | BIO_printf(out, "%*s", indent, ""); |
| 37 | if (!sk_CONF_VALUE_num(val)) { |
| 38 | BIO_puts(out, "<EMPTY>\n"); |
| 39 | } |
| 40 | } |
| 41 | for (size_t i = 0; i < sk_CONF_VALUE_num(val); i++) { |
| 42 | if (ml) { |
| 43 | BIO_printf(out, "%*s", indent, ""); |
| 44 | } else if (i > 0) { |
| 45 | BIO_printf(out, ", "); |
| 46 | } |
| 47 | const CONF_VALUE *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 | } else { |
| 53 | BIO_printf(out, "%s:%s", nval->name, nval->value); |
| 54 | } |
| 55 | if (ml) { |
| 56 | BIO_puts(out, "\n"); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 |
| 61 | // Main routine: print out a general extension |
| 62 |
| 63 | int X509V3_EXT_print(BIO *out, const X509_EXTENSION *ext, unsigned long flag, |
| 64 | int indent) { |
| 65 | const X509V3_EXT_METHOD *method = X509V3_EXT_get(ext); |
| 66 | if (method == NULL) { |
| 67 | return unknown_ext_print(out, ext, flag, indent, 0); |
| 68 | } |
| 69 | const ASN1_STRING *ext_data = X509_EXTENSION_get_data(ext); |
| 70 | const unsigned char *p = ASN1_STRING_get0_data(ext_data); |
| 71 | void *ext_str = ASN1_item_d2i(NULL, &p, ASN1_STRING_length(ext_data), |
| 72 | ASN1_ITEM_ptr(method->it)); |
| 73 | if (!ext_str) { |
| 74 | return unknown_ext_print(out, ext, flag, indent, 1); |
| 75 | } |
| 76 |
| 77 | char *value = NULL; |
| 78 | STACK_OF(CONF_VALUE) *nval = NULL; |
| 79 | int ok = 0; |
| 80 | if (method->i2s) { |
| 81 | if (!(value = method->i2s(method, ext_str))) { |
| 82 | goto err; |
| 83 | } |
| 84 | BIO_printf(out, "%*s%s", indent, "", value); |
| 85 | } else if (method->i2v) { |
| 86 | if (!(nval = method->i2v(method, ext_str, NULL))) { |
| 87 | goto err; |
| 88 | } |
| 89 | X509V3_EXT_val_prn(out, nval, indent, |
| 90 | method->ext_flags & X509V3_EXT_MULTILINE); |
| 91 | } else if (method->i2r) { |
| 92 | if (!method->i2r(method, ext_str, out, indent)) { |
| 93 | goto err; |
| 94 | } |
| 95 | } else { |
| 96 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_OPERATION_NOT_DEFINED); |
| 97 | goto err; |
| 98 | } |
| 99 |
| 100 | ok = 1; |
| 101 |
| 102 | err: |
| 103 | sk_CONF_VALUE_pop_free(nval, X509V3_conf_free); |
| 104 | OPENSSL_free(value); |
| 105 | ASN1_item_free(reinterpret_cast<ASN1_VALUE *>(ext_str), |
| 106 | ASN1_ITEM_ptr(method->it)); |
| 107 | return ok; |
| 108 | } |
| 109 |
| 110 | int X509V3_extensions_print(BIO *bp, const char *title, |
| 111 | const STACK_OF(X509_EXTENSION) *exts, |
| 112 | unsigned long flag, int indent) { |
| 113 | size_t i; |
| 114 | int j; |
| 115 |
| 116 | if (sk_X509_EXTENSION_num(exts) <= 0) { |
| 117 | return 1; |
| 118 | } |
| 119 |
| 120 | if (title) { |
| 121 | BIO_printf(bp, "%*s%s:\n", indent, "", title); |
| 122 | indent += 4; |
| 123 | } |
| 124 |
| 125 | for (i = 0; i < sk_X509_EXTENSION_num(exts); i++) { |
| 126 | const X509_EXTENSION *ex = sk_X509_EXTENSION_value(exts, i); |
| 127 | if (indent && BIO_printf(bp, "%*s", indent, "") <= 0) { |
| 128 | return 0; |
| 129 | } |
| 130 | const ASN1_OBJECT *obj = X509_EXTENSION_get_object(ex); |
| 131 | i2a_ASN1_OBJECT(bp, obj); |
| 132 | j = X509_EXTENSION_get_critical(ex); |
| 133 | if (BIO_printf(bp, ": %s\n", j ? "critical" : "") <= 0) { |
| 134 | return 0; |
| 135 | } |
| 136 | if (!X509V3_EXT_print(bp, ex, flag, indent + 4)) { |
| 137 | BIO_printf(bp, "%*s", indent + 4, ""); |
| 138 | ASN1_STRING_print(bp, X509_EXTENSION_get_data(ex)); |
| 139 | } |
| 140 | if (BIO_write(bp, "\n", 1) <= 0) { |
| 141 | return 0; |
| 142 | } |
| 143 | } |
| 144 | return 1; |
| 145 | } |
| 146 |
| 147 | static int unknown_ext_print(BIO *out, const X509_EXTENSION *ext, |
| 148 | unsigned long flag, int indent, int supported) { |
| 149 | switch (flag & X509V3_EXT_UNKNOWN_MASK) { |
| 150 | case X509V3_EXT_DEFAULT: |
| 151 | return 0; |
| 152 |
| 153 | case X509V3_EXT_ERROR_UNKNOWN: |
| 154 | if (supported) { |
| 155 | BIO_printf(out, "%*s<Parse Error>", indent, ""); |
| 156 | } else { |
| 157 | BIO_printf(out, "%*s<Not Supported>", indent, ""); |
| 158 | } |
| 159 | return 1; |
| 160 |
| 161 | case X509V3_EXT_PARSE_UNKNOWN: |
| 162 | case X509V3_EXT_DUMP_UNKNOWN: { |
| 163 | const ASN1_STRING *data = X509_EXTENSION_get_data(ext); |
| 164 | return BIO_hexdump(out, ASN1_STRING_get0_data(data), |
| 165 | ASN1_STRING_length(data), indent); |
| 166 | } |
| 167 |
| 168 | default: |
| 169 | return 1; |
| 170 | } |
| 171 | } |
| 172 |
| 173 | int X509V3_EXT_print_fp(FILE *fp, const X509_EXTENSION *ext, int flag, |
| 174 | int indent) { |
| 175 | BIO *bio_tmp; |
| 176 | int ret; |
| 177 | if (!(bio_tmp = BIO_new_fp(fp, BIO_NOCLOSE))) { |
| 178 | return 0; |
| 179 | } |
| 180 | ret = X509V3_EXT_print(bio_tmp, ext, flag, indent); |
| 181 | BIO_free(bio_tmp); |
| 182 | return ret; |
| 183 | } |
| 184 |