Deprecate the low level RSA functions.
[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 /*
11  * RSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <openssl/core_numbers.h>
17 #include <openssl/core_names.h>
18 #include <openssl/bn.h>
19 #include <openssl/err.h>
20 #include <openssl/rsa.h>
21 #include <openssl/evp.h>
22 #include <openssl/params.h>
23 #include <openssl/types.h>
24 #include "internal/param_build.h"
25 #include "prov/implementations.h"
26 #include "prov/providercommon.h"
27 #include "prov/provider_ctx.h"
28 #include "crypto/rsa.h"
29
30 static OSSL_OP_keymgmt_new_fn rsa_newdata;
31 static OSSL_OP_keymgmt_free_fn rsa_freedata;
32 static OSSL_OP_keymgmt_get_params_fn rsa_get_params;
33 static OSSL_OP_keymgmt_gettable_params_fn rsa_gettable_params;
34 static OSSL_OP_keymgmt_has_fn rsa_has;
35 static OSSL_OP_keymgmt_validate_fn rsa_validate;
36 static OSSL_OP_keymgmt_import_fn rsa_import;
37 static OSSL_OP_keymgmt_import_types_fn rsa_import_types;
38 static OSSL_OP_keymgmt_export_fn rsa_export;
39 static OSSL_OP_keymgmt_export_types_fn rsa_export_types;
40
41 #define RSA_DEFAULT_MD "SHA256"
42 #define RSA_POSSIBLE_SELECTIONS                 \
43     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS)
44
45 DEFINE_STACK_OF(BIGNUM)
46 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
47
48 static int collect_numbers(STACK_OF(BIGNUM) *numbers,
49                            const OSSL_PARAM params[], const char *key)
50 {
51     const OSSL_PARAM *p = NULL;
52
53     if (numbers == NULL)
54         return 0;
55
56     for (p = params; (p = OSSL_PARAM_locate_const(p, key)) != NULL; p++) {
57         BIGNUM *tmp = NULL;
58
59         if (!OSSL_PARAM_get_BN(p, &tmp))
60             return 0;
61         sk_BIGNUM_push(numbers, tmp);
62     }
63
64     return 1;
65 }
66
67 static int params_to_key(RSA *rsa, const OSSL_PARAM params[])
68 {
69     const OSSL_PARAM *param_n, *param_e,  *param_d;
70     BIGNUM *n = NULL, *e = NULL, *d = NULL;
71     STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
72     int is_private = 0;
73
74     if (rsa == NULL)
75         return 0;
76
77     param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
78     param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
79     param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
80
81     if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
82         || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
83         || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
84         goto err;
85
86     is_private = (d != NULL);
87
88     if (!RSA_set0_key(rsa, n, e, d))
89         goto err;
90     n = e = d = NULL;
91
92     if (is_private) {
93         if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
94                              OSSL_PKEY_PARAM_RSA_FACTOR)
95             || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
96                                 OSSL_PKEY_PARAM_RSA_EXPONENT)
97             || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
98                                 OSSL_PKEY_PARAM_RSA_COEFFICIENT))
99             goto err;
100
101         /* It's ok if this private key just has n, e and d */
102         if (sk_BIGNUM_num(factors) != 0
103             && !rsa_set0_all_params(rsa, factors, exps, coeffs))
104             goto err;
105     }
106
107     sk_BIGNUM_free(factors);
108     sk_BIGNUM_free(exps);
109     sk_BIGNUM_free(coeffs);
110     return 1;
111
112  err:
113     BN_free(n);
114     BN_free(e);
115     BN_free(d);
116     sk_BIGNUM_pop_free(factors, BN_free);
117     sk_BIGNUM_pop_free(exps, BN_free);
118     sk_BIGNUM_pop_free(coeffs, BN_free);
119     return 0;
120 }
121
122 static int export_numbers(OSSL_PARAM_BLD *tmpl, const char *key,
123                           STACK_OF(BIGNUM_const) *numbers)
124 {
125     int i, nnum;
126
127     if (numbers == NULL)
128         return 0;
129
130     nnum = sk_BIGNUM_const_num(numbers);
131
132     for (i = 0; i < nnum; i++) {
133         if (!ossl_param_bld_push_BN(tmpl, key,
134                                     sk_BIGNUM_const_value(numbers, i)))
135             return 0;
136     }
137
138     return 1;
139 }
140
141 static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *tmpl)
142 {
143     int ret = 0;
144     const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
145     STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
146     STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
147     STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
148
149     if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
150         goto err;
151
152     RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
153     rsa_get0_all_params(rsa, factors, exps, coeffs);
154
155     if (rsa_n != NULL
156         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_N, rsa_n))
157         goto err;
158     if (rsa_e != NULL
159         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, rsa_e))
160         goto err;
161     if (rsa_d != NULL
162         && !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_D, rsa_d))
163         goto err;
164
165     if (!export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_FACTOR, factors)
166         || !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_EXPONENT, exps)
167         || !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_COEFFICIENT, coeffs))
168         goto err;
169
170     ret = 1;
171  err:
172     sk_BIGNUM_const_free(factors);
173     sk_BIGNUM_const_free(exps);
174     sk_BIGNUM_const_free(coeffs);
175     return ret;
176 }
177
178 static void *rsa_newdata(void *provctx)
179 {
180     OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
181
182     return rsa_new_with_ctx(libctx);
183 }
184
185 static void rsa_freedata(void *keydata)
186 {
187     RSA_free(keydata);
188 }
189
190 static int rsa_has(void *keydata, int selection)
191 {
192     RSA *rsa = keydata;
193     int ok = 0;
194
195     if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
196         ok = 1;
197
198     ok = ok && (RSA_get0_e(rsa) != NULL);
199     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
200         ok = ok && (RSA_get0_n(rsa) != NULL);
201     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
202         ok = ok && (RSA_get0_d(rsa) != NULL);
203     return ok;
204 }
205
206 static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[])
207 {
208     RSA *rsa = keydata;
209     int ok = 1;
210
211     if (rsa == NULL)
212         return 0;
213
214     /* TODO(3.0) PSS and OAEP should bring on parameters */
215
216     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
217         ok = ok && params_to_key(rsa, params);
218
219     return ok;
220 }
221
222 static int rsa_export(void *keydata, int selection,
223                       OSSL_CALLBACK *param_callback, void *cbarg)
224 {
225     RSA *rsa = keydata;
226     OSSL_PARAM_BLD tmpl;
227     OSSL_PARAM *params = NULL;
228     int ok = 1;
229
230     if (rsa == NULL)
231         return 0;
232
233     /* TODO(3.0) PSS and OAEP should bring on parameters */
234
235     ossl_param_bld_init(&tmpl);
236
237     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
238         ok = ok && key_to_params(rsa, &tmpl);
239
240     if (!ok
241         || (params = ossl_param_bld_to_param(&tmpl)) == NULL)
242         return 0;
243
244     ok = param_callback(params, cbarg);
245     ossl_param_bld_free(params);
246     return ok;
247 }
248
249 /*
250  * This provider can export everything in an RSA key, so we use the exact
251  * same type description for export as for import.  Other providers might
252  * choose to import full keys, but only export the public parts, and will
253  * therefore have the importkey_types and importkey_types functions return
254  * different arrays.
255  */
256 static const OSSL_PARAM rsa_key_types[] = {
257     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
258     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
259     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),
260     /* We tolerate up to 10 factors... */
261     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
262     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
263     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
264     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
265     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
266     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
267     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
268     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
269     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
270     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
271     /* ..., up to 10 CRT exponents... */
272     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
273     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
274     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
275     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
276     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
277     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
278     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
279     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
280     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
281     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
282     /* ..., and up to 9 CRT coefficients */
283     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
284     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
285     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
286     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
287     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
288     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
289     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
290     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
291     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
292 };
293 /*
294  * We lied about the amount of factors, exponents and coefficients, the
295  * export and import functions can really deal with an infinite amount
296  * of these numbers.  However, RSA keys with too many primes are futile,
297  * so we at least pretend to have some limits.
298  */
299
300 static const OSSL_PARAM *rsa_imexport_types(int selection)
301 {
302     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
303         return rsa_key_types;
304     return NULL;
305 }
306
307 static const OSSL_PARAM *rsa_import_types(int selection)
308 {
309     return rsa_imexport_types(selection);
310 }
311
312
313 static const OSSL_PARAM *rsa_export_types(int selection)
314 {
315     return rsa_imexport_types(selection);
316 }
317
318 static int rsa_get_params(void *key, OSSL_PARAM params[])
319 {
320     RSA *rsa = key;
321     OSSL_PARAM *p;
322
323     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
324         && !OSSL_PARAM_set_int(p, RSA_bits(rsa)))
325         return 0;
326     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
327         && !OSSL_PARAM_set_int(p, RSA_security_bits(rsa)))
328         return 0;
329     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
330         && !OSSL_PARAM_set_int(p, RSA_size(rsa)))
331         return 0;
332
333 # if 0  /* TODO(3.0): PSS support pending */
334     if ((p = OSSL_PARAM_locate(params,
335                                OSSL_PKEY_PARAM_MANDATORY_DIGEST)) != NULL
336         && RSA_get0_pss_params(rsa) != NULL) {
337         const EVP_MD *md, *mgf1md;
338         int min_saltlen;
339
340         if (!rsa_pss_get_param(RSA_get0_pss_params(rsa),
341                                &md, &mgf1md, &min_saltlen)) {
342             ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
343             return 0;
344         }
345         if (!OSSL_PARAM_set_utf8_string(p, EVP_MD_name(md)))
346             return 0;
347     }
348 #endif
349     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
350 /* TODO(3.0): PSS support pending */
351 #if 0
352             && RSA_get0_pss_params(rsa) == NULL
353 #endif
354             ) {
355         if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD))
356             return 0;
357     }
358
359     return 1;
360 }
361
362 static const OSSL_PARAM rsa_params[] = {
363     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
364     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
365     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
366     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
367     OSSL_PARAM_END
368 };
369
370 static const OSSL_PARAM *rsa_gettable_params(void)
371 {
372     return rsa_params;
373 }
374
375 static int rsa_validate(void *keydata, int selection)
376 {
377     RSA *rsa = keydata;
378     int ok = 0;
379
380     if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
381         ok = 1;
382
383     /* If the whole key is selected, we do a pairwise validation */
384     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
385         == OSSL_KEYMGMT_SELECT_KEYPAIR) {
386         ok = ok && rsa_validate_pairwise(rsa);
387     } else {
388         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
389             ok = ok && rsa_validate_private(rsa);
390         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
391             ok = ok && rsa_validate_public(rsa);
392     }
393     return ok;
394 }
395
396 const OSSL_DISPATCH rsa_keymgmt_functions[] = {
397     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsa_newdata },
398     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
399     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params },
400     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params },
401     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
402     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
403     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
404     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types },
405     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
406     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types },
407     { 0, NULL }
408 };