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