a1f81041b8b438bd9bf8719c8b155dafd1a02b09
[openssl.git] / providers / implementations / keymgmt / rsa_kmgmt.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 <openssl/core_numbers.h>
11 #include <openssl/core_names.h>
12 #include <openssl/bn.h>
13 #include <openssl/rsa.h>
14 #include <openssl/params.h>
15 #include <openssl/types.h>
16 #include "internal/param_build.h"
17 #include "prov/implementations.h"
18 #include "prov/providercommon.h"
19 #include "crypto/rsa.h"
20
21 static OSSL_OP_keymgmt_importkey_fn rsa_importkey;
22 static OSSL_OP_keymgmt_exportkey_fn rsa_exportkey;
23 static OSSL_OP_keymgmt_get_key_params_fn rsa_get_key_params;
24
25 DEFINE_STACK_OF(BIGNUM)
26 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
27
28 static int collect_numbers(STACK_OF(BIGNUM) *numbers,
29                            const OSSL_PARAM params[], const char *key)
30 {
31     const OSSL_PARAM *p = NULL;
32
33     if (numbers == NULL)
34         return 0;
35
36     for (p = params; (p = OSSL_PARAM_locate_const(p, key)) != NULL; p++) {
37         BIGNUM *tmp = NULL;
38
39         if (!OSSL_PARAM_get_BN(p, &tmp))
40             return 0;
41         sk_BIGNUM_push(numbers, tmp);
42     }
43
44     return 1;
45 }
46
47 static int params_to_key(RSA *rsa, const OSSL_PARAM params[])
48 {
49     const OSSL_PARAM *param_n, *param_e,  *param_d;
50     BIGNUM *n = NULL, *e = NULL, *d = NULL;
51     STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
52     int is_private = 0;
53
54     if (rsa == NULL)
55         return 0;
56
57     param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
58     param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
59     param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
60
61     if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
62         || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
63         || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
64         goto err;
65
66     is_private = (d != NULL);
67
68     if (!RSA_set0_key(rsa, n, e, d))
69         goto err;
70     n = e = d = NULL;
71
72     if (is_private) {
73         if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
74                              OSSL_PKEY_PARAM_RSA_FACTOR)
75             || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
76                                 OSSL_PKEY_PARAM_RSA_EXPONENT)
77             || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
78                                 OSSL_PKEY_PARAM_RSA_COEFFICIENT))
79             goto err;
80
81         /* It's ok if this private key just has n, e and d */
82         if (sk_BIGNUM_num(factors) != 0
83             && !rsa_set0_all_params(rsa, factors, exps, coeffs))
84             goto err;
85     }
86
87     sk_BIGNUM_free(factors);
88     sk_BIGNUM_free(exps);
89     sk_BIGNUM_free(coeffs);
90     return 1;
91
92  err:
93     BN_free(n);
94     BN_free(e);
95     BN_free(d);
96     sk_BIGNUM_pop_free(factors, BN_free);
97     sk_BIGNUM_pop_free(exps, BN_free);
98     sk_BIGNUM_pop_free(coeffs, BN_free);
99     return 0;
100 }
101
102 static int export_numbers(OSSL_PARAM_BLD *tmpl, const char *key,
103                           STACK_OF(BIGNUM_const) *numbers)
104 {
105     int i, nnum;
106
107     if (numbers == NULL)
108         return 0;
109
110     nnum = sk_BIGNUM_const_num(numbers);
111
112     for (i = 0; i < nnum; i++) {
113         if (!ossl_param_bld_push_BN(tmpl, key,
114                                     sk_BIGNUM_const_value(numbers, i)))
115             return 0;
116     }
117
118     return 1;
119 }
120
121 static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *tmpl)
122 {
123     int ret = 0;
124     const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
125     STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
126     STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
127     STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
128
129     if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
130         goto err;
131
132     RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
133     rsa_get0_all_params(rsa, factors, exps, coeffs);
134
135     if (rsa_n != NULL
136         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_N, rsa_n))
137         goto err;
138     if (rsa_e != NULL
139         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, rsa_e))
140         goto err;
141     if (rsa_d != NULL
142         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_D, rsa_d))
143         goto err;
144
145     if (!export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_FACTOR, factors)
146         || !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_EXPONENT, exps)
147         || !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_COEFFICIENT, coeffs))
148         goto err;
149
150     ret = 1;
151  err:
152     sk_BIGNUM_const_free(factors);
153     sk_BIGNUM_const_free(exps);
154     sk_BIGNUM_const_free(coeffs);
155     return ret;
156 }
157
158 static void *rsa_importkey(void *provctx, const OSSL_PARAM params[])
159 {
160     RSA *rsa;
161
162     if ((rsa = RSA_new()) == NULL
163         || !params_to_key(rsa, params)) {
164         RSA_free(rsa);
165         rsa = NULL;
166     }
167     return rsa;
168 }
169
170 static int rsa_exportkey(void *key, OSSL_CALLBACK *param_callback, void *cbarg)
171 {
172     RSA *rsa = key;
173     OSSL_PARAM_BLD tmpl;
174     OSSL_PARAM *params = NULL;
175     int ret;
176
177     ossl_param_bld_init(&tmpl);
178     if (rsa == NULL
179         || !key_to_params(rsa, &tmpl)
180         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
181         return 0;
182     ret = param_callback(params, cbarg);
183     ossl_param_bld_free(params);
184     return ret;
185 }
186
187 /*
188  * This provider can export everything in an RSA key, so we use the exact
189  * same type description for export as for import.  Other providers might
190  * choose to import full keys, but only export the public parts, and will
191  * therefore have the importkey_types and importkey_types functions return
192  * different arrays.
193  */
194 static const OSSL_PARAM rsa_key_types[] = {
195     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
196     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
197     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),
198     /* We tolerate up to 10 factors... */
199     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
200     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
201     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
202     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
203     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
204     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
205     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
206     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
207     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
208     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
209     /* ..., up to 10 CRT exponents... */
210     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
211     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
212     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
213     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
214     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
215     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
216     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
217     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
218     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
219     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
220     /* ..., and up to 9 CRT coefficients */
221     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
222     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
223     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
224     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
225     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
226     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
227     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
228     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
229     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
230 };
231 /*
232  * We lied about the amount of factors, exponents and coefficients, the
233  * export and import functions can really deal with an infinite amount
234  * of these numbers.  However, RSA keys with too many primes are futile,
235  * so we at least pretend to have some limits.
236  */
237
238 static const OSSL_PARAM *rsa_exportkey_types(void)
239 {
240     return rsa_key_types;
241 }
242
243 static const OSSL_PARAM *rsa_importkey_types(void)
244 {
245     return rsa_key_types;
246 }
247
248 static int rsa_get_key_params(void *key, OSSL_PARAM params[])
249 {
250     RSA *rsa = key;
251     OSSL_PARAM *p;
252
253     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
254         && !OSSL_PARAM_set_int(p, RSA_bits(rsa)))
255         return 0;
256     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
257         && !OSSL_PARAM_set_int(p, RSA_security_bits(rsa)))
258         return 0;
259     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
260         && !OSSL_PARAM_set_int(p, RSA_size(rsa)))
261         return 0;
262     return 1;
263 }
264
265 const OSSL_DISPATCH rsa_keymgmt_functions[] = {
266     { OSSL_FUNC_KEYMGMT_IMPORTKEY, (void (*)(void))rsa_importkey },
267     { OSSL_FUNC_KEYMGMT_IMPORTKEY_TYPES, (void (*)(void))rsa_importkey_types },
268     { OSSL_FUNC_KEYMGMT_EXPORTKEY, (void (*)(void))rsa_exportkey },
269     { OSSL_FUNC_KEYMGMT_EXPORTKEY_TYPES, (void (*)(void))rsa_exportkey_types },
270     { OSSL_FUNC_KEYMGMT_FREEKEY, (void (*)(void))RSA_free },
271     { OSSL_FUNC_KEYMGMT_GET_KEY_PARAMS,  (void (*) (void))rsa_get_key_params },
272     { 0, NULL }
273 };