1/*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
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"
14
15DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
16
17OSSL_OP_keymgmt_importkey_fn *ossl_prov_get_rsa_importkey(void)
18{
19 return ossl_prov_get_importkey(rsa_keymgmt_functions);
20}
21
22int ossl_prov_print_rsa(BIO *out, RSA *rsa, int priv)
23{
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();
30 int ret = 0;
31
32 if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
33 goto err;
34
35 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
36 rsa_get0_all_params(rsa, factors, exps, coeffs);
37
38 if (priv && rsa_d != NULL) {
39 if (ossl_prov_bio_printf(out, "Private-Key: (%d bit, %d primes)\n",
40 BN_num_bits(rsa_n),
41 sk_BIGNUM_const_num(factors)) <= 0)
42 goto err;
43 modulus_label = "modulus:";
44 exponent_label = "publicExponent:";
45 } else {
46 if (ossl_prov_bio_printf(out, "Public-Key: (%d bit)\n",
47 BN_num_bits(rsa_n)) <= 0)
48 goto err;
49 modulus_label = "Modulus:";
50 exponent_label = "Exponent:";
51 }
52 if (!ossl_prov_print_labeled_bignum(out, modulus_label, rsa_n))
53 goto err;
54 if (!ossl_prov_print_labeled_bignum(out, exponent_label, rsa_e))
55 goto err;
56 if (priv) {
57 int i;
58
59 if (!ossl_prov_print_labeled_bignum(out, "privateExponent:", rsa_d))
60 goto err;
61 if (!ossl_prov_print_labeled_bignum(out, "prime1:",
62 sk_BIGNUM_const_value(factors, 0)))
63 goto err;
64 if (!ossl_prov_print_labeled_bignum(out, "prime2:",
65 sk_BIGNUM_const_value(factors, 1)))
66 goto err;
67 if (!ossl_prov_print_labeled_bignum(out, "exponent1:",
68 sk_BIGNUM_const_value(exps, 0)))
69 goto err;
70 if (!ossl_prov_print_labeled_bignum(out, "exponent2:",
71 sk_BIGNUM_const_value(exps, 1)))
72 goto err;
73 if (!ossl_prov_print_labeled_bignum(out, "coefficient:",
74 sk_BIGNUM_const_value(coeffs, 0)))
75 goto err;
76 for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
77 if (ossl_prov_bio_printf(out, "prime%d:", i + 1) <= 0)
78 goto err;
79 if (!ossl_prov_print_labeled_bignum(out, NULL,
80 sk_BIGNUM_const_value(factors,
81 i)))
82 goto err;
83 if (ossl_prov_bio_printf(out, "exponent%d:", i + 1) <= 0)
84 goto err;
85 if (!ossl_prov_print_labeled_bignum(out, NULL,
86 sk_BIGNUM_const_value(exps, i)))
87 goto err;
88 if (ossl_prov_bio_printf(out, "coefficient%d:", i + 1) <= 0)
89 goto err;
90 if (!ossl_prov_print_labeled_bignum(out, NULL,
91 sk_BIGNUM_const_value(coeffs,
92 i - 1)))
93 goto err;
94 }
95 }
96 ret = 1;
97 err:
98 sk_BIGNUM_const_free(factors);
99 sk_BIGNUM_const_free(exps);
100 sk_BIGNUM_const_free(coeffs);
101 return ret;
102}
103
104