]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/serializers/serializer_rsa.c
Update copyright year
[thirdparty/openssl.git] / providers / implementations / serializers / serializer_rsa.c
CommitLineData
677add38 1/*
33388b44 2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
677add38
RL
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
c5f87134
P
10/*
11 * RSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
677add38
RL
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"
20
21DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
22
32b0645c 23OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_rsa_new(void)
677add38 24{
32b0645c
RL
25 return ossl_prov_get_keymgmt_new(rsa_keymgmt_functions);
26}
27
28OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_rsa_free(void)
29{
30 return ossl_prov_get_keymgmt_free(rsa_keymgmt_functions);
31}
32
33OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_rsa_import(void)
34{
35 return ossl_prov_get_keymgmt_import(rsa_keymgmt_functions);
677add38
RL
36}
37
38int ossl_prov_print_rsa(BIO *out, RSA *rsa, int priv)
39{
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();
46 int ret = 0;
47
48 if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
49 goto err;
50
51 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
52 rsa_get0_all_params(rsa, factors, exps, coeffs);
53
54 if (priv && rsa_d != NULL) {
55 if (ossl_prov_bio_printf(out, "Private-Key: (%d bit, %d primes)\n",
56 BN_num_bits(rsa_n),
57 sk_BIGNUM_const_num(factors)) <= 0)
58 goto err;
59 modulus_label = "modulus:";
60 exponent_label = "publicExponent:";
61 } else {
62 if (ossl_prov_bio_printf(out, "Public-Key: (%d bit)\n",
63 BN_num_bits(rsa_n)) <= 0)
64 goto err;
65 modulus_label = "Modulus:";
66 exponent_label = "Exponent:";
67 }
68 if (!ossl_prov_print_labeled_bignum(out, modulus_label, rsa_n))
69 goto err;
70 if (!ossl_prov_print_labeled_bignum(out, exponent_label, rsa_e))
71 goto err;
72 if (priv) {
73 int i;
74
75 if (!ossl_prov_print_labeled_bignum(out, "privateExponent:", rsa_d))
76 goto err;
77 if (!ossl_prov_print_labeled_bignum(out, "prime1:",
78 sk_BIGNUM_const_value(factors, 0)))
79 goto err;
80 if (!ossl_prov_print_labeled_bignum(out, "prime2:",
81 sk_BIGNUM_const_value(factors, 1)))
82 goto err;
83 if (!ossl_prov_print_labeled_bignum(out, "exponent1:",
84 sk_BIGNUM_const_value(exps, 0)))
85 goto err;
86 if (!ossl_prov_print_labeled_bignum(out, "exponent2:",
87 sk_BIGNUM_const_value(exps, 1)))
88 goto err;
89 if (!ossl_prov_print_labeled_bignum(out, "coefficient:",
90 sk_BIGNUM_const_value(coeffs, 0)))
91 goto err;
92 for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
93 if (ossl_prov_bio_printf(out, "prime%d:", i + 1) <= 0)
94 goto err;
95 if (!ossl_prov_print_labeled_bignum(out, NULL,
96 sk_BIGNUM_const_value(factors,
97 i)))
98 goto err;
99 if (ossl_prov_bio_printf(out, "exponent%d:", i + 1) <= 0)
100 goto err;
101 if (!ossl_prov_print_labeled_bignum(out, NULL,
102 sk_BIGNUM_const_value(exps, i)))
103 goto err;
104 if (ossl_prov_bio_printf(out, "coefficient%d:", i + 1) <= 0)
105 goto err;
106 if (!ossl_prov_print_labeled_bignum(out, NULL,
107 sk_BIGNUM_const_value(coeffs,
108 i - 1)))
109 goto err;
110 }
111 }
112 ret = 1;
113 err:
114 sk_BIGNUM_const_free(factors);
115 sk_BIGNUM_const_free(exps);
116 sk_BIGNUM_const_free(coeffs);
117 return ret;
118}