2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
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"
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;
45 #define RSA_DEFAULT_MD "SHA256"
46 #define RSA_POSSIBLE_SELECTIONS \
47 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS)
49 DEFINE_STACK_OF(BIGNUM)
50 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
52 static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
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();
60 if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
63 RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
64 rsa_get0_all_params(rsa, factors, exps, coeffs);
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,
71 || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_exp_names,
73 || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_coeff_names,
78 sk_BIGNUM_const_free(factors);
79 sk_BIGNUM_const_free(exps);
80 sk_BIGNUM_const_free(coeffs);
84 static void *rsa_newdata(void *provctx)
86 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
88 return rsa_new_with_ctx(libctx);
91 static void rsa_freedata(void *keydata)
96 static int rsa_has(void *keydata, int selection)
102 if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
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);
117 static int rsa_match(const void *keydata1, const void *keydata2, int selection)
119 const RSA *rsa1 = keydata1;
120 const RSA *rsa2 = keydata2;
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;
132 static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[])
140 /* TODO(3.0) PSS and OAEP should bring on parameters */
142 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
143 ok = ok && rsa_fromdata(rsa, params);
148 static int rsa_export(void *keydata, int selection,
149 OSSL_CALLBACK *param_callback, void *cbarg)
152 OSSL_PARAM_BLD *tmpl;
153 OSSL_PARAM *params = NULL;
159 /* TODO(3.0) PSS and OAEP should bring on parameters */
161 tmpl = OSSL_PARAM_BLD_new();
165 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
166 ok = ok && key_to_params(rsa, tmpl, NULL);
169 || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
172 ok = param_callback(params, cbarg);
173 OSSL_PARAM_BLD_free_params(params);
175 OSSL_PARAM_BLD_free(tmpl);
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),
189 * We allow up to 10 prime factors (starting with p, q).
190 * NOTE: there is only 9 OSSL_PKEY_PARAM_RSA_COEFFICIENT
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),
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), \
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
237 static const OSSL_PARAM rsa_key_types[] = {
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.
248 static const OSSL_PARAM *rsa_imexport_types(int selection)
250 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
251 return rsa_key_types;
255 static const OSSL_PARAM *rsa_import_types(int selection)
257 return rsa_imexport_types(selection);
260 static const OSSL_PARAM *rsa_export_types(int selection)
262 return rsa_imexport_types(selection);
265 static int rsa_get_params(void *key, OSSL_PARAM params[])
270 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
271 && !OSSL_PARAM_set_int(p, RSA_bits(rsa)))
273 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
274 && !OSSL_PARAM_set_int(p, RSA_security_bits(rsa)))
276 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
277 && !OSSL_PARAM_set_int(p, RSA_size(rsa)))
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;
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);
292 if (!OSSL_PARAM_set_utf8_string(p, EVP_MD_name(md)))
296 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
297 /* TODO(3.0): PSS support pending */
299 && RSA_get0_pss_params(rsa) == NULL
302 if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD))
305 return key_to_params(rsa, NULL, params);
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),
317 static const OSSL_PARAM *rsa_gettable_params(void)
322 static int rsa_validate(void *keydata, int selection)
327 if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
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);
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);
350 /* For generation callback */
355 static int rsa_gencb(int p, int n, BN_GENCB *cb)
357 struct rsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
358 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
360 params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
361 params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
363 return gctx->cb(params, gctx->cbarg);
366 static void *rsa_gen_init(void *provctx, int selection)
368 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
369 struct rsa_gen_ctx *gctx = NULL;
371 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
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);
382 gctx->primes = RSA_DEFAULT_PRIME_NUM;
388 static int rsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
390 struct rsa_gen_ctx *gctx = genctx;
393 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_BITS)) != NULL
394 && !OSSL_PARAM_get_size_t(p, &gctx->nbits))
396 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PRIMES)) != NULL
397 && !OSSL_PARAM_get_size_t(p, &gctx->primes))
399 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E)) != NULL
400 && !OSSL_PARAM_get_BN(p, &gctx->pub_exp))
405 static const OSSL_PARAM *rsa_gen_settable_params(void *provctx)
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),
417 static void *rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
419 struct rsa_gen_ctx *gctx = genctx;
421 BN_GENCB *gencb = NULL;
424 || (rsa = rsa_new_with_ctx(gctx->libctx)) == NULL)
429 gencb = BN_GENCB_new();
431 BN_GENCB_set(gencb, rsa_gencb, genctx);
433 if (!RSA_generate_multi_prime_key(rsa, (int)gctx->nbits, (int)gctx->primes,
434 gctx->pub_exp, gencb)) {
439 BN_GENCB_free(gencb);
444 static void rsa_gen_cleanup(void *genctx)
446 struct rsa_gen_ctx *gctx = genctx;
451 BN_clear_free(gctx->pub_exp);
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 },