3bf175b752ed09acc22bae3843335a814fb9bd02
[openssl.git] / providers / implementations / keymgmt / rsa_kmgmt.c
1 /*
2  * Copyright 2019-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 /*
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 "prov/implementations.h"
23 #include "prov/providercommon.h"
24 #include "prov/provider_ctx.h"
25 #include "crypto/rsa.h"
26 #include "internal/param_build_set.h"
27
28 static OSSL_OP_keymgmt_new_fn rsa_newdata;
29 static OSSL_OP_keymgmt_gen_init_fn rsa_gen_init;
30 static OSSL_OP_keymgmt_gen_set_params_fn rsa_gen_set_params;
31 static OSSL_OP_keymgmt_gen_settable_params_fn rsa_gen_settable_params;
32 static OSSL_OP_keymgmt_gen_fn rsa_gen;
33 static OSSL_OP_keymgmt_gen_cleanup_fn rsa_gen_cleanup;
34 static OSSL_OP_keymgmt_free_fn rsa_freedata;
35 static OSSL_OP_keymgmt_get_params_fn rsa_get_params;
36 static OSSL_OP_keymgmt_gettable_params_fn rsa_gettable_params;
37 static OSSL_OP_keymgmt_has_fn rsa_has;
38 static OSSL_OP_keymgmt_match_fn rsa_match;
39 static OSSL_OP_keymgmt_validate_fn rsa_validate;
40 static OSSL_OP_keymgmt_import_fn rsa_import;
41 static OSSL_OP_keymgmt_import_types_fn rsa_import_types;
42 static OSSL_OP_keymgmt_export_fn rsa_export;
43 static OSSL_OP_keymgmt_export_types_fn rsa_export_types;
44
45 #define RSA_DEFAULT_MD "SHA256"
46 #define RSA_POSSIBLE_SELECTIONS                                                \
47     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS)
48
49 DEFINE_STACK_OF(BIGNUM)
50 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
51
52 static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
53 {
54     int ret = 0;
55     const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
56     STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
57     STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
58     STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
59
60     if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
61         goto err;
62
63     RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
64     rsa_get0_all_params(rsa, factors, exps, coeffs);
65
66     if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n)
67         || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e)
68         || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D, rsa_d)
69         || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_factor_names,
70                                               factors)
71         || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_exp_names,
72                                               exps)
73         || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_coeff_names,
74                                               coeffs))
75         goto err;
76     ret = 1;
77  err:
78     sk_BIGNUM_const_free(factors);
79     sk_BIGNUM_const_free(exps);
80     sk_BIGNUM_const_free(coeffs);
81     return ret;
82 }
83
84 static void *rsa_newdata(void *provctx)
85 {
86     OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
87
88     return rsa_new_with_ctx(libctx);
89 }
90
91 static void rsa_freedata(void *keydata)
92 {
93     RSA_free(keydata);
94 }
95
96 static int rsa_has(void *keydata, int selection)
97 {
98     RSA *rsa = keydata;
99     int ok = 0;
100
101     if (rsa != NULL) {
102         if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
103             ok = 1;
104
105         if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
106             ok = ok && 0;     /* This will change with PSS and OAEP */
107         if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
108             ok = ok && (RSA_get0_e(rsa) != NULL);
109         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
110             ok = ok && (RSA_get0_n(rsa) != NULL);
111         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
112             ok = ok && (RSA_get0_d(rsa) != NULL);
113     }
114     return ok;
115 }
116
117 static int rsa_match(const void *keydata1, const void *keydata2, int selection)
118 {
119     const RSA *rsa1 = keydata1;
120     const RSA *rsa2 = keydata2;
121     int ok = 1;
122
123     /* There is always an |e| */
124     ok = ok && BN_cmp(RSA_get0_e(rsa1), RSA_get0_e(rsa2)) == 0;
125     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
126         ok = ok && BN_cmp(RSA_get0_n(rsa1), RSA_get0_n(rsa2)) == 0;
127     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
128         ok = ok && BN_cmp(RSA_get0_d(rsa1), RSA_get0_d(rsa2)) == 0;
129     return ok;
130 }
131
132 static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[])
133 {
134     RSA *rsa = keydata;
135     int ok = 1;
136
137     if (rsa == NULL)
138         return 0;
139
140     /* TODO(3.0) PSS and OAEP should bring on parameters */
141
142     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
143         ok = ok && rsa_fromdata(rsa, params);
144
145     return ok;
146 }
147
148 static int rsa_export(void *keydata, int selection,
149                       OSSL_CALLBACK *param_callback, void *cbarg)
150 {
151     RSA *rsa = keydata;
152     OSSL_PARAM_BLD *tmpl;
153     OSSL_PARAM *params = NULL;
154     int ok = 1;
155
156     if (rsa == NULL)
157         return 0;
158
159     /* TODO(3.0) PSS and OAEP should bring on parameters */
160
161     tmpl = OSSL_PARAM_BLD_new();
162     if (tmpl == NULL)
163         return 0;
164
165     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
166         ok = ok && key_to_params(rsa, tmpl, NULL);
167
168     if (!ok
169         || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
170         goto err;
171
172     ok = param_callback(params, cbarg);
173     OSSL_PARAM_BLD_free_params(params);
174 err:
175     OSSL_PARAM_BLD_free(tmpl);
176     return ok;
177 }
178
179 #ifdef FIPS_MODULE
180 /* In fips mode there are no multi-primes. */
181 # define RSA_KEY_MP_TYPES()                                                    \
182 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0),                           \
183 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0),                           \
184 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0),                         \
185 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0),                         \
186 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0),
187 #else
188 /*
189  * We allow up to 10 prime factors (starting with p, q).
190  * NOTE: there is only 9 OSSL_PKEY_PARAM_RSA_COEFFICIENT
191  */
192 # define RSA_KEY_MP_TYPES()                                                    \
193 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0),                           \
194 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0),                           \
195 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR3, NULL, 0),                           \
196 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR4, NULL, 0),                           \
197 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR5, NULL, 0),                           \
198 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR6, NULL, 0),                           \
199 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR7, NULL, 0),                           \
200 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR8, NULL, 0),                           \
201 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR9, NULL, 0),                           \
202 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR10, NULL, 0),                          \
203 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0),                         \
204 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0),                         \
205 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT3, NULL, 0),                         \
206 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT4, NULL, 0),                         \
207 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT5, NULL, 0),                         \
208 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT6, NULL, 0),                         \
209 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT7, NULL, 0),                         \
210 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT8, NULL, 0),                         \
211 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT9, NULL, 0),                         \
212 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT10, NULL, 0),                        \
213 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0),                      \
214 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT2, NULL, 0),                      \
215 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT3, NULL, 0),                      \
216 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT4, NULL, 0),                      \
217 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT5, NULL, 0),                      \
218 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT6, NULL, 0),                      \
219 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT7, NULL, 0),                      \
220 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT8, NULL, 0),                      \
221 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT9, NULL, 0),
222 #endif
223
224 #define RSA_KEY_TYPES()                                                        \
225 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),                                 \
226 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),                                 \
227 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),                                 \
228 RSA_KEY_MP_TYPES()
229
230 /*
231  * This provider can export everything in an RSA key, so we use the exact
232  * same type description for export as for import.  Other providers might
233  * choose to import full keys, but only export the public parts, and will
234  * therefore have the importkey_types and importkey_types functions return
235  * different arrays.
236  */
237 static const OSSL_PARAM rsa_key_types[] = {
238     RSA_KEY_TYPES()
239     OSSL_PARAM_END
240 };
241 /*
242  * We lied about the amount of factors, exponents and coefficients, the
243  * export and import functions can really deal with an infinite amount
244  * of these numbers.  However, RSA keys with too many primes are futile,
245  * so we at least pretend to have some limits.
246  */
247
248 static const OSSL_PARAM *rsa_imexport_types(int selection)
249 {
250     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
251         return rsa_key_types;
252     return NULL;
253 }
254
255 static const OSSL_PARAM *rsa_import_types(int selection)
256 {
257     return rsa_imexport_types(selection);
258 }
259
260 static const OSSL_PARAM *rsa_export_types(int selection)
261 {
262     return rsa_imexport_types(selection);
263 }
264
265 static int rsa_get_params(void *key, OSSL_PARAM params[])
266 {
267     RSA *rsa = key;
268     OSSL_PARAM *p;
269
270     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
271         && !OSSL_PARAM_set_int(p, RSA_bits(rsa)))
272         return 0;
273     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
274         && !OSSL_PARAM_set_int(p, RSA_security_bits(rsa)))
275         return 0;
276     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
277         && !OSSL_PARAM_set_int(p, RSA_size(rsa)))
278         return 0;
279
280 # if 0  /* TODO(3.0): PSS support pending */
281     if ((p = OSSL_PARAM_locate(params,
282                                OSSL_PKEY_PARAM_MANDATORY_DIGEST)) != NULL
283         && RSA_get0_pss_params(rsa) != NULL) {
284         const EVP_MD *md, *mgf1md;
285         int min_saltlen;
286
287         if (!rsa_pss_get_param(RSA_get0_pss_params(rsa),
288                                &md, &mgf1md, &min_saltlen)) {
289             ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
290             return 0;
291         }
292         if (!OSSL_PARAM_set_utf8_string(p, EVP_MD_name(md)))
293             return 0;
294     }
295 #endif
296     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
297 /* TODO(3.0): PSS support pending */
298 #if 0
299             && RSA_get0_pss_params(rsa) == NULL
300 #endif
301             ) {
302         if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD))
303             return 0;
304     }
305     return key_to_params(rsa, NULL, params);
306 }
307
308 static const OSSL_PARAM rsa_params[] = {
309     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
310     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
311     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
312     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
313     RSA_KEY_TYPES()
314     OSSL_PARAM_END
315 };
316
317 static const OSSL_PARAM *rsa_gettable_params(void)
318 {
319     return rsa_params;
320 }
321
322 static int rsa_validate(void *keydata, int selection)
323 {
324     RSA *rsa = keydata;
325     int ok = 0;
326
327     if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
328         ok = 1;
329
330     /* If the whole key is selected, we do a pairwise validation */
331     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
332         == OSSL_KEYMGMT_SELECT_KEYPAIR) {
333         ok = ok && rsa_validate_pairwise(rsa);
334     } else {
335         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
336             ok = ok && rsa_validate_private(rsa);
337         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
338             ok = ok && rsa_validate_public(rsa);
339     }
340     return ok;
341 }
342
343 struct rsa_gen_ctx {
344     OPENSSL_CTX *libctx;
345
346     size_t nbits;
347     BIGNUM *pub_exp;
348     size_t primes;
349
350     /* For generation callback */
351     OSSL_CALLBACK *cb;
352     void *cbarg;
353 };
354
355 static int rsa_gencb(int p, int n, BN_GENCB *cb)
356 {
357     struct rsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
358     OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
359
360     params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
361     params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
362
363     return gctx->cb(params, gctx->cbarg);
364 }
365
366 static void *rsa_gen_init(void *provctx, int selection)
367 {
368     OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
369     struct rsa_gen_ctx *gctx = NULL;
370
371     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
372         return NULL;
373
374     if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
375         gctx->libctx = libctx;
376         if ((gctx->pub_exp = BN_new()) == NULL
377             || !BN_set_word(gctx->pub_exp, RSA_F4)) {
378             BN_free(gctx->pub_exp);
379             gctx = NULL;
380         } else {
381             gctx->nbits = 2048;
382             gctx->primes = RSA_DEFAULT_PRIME_NUM;
383         }
384     }
385     return gctx;
386 }
387
388 static int rsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
389 {
390     struct rsa_gen_ctx *gctx = genctx;
391     const OSSL_PARAM *p;
392
393     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_BITS)) != NULL
394         && !OSSL_PARAM_get_size_t(p, &gctx->nbits))
395         return 0;
396     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PRIMES)) != NULL
397         && !OSSL_PARAM_get_size_t(p, &gctx->primes))
398         return 0;
399     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E)) != NULL
400         && !OSSL_PARAM_get_BN(p, &gctx->pub_exp))
401         return 0;
402     return 1;
403 }
404
405 static const OSSL_PARAM *rsa_gen_settable_params(void *provctx)
406 {
407     static OSSL_PARAM settable[] = {
408         OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_BITS, NULL),
409         OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, NULL),
410         OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
411         OSSL_PARAM_END
412     };
413
414     return settable;
415 }
416
417 static void *rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
418 {
419     struct rsa_gen_ctx *gctx = genctx;
420     RSA *rsa = NULL;
421     BN_GENCB *gencb = NULL;
422
423     if (gctx == NULL
424         || (rsa = rsa_new_with_ctx(gctx->libctx)) == NULL)
425         return NULL;
426
427     gctx->cb = osslcb;
428     gctx->cbarg = cbarg;
429     gencb = BN_GENCB_new();
430     if (gencb != NULL)
431         BN_GENCB_set(gencb, rsa_gencb, genctx);
432
433     if (!RSA_generate_multi_prime_key(rsa, (int)gctx->nbits, (int)gctx->primes,
434                                       gctx->pub_exp, gencb)) {
435         RSA_free(rsa);
436         rsa = NULL;
437     }
438
439     BN_GENCB_free(gencb);
440
441     return rsa;
442 }
443
444 static void rsa_gen_cleanup(void *genctx)
445 {
446     struct rsa_gen_ctx *gctx = genctx;
447
448     if (gctx == NULL)
449         return;
450
451     BN_clear_free(gctx->pub_exp);
452     OPENSSL_free(gctx);
453 }
454
455 const OSSL_DISPATCH rsa_keymgmt_functions[] = {
456     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsa_newdata },
457     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsa_gen_init },
458     { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS,
459       (void (*)(void))rsa_gen_set_params },
460     { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
461       (void (*)(void))rsa_gen_settable_params },
462     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen },
463     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup },
464     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
465     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params },
466     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params },
467     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
468     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))rsa_match },
469     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
470     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
471     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types },
472     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
473     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types },
474     { 0, NULL }
475 };