| 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 | #include <openssl/evp.h> |
| 16 |
| 17 | #include <openssl/bio.h> |
| 18 | #include <openssl/bn.h> |
| 19 | #include <openssl/dsa.h> |
| 20 | #include <openssl/ec.h> |
| 21 | #include <openssl/ec_key.h> |
| 22 | #include <openssl/mem.h> |
| 23 | #include <openssl/rsa.h> |
| 24 |
| 25 | #include "../fipsmodule/rsa/internal.h" |
| 26 | #include "../internal.h" |
| 27 |
| 28 |
| 29 | static int print_hex(BIO *bp, const uint8_t *data, size_t len, int off) { |
| 30 | for (size_t i = 0; i < len; i++) { |
| 31 | if ((i % 15) == 0) { |
| 32 | if (BIO_puts(bp, "\n") <= 0 || // |
| 33 | !BIO_indent(bp, off + 4, 128)) { |
| 34 | return 0; |
| 35 | } |
| 36 | } |
| 37 | if (BIO_printf(bp, "%02x%s", data[i], (i + 1 == len) ? "" : ":") <= 0) { |
| 38 | return 0; |
| 39 | } |
| 40 | } |
| 41 | if (BIO_write(bp, "\n", 1) <= 0) { |
| 42 | return 0; |
| 43 | } |
| 44 | return 1; |
| 45 | } |
| 46 |
| 47 | static int bn_print(BIO *bp, const char *name, const BIGNUM *num, int off) { |
| 48 | if (num == NULL) { |
| 49 | return 1; |
| 50 | } |
| 51 |
| 52 | if (!BIO_indent(bp, off, 128)) { |
| 53 | return 0; |
| 54 | } |
| 55 | if (BN_is_zero(num)) { |
| 56 | if (BIO_printf(bp, "%s 0\n", name) <= 0) { |
| 57 | return 0; |
| 58 | } |
| 59 | return 1; |
| 60 | } |
| 61 |
| 62 | uint64_t u64; |
| 63 | if (BN_get_u64(num, &u64)) { |
| 64 | const char *neg = BN_is_negative(num) ? "-" : ""; |
| 65 | return BIO_printf(bp, "%s %s%" PRIu64 " (%s0x%" PRIx64 ")\n", name, neg, |
| 66 | u64, neg, u64) > 0; |
| 67 | } |
| 68 |
| 69 | if (BIO_printf(bp, "%s%s", name, |
| 70 | (BN_is_negative(num)) ? " (Negative)" : "") <= 0) { |
| 71 | return 0; |
| 72 | } |
| 73 |
| 74 | // Print |num| in hex, adding a leading zero, as in ASN.1, if the high bit |
| 75 | // is set. |
| 76 | // |
| 77 | // TODO(davidben): Do we need to do this? We already print "(Negative)" above |
| 78 | // and negative values are never valid in keys anyway. |
| 79 | size_t len = BN_num_bytes(num); |
| 80 | uint8_t *buf = reinterpret_cast<uint8_t *>(OPENSSL_malloc(len + 1)); |
| 81 | if (buf == NULL) { |
| 82 | return 0; |
| 83 | } |
| 84 |
| 85 | buf[0] = 0; |
| 86 | BN_bn2bin(num, buf + 1); |
| 87 | int ret; |
| 88 | if (len > 0 && (buf[1] & 0x80) != 0) { |
| 89 | // Print the whole buffer. |
| 90 | ret = print_hex(bp, buf, len + 1, off); |
| 91 | } else { |
| 92 | // Skip the leading zero. |
| 93 | ret = print_hex(bp, buf + 1, len, off); |
| 94 | } |
| 95 | OPENSSL_free(buf); |
| 96 | return ret; |
| 97 | } |
| 98 |
| 99 | // RSA keys. |
| 100 |
| 101 | static int do_rsa_print(BIO *out, const RSA *rsa, int off, |
| 102 | int include_private) { |
| 103 | int mod_len = 0; |
| 104 | if (rsa->n != NULL) { |
| 105 | mod_len = BN_num_bits(rsa->n); |
| 106 | } |
| 107 |
| 108 | if (!BIO_indent(out, off, 128)) { |
| 109 | return 0; |
| 110 | } |
| 111 |
| 112 | const char *s, *str; |
| 113 | if (include_private && rsa->d) { |
| 114 | if (BIO_printf(out, "Private-Key: (%d bit)\n", mod_len) <= 0) { |
| 115 | return 0; |
| 116 | } |
| 117 | str = "modulus:"; |
| 118 | s = "publicExponent:"; |
| 119 | } else { |
| 120 | if (BIO_printf(out, "Public-Key: (%d bit)\n", mod_len) <= 0) { |
| 121 | return 0; |
| 122 | } |
| 123 | str = "Modulus:"; |
| 124 | s = "Exponent:"; |
| 125 | } |
| 126 | if (!bn_print(out, str, rsa->n, off) || !bn_print(out, s, rsa->e, off)) { |
| 127 | return 0; |
| 128 | } |
| 129 |
| 130 | if (include_private) { |
| 131 | if (!bn_print(out, "privateExponent:", rsa->d, off) || |
| 132 | !bn_print(out, "prime1:", rsa->p, off) || |
| 133 | !bn_print(out, "prime2:", rsa->q, off) || |
| 134 | !bn_print(out, "exponent1:", rsa->dmp1, off) || |
| 135 | !bn_print(out, "exponent2:", rsa->dmq1, off) || |
| 136 | !bn_print(out, "coefficient:", rsa->iqmp, off)) { |
| 137 | return 0; |
| 138 | } |
| 139 | } |
| 140 |
| 141 | return 1; |
| 142 | } |
| 143 |
| 144 | static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
| 145 | return do_rsa_print(bp, EVP_PKEY_get0_RSA(pkey), indent, 0); |
| 146 | } |
| 147 |
| 148 | static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
| 149 | return do_rsa_print(bp, EVP_PKEY_get0_RSA(pkey), indent, 1); |
| 150 | } |
| 151 |
| 152 |
| 153 | // DSA keys. |
| 154 |
| 155 | static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) { |
| 156 | const BIGNUM *priv_key = NULL; |
| 157 | if (ptype == 2) { |
| 158 | priv_key = DSA_get0_priv_key(x); |
| 159 | } |
| 160 |
| 161 | const BIGNUM *pub_key = NULL; |
| 162 | if (ptype > 0) { |
| 163 | pub_key = DSA_get0_pub_key(x); |
| 164 | } |
| 165 |
| 166 | const char *ktype = "DSA-Parameters"; |
| 167 | if (ptype == 2) { |
| 168 | ktype = "Private-Key"; |
| 169 | } else if (ptype == 1) { |
| 170 | ktype = "Public-Key"; |
| 171 | } |
| 172 |
| 173 | if (!BIO_indent(bp, off, 128) || |
| 174 | BIO_printf(bp, "%s: (%u bit)\n", ktype, BN_num_bits(DSA_get0_p(x))) <= |
| 175 | 0 || |
| 176 | // |priv_key| and |pub_key| may be NULL, in which case |bn_print| will |
| 177 | // silently skip them. |
| 178 | !bn_print(bp, "priv:", priv_key, off) || |
| 179 | !bn_print(bp, "pub:", pub_key, off) || |
| 180 | !bn_print(bp, "P:", DSA_get0_p(x), off) || |
| 181 | !bn_print(bp, "Q:", DSA_get0_q(x), off) || |
| 182 | !bn_print(bp, "G:", DSA_get0_g(x), off)) { |
| 183 | return 0; |
| 184 | } |
| 185 |
| 186 | return 1; |
| 187 | } |
| 188 |
| 189 | static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
| 190 | return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 0); |
| 191 | } |
| 192 |
| 193 | static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
| 194 | return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 1); |
| 195 | } |
| 196 |
| 197 | static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
| 198 | return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 2); |
| 199 | } |
| 200 |
| 201 |
| 202 | // EC keys. |
| 203 |
| 204 | static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) { |
| 205 | const EC_GROUP *group; |
| 206 | if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) { |
| 207 | OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER); |
| 208 | return 0; |
| 209 | } |
| 210 |
| 211 | const char *ecstr; |
| 212 | if (ktype == 2) { |
| 213 | ecstr = "Private-Key"; |
| 214 | } else if (ktype == 1) { |
| 215 | ecstr = "Public-Key"; |
| 216 | } else { |
| 217 | ecstr = "ECDSA-Parameters"; |
| 218 | } |
| 219 |
| 220 | if (!BIO_indent(bp, off, 128)) { |
| 221 | return 0; |
| 222 | } |
| 223 | int curve_name = EC_GROUP_get_curve_name(group); |
| 224 | if (BIO_printf(bp, "%s: (%s)\n", ecstr, |
| 225 | curve_name == NID_undef |
| 226 | ? "unknown curve" |
| 227 | : EC_curve_nid2nist(curve_name)) <= 0) { |
| 228 | return 0; |
| 229 | } |
| 230 |
| 231 | if (ktype == 2) { |
| 232 | const BIGNUM *priv_key = EC_KEY_get0_private_key(x); |
| 233 | if (priv_key != NULL && // |
| 234 | !bn_print(bp, "priv:", priv_key, off)) { |
| 235 | return 0; |
| 236 | } |
| 237 | } |
| 238 |
| 239 | if (ktype > 0 && EC_KEY_get0_public_key(x) != NULL) { |
| 240 | uint8_t *pub = NULL; |
| 241 | size_t pub_len = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL); |
| 242 | if (pub_len == 0) { |
| 243 | return 0; |
| 244 | } |
| 245 | int ret = BIO_indent(bp, off, 128) && // |
| 246 | BIO_puts(bp, "pub:") > 0 && // |
| 247 | print_hex(bp, pub, pub_len, off); |
| 248 | OPENSSL_free(pub); |
| 249 | if (!ret) { |
| 250 | return 0; |
| 251 | } |
| 252 | } |
| 253 |
| 254 | return 1; |
| 255 | } |
| 256 |
| 257 | static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
| 258 | return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 0); |
| 259 | } |
| 260 |
| 261 | static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
| 262 | return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 1); |
| 263 | } |
| 264 |
| 265 |
| 266 | static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) { |
| 267 | return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 2); |
| 268 | } |
| 269 |
| 270 |
| 271 | typedef struct { |
| 272 | int type; |
| 273 | int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent); |
| 274 | int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent); |
| 275 | int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent); |
| 276 | } EVP_PKEY_PRINT_METHOD; |
| 277 |
| 278 | static EVP_PKEY_PRINT_METHOD kPrintMethods[] = { |
| 279 | { |
| 280 | EVP_PKEY_RSA, |
| 281 | rsa_pub_print, |
| 282 | rsa_priv_print, |
| 283 | NULL /* param_print */, |
| 284 | }, |
| 285 | { |
| 286 | EVP_PKEY_DSA, |
| 287 | dsa_pub_print, |
| 288 | dsa_priv_print, |
| 289 | dsa_param_print, |
| 290 | }, |
| 291 | { |
| 292 | EVP_PKEY_EC, |
| 293 | eckey_pub_print, |
| 294 | eckey_priv_print, |
| 295 | eckey_param_print, |
| 296 | }, |
| 297 | }; |
| 298 |
| 299 | static size_t kPrintMethodsLen = OPENSSL_ARRAY_SIZE(kPrintMethods); |
| 300 |
| 301 | static EVP_PKEY_PRINT_METHOD *find_method(int type) { |
| 302 | for (size_t i = 0; i < kPrintMethodsLen; i++) { |
| 303 | if (kPrintMethods[i].type == type) { |
| 304 | return &kPrintMethods[i]; |
| 305 | } |
| 306 | } |
| 307 | return NULL; |
| 308 | } |
| 309 |
| 310 | static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent, |
| 311 | const char *kstr) { |
| 312 | BIO_indent(out, indent, 128); |
| 313 | BIO_printf(out, "%s algorithm unsupported\n", kstr); |
| 314 | return 1; |
| 315 | } |
| 316 |
| 317 | int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent, |
| 318 | ASN1_PCTX *pctx) { |
| 319 | EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey)); |
| 320 | if (method != NULL && method->pub_print != NULL) { |
| 321 | return method->pub_print(out, pkey, indent); |
| 322 | } |
| 323 | return print_unsupported(out, pkey, indent, "Public Key"); |
| 324 | } |
| 325 |
| 326 | int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent, |
| 327 | ASN1_PCTX *pctx) { |
| 328 | EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey)); |
| 329 | if (method != NULL && method->priv_print != NULL) { |
| 330 | return method->priv_print(out, pkey, indent); |
| 331 | } |
| 332 | return print_unsupported(out, pkey, indent, "Private Key"); |
| 333 | } |
| 334 |
| 335 | int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent, |
| 336 | ASN1_PCTX *pctx) { |
| 337 | EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey)); |
| 338 | if (method != NULL && method->param_print != NULL) { |
| 339 | return method->param_print(out, pkey, indent); |
| 340 | } |
| 341 | return print_unsupported(out, pkey, indent, "Parameters"); |
| 342 | } |
| 343 |