RSA: Extract much of the rsa_pkey_export_to() code to a separate function
[oweals/openssl.git] / crypto / rsa / rsa_backend.c
1 /*
2  * Copyright 2020 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 <openssl/core_names.h>
11 #include <openssl/params.h>
12 #include "crypto/rsa.h"
13
14 /*
15  * The intention with the "backend" source file is to offer backend support
16  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
17  * implementations alike.
18  */
19
20 DEFINE_STACK_OF(BIGNUM)
21
22 static int collect_numbers(STACK_OF(BIGNUM) *numbers,
23                            const OSSL_PARAM params[], const char *names[])
24 {
25     const OSSL_PARAM *p = NULL;
26     int i;
27
28     if (numbers == NULL)
29         return 0;
30
31     for (i = 0; names[i] != NULL; i++){
32         p = OSSL_PARAM_locate_const(params, names[i]);
33         if (p != NULL) {
34             BIGNUM *tmp = NULL;
35
36             if (!OSSL_PARAM_get_BN(p, &tmp)
37                 || sk_BIGNUM_push(numbers, tmp) == 0)
38                 return 0;
39         }
40     }
41
42     return 1;
43 }
44
45 int rsa_fromdata(RSA *rsa, const OSSL_PARAM params[])
46 {
47     const OSSL_PARAM *param_n, *param_e,  *param_d;
48     BIGNUM *n = NULL, *e = NULL, *d = NULL;
49     STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
50     int is_private = 0;
51
52     if (rsa == NULL)
53         return 0;
54
55     param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
56     param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
57     param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
58
59     if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
60         || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
61         || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
62         goto err;
63
64     is_private = (d != NULL);
65
66     if (!RSA_set0_key(rsa, n, e, d))
67         goto err;
68     n = e = d = NULL;
69
70     if (is_private) {
71         if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
72                              rsa_mp_factor_names)
73             || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
74                                 rsa_mp_exp_names)
75             || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
76                                 rsa_mp_coeff_names))
77             goto err;
78
79         /* It's ok if this private key just has n, e and d */
80         if (sk_BIGNUM_num(factors) != 0
81             && !rsa_set0_all_params(rsa, factors, exps, coeffs))
82             goto err;
83     }
84
85     sk_BIGNUM_free(factors);
86     sk_BIGNUM_free(exps);
87     sk_BIGNUM_free(coeffs);
88     return 1;
89
90  err:
91     BN_free(n);
92     BN_free(e);
93     BN_free(d);
94     sk_BIGNUM_pop_free(factors, BN_free);
95     sk_BIGNUM_pop_free(exps, BN_free);
96     sk_BIGNUM_pop_free(coeffs, BN_free);
97     return 0;
98 }
99
100 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
101
102 int rsa_todata(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
103 {
104     int ret = 0;
105     const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
106     STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
107     STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
108     STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
109
110     if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
111         goto err;
112
113     RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
114     rsa_get0_all_params(rsa, factors, exps, coeffs);
115
116     /* Check private key data integrity */
117     if (rsa_d != NULL) {
118         int numprimes = sk_BIGNUM_const_num(factors);
119         int numexps = sk_BIGNUM_const_num(exps);
120         int numcoeffs = sk_BIGNUM_const_num(coeffs);
121
122         /*
123          * It's permisssible to have zero primes, i.e. no CRT params.
124          * Otherwise, there must be at least two, as many exponents,
125          * and one coefficient less.
126          */
127         if (numprimes != 0
128             && (numprimes < 2 || numexps < 2 || numcoeffs < 1))
129             goto err;
130     }
131
132     if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n)
133         || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e)
134         || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D, rsa_d)
135         || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_factor_names,
136                                               factors)
137         || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_exp_names,
138                                               exps)
139         || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_coeff_names,
140                                               coeffs))
141         goto err;
142     ret = 1;
143  err:
144     sk_BIGNUM_const_free(factors);
145     sk_BIGNUM_const_free(exps);
146     sk_BIGNUM_const_free(coeffs);
147     return ret;
148 }