Adapt existing SERIALIZER implementations to the redesigned interface
[openssl.git] / providers / implementations / serializers / serializer_rsa.c
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
15 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
16
17 OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_rsa_new(void)
18 {
19     return ossl_prov_get_keymgmt_new(rsa_keymgmt_functions);
20 }
21
22 OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_rsa_free(void)
23 {
24     return ossl_prov_get_keymgmt_free(rsa_keymgmt_functions);
25 }
26
27 OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_rsa_import(void)
28 {
29     return ossl_prov_get_keymgmt_import(rsa_keymgmt_functions);
30 }
31
32 int ossl_prov_print_rsa(BIO *out, RSA *rsa, int priv)
33 {
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();
40     int ret = 0;
41
42     if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
43         goto err;
44
45     RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
46     rsa_get0_all_params(rsa, factors, exps, coeffs);
47
48     if (priv && rsa_d != NULL) {
49         if (ossl_prov_bio_printf(out, "Private-Key: (%d bit, %d primes)\n",
50                                  BN_num_bits(rsa_n),
51                                  sk_BIGNUM_const_num(factors)) <= 0)
52             goto err;
53         modulus_label = "modulus:";
54         exponent_label = "publicExponent:";
55     } else {
56         if (ossl_prov_bio_printf(out, "Public-Key: (%d bit)\n",
57                                  BN_num_bits(rsa_n)) <= 0)
58             goto err;
59         modulus_label = "Modulus:";
60         exponent_label = "Exponent:";
61     }
62     if (!ossl_prov_print_labeled_bignum(out, modulus_label, rsa_n))
63         goto err;
64     if (!ossl_prov_print_labeled_bignum(out, exponent_label, rsa_e))
65         goto err;
66     if (priv) {
67         int i;
68
69         if (!ossl_prov_print_labeled_bignum(out, "privateExponent:", rsa_d))
70             goto err;
71         if (!ossl_prov_print_labeled_bignum(out, "prime1:",
72                                             sk_BIGNUM_const_value(factors, 0)))
73             goto err;
74         if (!ossl_prov_print_labeled_bignum(out, "prime2:",
75                                             sk_BIGNUM_const_value(factors, 1)))
76             goto err;
77         if (!ossl_prov_print_labeled_bignum(out, "exponent1:",
78                                             sk_BIGNUM_const_value(exps, 0)))
79             goto err;
80         if (!ossl_prov_print_labeled_bignum(out, "exponent2:",
81                                             sk_BIGNUM_const_value(exps, 1)))
82             goto err;
83         if (!ossl_prov_print_labeled_bignum(out, "coefficient:",
84                                             sk_BIGNUM_const_value(coeffs, 0)))
85             goto err;
86         for (i = 2; i < sk_BIGNUM_const_num(factors); i++) {
87             if (ossl_prov_bio_printf(out, "prime%d:", i + 1) <= 0)
88                 goto err;
89             if (!ossl_prov_print_labeled_bignum(out, NULL,
90                                                 sk_BIGNUM_const_value(factors,
91                                                                       i)))
92                 goto err;
93             if (ossl_prov_bio_printf(out, "exponent%d:", i + 1) <= 0)
94                 goto err;
95             if (!ossl_prov_print_labeled_bignum(out, NULL,
96                                                 sk_BIGNUM_const_value(exps, i)))
97                 goto err;
98             if (ossl_prov_bio_printf(out, "coefficient%d:", i + 1) <= 0)
99                 goto err;
100             if (!ossl_prov_print_labeled_bignum(out, NULL,
101                                                 sk_BIGNUM_const_value(coeffs,
102                                                                       i - 1)))
103                 goto err;
104         }
105     }
106     ret = 1;
107  err:
108     sk_BIGNUM_const_free(factors);
109     sk_BIGNUM_const_free(exps);
110     sk_BIGNUM_const_free(coeffs);
111     return ret;
112 }
113