2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
16 #include "crypto/rsa.h" /* rsa_get0_all_params() */
17 #include "prov/bio.h" /* ossl_prov_bio_printf() */
18 #include "prov/implementations.h" /* rsa_keymgmt_functions */
19 #include "serializer_local.h"
21 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
23 OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_rsa_new(void)
25 return ossl_prov_get_keymgmt_new(rsa_keymgmt_functions);
28 OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_rsa_free(void)
30 return ossl_prov_get_keymgmt_free(rsa_keymgmt_functions);
33 OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_rsa_import(void)
35 return ossl_prov_get_keymgmt_import(rsa_keymgmt_functions);
38 int ossl_prov_print_rsa(BIO *out, RSA *rsa, int priv)
40 const char *modulus_label;
41 const char *exponent_label;
42 const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
43 STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
44 STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
45 STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
48 if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
51 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
52 rsa_get0_all_params(rsa, factors, exps, coeffs);
54 if (priv && rsa_d != NULL) {
55 if (ossl_prov_bio_printf(out, "Private-Key: (%d bit, %d primes)\n",
57 sk_BIGNUM_const_num(factors)) <= 0)
59 modulus_label = "modulus:";
60 exponent_label = "publicExponent:";
62 if (ossl_prov_bio_printf(out, "Public-Key: (%d bit)\n",
63 BN_num_bits(rsa_n)) <= 0)
65 modulus_label = "Modulus:";
66 exponent_label = "Exponent:";
68 if (!ossl_prov_print_labeled_bignum(out, modulus_label, rsa_n))
70 if (!ossl_prov_print_labeled_bignum(out, exponent_label, rsa_e))
75 if (!ossl_prov_print_labeled_bignum(out, "privateExponent:", rsa_d))
77 if (!ossl_prov_print_labeled_bignum(out, "prime1:",
78 sk_BIGNUM_const_value(factors, 0)))
80 if (!ossl_prov_print_labeled_bignum(out, "prime2:",
81 sk_BIGNUM_const_value(factors, 1)))
83 if (!ossl_prov_print_labeled_bignum(out, "exponent1:",
84 sk_BIGNUM_const_value(exps, 0)))
86 if (!ossl_prov_print_labeled_bignum(out, "exponent2:",
87 sk_BIGNUM_const_value(exps, 1)))
89 if (!ossl_prov_print_labeled_bignum(out, "coefficient:",
90 sk_BIGNUM_const_value(coeffs, 0)))
92 for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
93 if (ossl_prov_bio_printf(out, "prime%d:", i + 1) <= 0)
95 if (!ossl_prov_print_labeled_bignum(out, NULL,
96 sk_BIGNUM_const_value(factors,
99 if (ossl_prov_bio_printf(out, "exponent%d:", i + 1) <= 0)
101 if (!ossl_prov_print_labeled_bignum(out, NULL,
102 sk_BIGNUM_const_value(exps, i)))
104 if (ossl_prov_bio_printf(out, "coefficient%d:", i + 1) <= 0)
106 if (!ossl_prov_print_labeled_bignum(out, NULL,
107 sk_BIGNUM_const_value(coeffs,
114 sk_BIGNUM_const_free(factors);
115 sk_BIGNUM_const_free(exps);
116 sk_BIGNUM_const_free(coeffs);