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_importkey_fn
*ossl_prov_get_rsa_importkey(void)
19 return ossl_prov_get_importkey(rsa_keymgmt_functions
);
22 int ossl_prov_print_rsa(BIO
*out
, RSA
*rsa
, int priv
)
24 const char *modulus_label
;
25 const char *exponent_label
;
26 const BIGNUM
*rsa_d
= NULL
, *rsa_n
= NULL
, *rsa_e
= NULL
;
27 STACK_OF(BIGNUM_const
) *factors
= sk_BIGNUM_const_new_null();
28 STACK_OF(BIGNUM_const
) *exps
= sk_BIGNUM_const_new_null();
29 STACK_OF(BIGNUM_const
) *coeffs
= sk_BIGNUM_const_new_null();
32 if (rsa
== NULL
|| factors
== NULL
|| exps
== NULL
|| coeffs
== NULL
)
35 RSA_get0_key(rsa
, &rsa_n
, &rsa_e
, &rsa_d
);
36 rsa_get0_all_params(rsa
, factors
, exps
, coeffs
);
38 if (priv
&& rsa_d
!= NULL
) {
39 if (ossl_prov_bio_printf(out
, "Private-Key: (%d bit, %d primes)\n",
41 sk_BIGNUM_const_num(factors
)) <= 0)
43 modulus_label
= "modulus:";
44 exponent_label
= "publicExponent:";
46 if (ossl_prov_bio_printf(out
, "Public-Key: (%d bit)\n",
47 BN_num_bits(rsa_n
)) <= 0)
49 modulus_label
= "Modulus:";
50 exponent_label
= "Exponent:";
52 if (!ossl_prov_print_labeled_bignum(out
, modulus_label
, rsa_n
))
54 if (!ossl_prov_print_labeled_bignum(out
, exponent_label
, rsa_e
))
59 if (!ossl_prov_print_labeled_bignum(out
, "privateExponent:", rsa_d
))
61 if (!ossl_prov_print_labeled_bignum(out
, "prime1:",
62 sk_BIGNUM_const_value(factors
, 0)))
64 if (!ossl_prov_print_labeled_bignum(out
, "prime2:",
65 sk_BIGNUM_const_value(factors
, 1)))
67 if (!ossl_prov_print_labeled_bignum(out
, "exponent1:",
68 sk_BIGNUM_const_value(exps
, 0)))
70 if (!ossl_prov_print_labeled_bignum(out
, "exponent2:",
71 sk_BIGNUM_const_value(exps
, 1)))
73 if (!ossl_prov_print_labeled_bignum(out
, "coefficient:",
74 sk_BIGNUM_const_value(coeffs
, 0)))
76 for (i
= 2; i
< sk_BIGNUM_const_num(factors
); i
++) {
77 if (ossl_prov_bio_printf(out
, "prime%d:", i
+ 1) <= 0)
79 if (!ossl_prov_print_labeled_bignum(out
, NULL
,
80 sk_BIGNUM_const_value(factors
,
83 if (ossl_prov_bio_printf(out
, "exponent%d:", i
+ 1) <= 0)
85 if (!ossl_prov_print_labeled_bignum(out
, NULL
,
86 sk_BIGNUM_const_value(exps
, i
)))
88 if (ossl_prov_bio_printf(out
, "coefficient%d:", i
+ 1) <= 0)
90 if (!ossl_prov_print_labeled_bignum(out
, NULL
,
91 sk_BIGNUM_const_value(coeffs
,
98 sk_BIGNUM_const_free(factors
);
99 sk_BIGNUM_const_free(exps
);
100 sk_BIGNUM_const_free(coeffs
);