David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 1 | /* ==================================================================== |
| 2 | * Copyright (c) 2006 The OpenSSL Project. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in |
| 13 | * the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * |
| 16 | * 3. All advertising materials mentioning features or use of this |
| 17 | * software must display the following acknowledgment: |
| 18 | * "This product includes software developed by the OpenSSL Project |
| 19 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 20 | * |
| 21 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 22 | * endorse or promote products derived from this software without |
| 23 | * prior written permission. For written permission, please contact |
| 24 | * licensing@OpenSSL.org. |
| 25 | * |
| 26 | * 5. Products derived from this software may not be called "OpenSSL" |
| 27 | * nor may "OpenSSL" appear in their names without prior written |
| 28 | * permission of the OpenSSL Project. |
| 29 | * |
| 30 | * 6. Redistributions of any form whatsoever must retain the following |
| 31 | * acknowledgment: |
| 32 | * "This product includes software developed by the OpenSSL Project |
| 33 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 34 | * |
| 35 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 47 | * ==================================================================== |
| 48 | * |
| 49 | * This product includes cryptographic software written by Eric Young |
| 50 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 51 | * Hudson (tjh@cryptsoft.com). */ |
| 52 | |
| 53 | #include <openssl/evp.h> |
| 54 | |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 55 | #include <openssl/bio.h> |
| 56 | #include <openssl/bn.h> |
| 57 | #include <openssl/dsa.h> |
| 58 | #include <openssl/ec.h> |
| 59 | #include <openssl/ec_key.h> |
| 60 | #include <openssl/mem.h> |
| 61 | #include <openssl/rsa.h> |
| 62 | |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 63 | #include "../internal.h" |
Adam Langley | 96dec44 | 2017-05-03 11:50:51 -0700 | [diff] [blame] | 64 | #include "../fipsmodule/rsa/internal.h" |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 65 | |
| 66 | |
David Benjamin | 045129c | 2022-10-05 00:08:01 -0400 | [diff] [blame] | 67 | static int print_hex(BIO *bp, const uint8_t *data, size_t len, int off) { |
| 68 | for (size_t i = 0; i < len; i++) { |
| 69 | if ((i % 15) == 0) { |
| 70 | if (BIO_puts(bp, "\n") <= 0 || // |
| 71 | !BIO_indent(bp, off + 4, 128)) { |
| 72 | return 0; |
| 73 | } |
| 74 | } |
| 75 | if (BIO_printf(bp, "%02x%s", data[i], (i + 1 == len) ? "" : ":") <= 0) { |
| 76 | return 0; |
| 77 | } |
| 78 | } |
| 79 | if (BIO_write(bp, "\n", 1) <= 0) { |
| 80 | return 0; |
| 81 | } |
| 82 | return 1; |
| 83 | } |
| 84 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 85 | static int bn_print(BIO *bp, const char *name, const BIGNUM *num, int off) { |
David Benjamin | f4e447c | 2016-02-07 12:31:53 -0500 | [diff] [blame] | 86 | if (num == NULL) { |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | if (!BIO_indent(bp, off, 128)) { |
| 91 | return 0; |
| 92 | } |
| 93 | if (BN_is_zero(num)) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 94 | if (BIO_printf(bp, "%s 0\n", name) <= 0) { |
David Benjamin | f4e447c | 2016-02-07 12:31:53 -0500 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | return 1; |
| 98 | } |
| 99 | |
David Benjamin | 7deb831 | 2022-09-05 22:36:20 -0400 | [diff] [blame] | 100 | uint64_t u64; |
| 101 | if (BN_get_u64(num, &u64)) { |
David Benjamin | f4e447c | 2016-02-07 12:31:53 -0500 | [diff] [blame] | 102 | const char *neg = BN_is_negative(num) ? "-" : ""; |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 103 | return BIO_printf(bp, "%s %s%" PRIu64 " (%s0x%" PRIx64 ")\n", name, neg, |
| 104 | u64, neg, u64) > 0; |
| 105 | } |
| 106 | |
| 107 | if (BIO_printf(bp, "%s%s", name, |
| 108 | (BN_is_negative(num)) ? " (Negative)" : "") <= 0) { |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | // Print |num| in hex, adding a leading zero, as in ASN.1, if the high bit |
| 113 | // is set. |
| 114 | // |
| 115 | // TODO(davidben): Do we need to do this? We already print "(Negative)" above |
| 116 | // and negative values are never valid in keys anyway. |
| 117 | size_t len = BN_num_bytes(num); |
| 118 | uint8_t *buf = OPENSSL_malloc(len + 1); |
| 119 | if (buf == NULL) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | buf[0] = 0; |
| 124 | BN_bn2bin(num, buf + 1); |
David Benjamin | 045129c | 2022-10-05 00:08:01 -0400 | [diff] [blame] | 125 | int ret; |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 126 | if (len > 0 && (buf[1] & 0x80) != 0) { |
David Benjamin | 045129c | 2022-10-05 00:08:01 -0400 | [diff] [blame] | 127 | // Print the whole buffer. |
| 128 | ret = print_hex(bp, buf, len + 1, off); |
David Benjamin | f4e447c | 2016-02-07 12:31:53 -0500 | [diff] [blame] | 129 | } else { |
David Benjamin | 045129c | 2022-10-05 00:08:01 -0400 | [diff] [blame] | 130 | // Skip the leading zero. |
| 131 | ret = print_hex(bp, buf + 1, len, off); |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 132 | } |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 133 | OPENSSL_free(buf); |
| 134 | return ret; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 135 | } |
| 136 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 137 | // RSA keys. |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 138 | |
| 139 | static int do_rsa_print(BIO *out, const RSA *rsa, int off, |
| 140 | int include_private) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 141 | int mod_len = 0; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 142 | if (rsa->n != NULL) { |
| 143 | mod_len = BN_num_bits(rsa->n); |
| 144 | } |
| 145 | |
| 146 | if (!BIO_indent(out, off, 128)) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 147 | return 0; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 148 | } |
| 149 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 150 | const char *s, *str; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 151 | if (include_private && rsa->d) { |
| 152 | if (BIO_printf(out, "Private-Key: (%d bit)\n", mod_len) <= 0) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 153 | return 0; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 154 | } |
| 155 | str = "modulus:"; |
| 156 | s = "publicExponent:"; |
| 157 | } else { |
| 158 | if (BIO_printf(out, "Public-Key: (%d bit)\n", mod_len) <= 0) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 159 | return 0; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 160 | } |
| 161 | str = "Modulus:"; |
| 162 | s = "Exponent:"; |
| 163 | } |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 164 | if (!bn_print(out, str, rsa->n, off) || |
| 165 | !bn_print(out, s, rsa->e, off)) { |
| 166 | return 0; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if (include_private) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 170 | if (!bn_print(out, "privateExponent:", rsa->d, off) || |
| 171 | !bn_print(out, "prime1:", rsa->p, off) || |
| 172 | !bn_print(out, "prime2:", rsa->q, off) || |
| 173 | !bn_print(out, "exponent1:", rsa->dmp1, off) || |
| 174 | !bn_print(out, "exponent2:", rsa->dmq1, off) || |
| 175 | !bn_print(out, "coefficient:", rsa->iqmp, off)) { |
| 176 | return 0; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 177 | } |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 178 | } |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 179 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 180 | return 1; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 181 | } |
| 182 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 183 | static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
David Benjamin | 890c201 | 2023-02-08 15:24:47 -0500 | [diff] [blame^] | 184 | return do_rsa_print(bp, EVP_PKEY_get0_RSA(pkey), indent, 0); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 185 | } |
| 186 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 187 | static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
David Benjamin | 890c201 | 2023-02-08 15:24:47 -0500 | [diff] [blame^] | 188 | return do_rsa_print(bp, EVP_PKEY_get0_RSA(pkey), indent, 1); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 192 | // DSA keys. |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 193 | |
| 194 | static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 195 | const BIGNUM *priv_key = NULL; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 196 | if (ptype == 2) { |
| 197 | priv_key = x->priv_key; |
| 198 | } |
| 199 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 200 | const BIGNUM *pub_key = NULL; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 201 | if (ptype > 0) { |
| 202 | pub_key = x->pub_key; |
| 203 | } |
| 204 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 205 | const char *ktype = "DSA-Parameters"; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 206 | if (ptype == 2) { |
| 207 | ktype = "Private-Key"; |
| 208 | } else if (ptype == 1) { |
| 209 | ktype = "Public-Key"; |
| 210 | } |
| 211 | |
David Benjamin | 11c25a6 | 2022-10-04 23:59:50 -0400 | [diff] [blame] | 212 | if (!BIO_indent(bp, off, 128) || |
| 213 | BIO_printf(bp, "%s: (%u bit)\n", ktype, BN_num_bits(x->p)) <= 0 || |
| 214 | // |priv_key| and |pub_key| may be NULL, in which case |bn_print| will |
| 215 | // silently skip them. |
| 216 | !bn_print(bp, "priv:", priv_key, off) || |
David Benjamin | 1e0f042 | 2022-10-04 23:58:00 -0400 | [diff] [blame] | 217 | !bn_print(bp, "pub:", pub_key, off) || |
| 218 | !bn_print(bp, "P:", x->p, off) || |
| 219 | !bn_print(bp, "Q:", x->q, off) || |
| 220 | !bn_print(bp, "G:", x->g, off)) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 221 | return 0; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 222 | } |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 223 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 224 | return 1; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 225 | } |
| 226 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 227 | static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
David Benjamin | 890c201 | 2023-02-08 15:24:47 -0500 | [diff] [blame^] | 228 | return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 0); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 229 | } |
| 230 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 231 | static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
David Benjamin | 890c201 | 2023-02-08 15:24:47 -0500 | [diff] [blame^] | 232 | return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 1); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 233 | } |
| 234 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 235 | static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
David Benjamin | 890c201 | 2023-02-08 15:24:47 -0500 | [diff] [blame^] | 236 | return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 2); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 240 | // EC keys. |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 241 | |
| 242 | static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) { |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 243 | const EC_GROUP *group; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 244 | if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 245 | OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER); |
| 246 | return 0; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 247 | } |
| 248 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 249 | const char *ecstr; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 250 | if (ktype == 2) { |
| 251 | ecstr = "Private-Key"; |
| 252 | } else if (ktype == 1) { |
| 253 | ecstr = "Public-Key"; |
| 254 | } else { |
| 255 | ecstr = "ECDSA-Parameters"; |
| 256 | } |
| 257 | |
| 258 | if (!BIO_indent(bp, off, 128)) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 259 | return 0; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 260 | } |
David Benjamin | 054a5d3 | 2022-10-05 00:10:28 -0400 | [diff] [blame] | 261 | int curve_name = EC_GROUP_get_curve_name(group); |
| 262 | if (BIO_printf(bp, "%s: (%s)\n", ecstr, |
| 263 | curve_name == NID_undef |
| 264 | ? "unknown curve" |
| 265 | : EC_curve_nid2nist(curve_name)) <= 0) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 266 | return 0; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 267 | } |
| 268 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 269 | if (ktype == 2) { |
| 270 | const BIGNUM *priv_key = EC_KEY_get0_private_key(x); |
| 271 | if (priv_key != NULL && // |
| 272 | !bn_print(bp, "priv:", priv_key, off)) { |
| 273 | return 0; |
| 274 | } |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 275 | } |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 276 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 277 | if (ktype > 0 && EC_KEY_get0_public_key(x) != NULL) { |
| 278 | uint8_t *pub = NULL; |
| 279 | size_t pub_len = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL); |
| 280 | if (pub_len == 0) { |
| 281 | return 0; |
| 282 | } |
David Benjamin | 045129c | 2022-10-05 00:08:01 -0400 | [diff] [blame] | 283 | int ret = BIO_indent(bp, off, 128) && // |
| 284 | BIO_puts(bp, "pub:") > 0 && // |
| 285 | print_hex(bp, pub, pub_len, off); |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 286 | OPENSSL_free(pub); |
| 287 | if (!ret) { |
| 288 | return 0; |
| 289 | } |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 290 | } |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 291 | |
| 292 | return 1; |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 293 | } |
| 294 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 295 | static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
David Benjamin | 890c201 | 2023-02-08 15:24:47 -0500 | [diff] [blame^] | 296 | return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 0); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 297 | } |
| 298 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 299 | static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
David Benjamin | 890c201 | 2023-02-08 15:24:47 -0500 | [diff] [blame^] | 300 | return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 1); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 304 | static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
David Benjamin | 890c201 | 2023-02-08 15:24:47 -0500 | [diff] [blame^] | 305 | return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 2); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | |
| 309 | typedef struct { |
| 310 | int type; |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 311 | int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent); |
| 312 | int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent); |
| 313 | int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 314 | } EVP_PKEY_PRINT_METHOD; |
| 315 | |
| 316 | static EVP_PKEY_PRINT_METHOD kPrintMethods[] = { |
| 317 | { |
| 318 | EVP_PKEY_RSA, |
| 319 | rsa_pub_print, |
| 320 | rsa_priv_print, |
| 321 | NULL /* param_print */, |
| 322 | }, |
| 323 | { |
| 324 | EVP_PKEY_DSA, |
| 325 | dsa_pub_print, |
| 326 | dsa_priv_print, |
| 327 | dsa_param_print, |
| 328 | }, |
| 329 | { |
| 330 | EVP_PKEY_EC, |
| 331 | eckey_pub_print, |
| 332 | eckey_priv_print, |
| 333 | eckey_param_print, |
| 334 | }, |
| 335 | }; |
| 336 | |
Steven Valdez | cb96654 | 2016-08-17 16:56:14 -0400 | [diff] [blame] | 337 | static size_t kPrintMethodsLen = OPENSSL_ARRAY_SIZE(kPrintMethods); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 338 | |
| 339 | static EVP_PKEY_PRINT_METHOD *find_method(int type) { |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 340 | for (size_t i = 0; i < kPrintMethodsLen; i++) { |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 341 | if (kPrintMethods[i].type == type) { |
| 342 | return &kPrintMethods[i]; |
| 343 | } |
| 344 | } |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent, |
| 349 | const char *kstr) { |
| 350 | BIO_indent(out, indent, 128); |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 351 | BIO_printf(out, "%s algorithm unsupported\n", kstr); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent, |
| 356 | ASN1_PCTX *pctx) { |
David Benjamin | 890c201 | 2023-02-08 15:24:47 -0500 | [diff] [blame^] | 357 | EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey)); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 358 | if (method != NULL && method->pub_print != NULL) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 359 | return method->pub_print(out, pkey, indent); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 360 | } |
| 361 | return print_unsupported(out, pkey, indent, "Public Key"); |
| 362 | } |
| 363 | |
| 364 | int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent, |
| 365 | ASN1_PCTX *pctx) { |
David Benjamin | 890c201 | 2023-02-08 15:24:47 -0500 | [diff] [blame^] | 366 | EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey)); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 367 | if (method != NULL && method->priv_print != NULL) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 368 | return method->priv_print(out, pkey, indent); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 369 | } |
| 370 | return print_unsupported(out, pkey, indent, "Private Key"); |
| 371 | } |
| 372 | |
| 373 | int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent, |
| 374 | ASN1_PCTX *pctx) { |
David Benjamin | 890c201 | 2023-02-08 15:24:47 -0500 | [diff] [blame^] | 375 | EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey)); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 376 | if (method != NULL && method->param_print != NULL) { |
David Benjamin | 3592aa3 | 2022-09-05 23:53:13 -0400 | [diff] [blame] | 377 | return method->param_print(out, pkey, indent); |
David Benjamin | 8c07ad3 | 2015-08-07 12:34:57 -0400 | [diff] [blame] | 378 | } |
| 379 | return print_unsupported(out, pkey, indent, "Parameters"); |
| 380 | } |