2 * Copyright 2019 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
10 #include "crypto/rsa.h" /* rsa_get0_all_params() */
11 #include "prov/bio.h" /* ossl_prov_bio_printf() */
12 #include "prov/implementations.h" /* rsa_keymgmt_functions */
13 #include "serializer_local.h"
15 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
17 OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_rsa_new(void)
19 return ossl_prov_get_keymgmt_new(rsa_keymgmt_functions);
22 OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_rsa_free(void)
24 return ossl_prov_get_keymgmt_free(rsa_keymgmt_functions);
27 OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_rsa_import(void)
29 return ossl_prov_get_keymgmt_import(rsa_keymgmt_functions);
32 int ossl_prov_print_rsa(BIO *out, RSA *rsa, int priv)
34 const char *modulus_label;
35 const char *exponent_label;
36 const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
37 STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
38 STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
39 STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
42 if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
45 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
46 rsa_get0_all_params(rsa, factors, exps, coeffs);
48 if (priv && rsa_d != NULL) {
49 if (ossl_prov_bio_printf(out, "Private-Key: (%d bit, %d primes)\n",
51 sk_BIGNUM_const_num(factors)) <= 0)
53 modulus_label = "modulus:";
54 exponent_label = "publicExponent:";
56 if (ossl_prov_bio_printf(out, "Public-Key: (%d bit)\n",
57 BN_num_bits(rsa_n)) <= 0)
59 modulus_label = "Modulus:";
60 exponent_label = "Exponent:";
62 if (!ossl_prov_print_labeled_bignum(out, modulus_label, rsa_n))
64 if (!ossl_prov_print_labeled_bignum(out, exponent_label, rsa_e))
69 if (!ossl_prov_print_labeled_bignum(out, "privateExponent:", rsa_d))
71 if (!ossl_prov_print_labeled_bignum(out, "prime1:",
72 sk_BIGNUM_const_value(factors, 0)))
74 if (!ossl_prov_print_labeled_bignum(out, "prime2:",
75 sk_BIGNUM_const_value(factors, 1)))
77 if (!ossl_prov_print_labeled_bignum(out, "exponent1:",
78 sk_BIGNUM_const_value(exps, 0)))
80 if (!ossl_prov_print_labeled_bignum(out, "exponent2:",
81 sk_BIGNUM_const_value(exps, 1)))
83 if (!ossl_prov_print_labeled_bignum(out, "coefficient:",
84 sk_BIGNUM_const_value(coeffs, 0)))
86 for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
87 if (ossl_prov_bio_printf(out, "prime%d:", i + 1) <= 0)
89 if (!ossl_prov_print_labeled_bignum(out, NULL,
90 sk_BIGNUM_const_value(factors,
93 if (ossl_prov_bio_printf(out, "exponent%d:", i + 1) <= 0)
95 if (!ossl_prov_print_labeled_bignum(out, NULL,
96 sk_BIGNUM_const_value(exps, i)))
98 if (ossl_prov_bio_printf(out, "coefficient%d:", i + 1) <= 0)
100 if (!ossl_prov_print_labeled_bignum(out, NULL,
101 sk_BIGNUM_const_value(coeffs,
108 sk_BIGNUM_const_free(factors);
109 sk_BIGNUM_const_free(exps);
110 sk_BIGNUM_const_free(coeffs);