2 * Copyright 2019 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 <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"
30 static OSSL_OP_keymgmt_new_fn rsa_newdata
;
31 static OSSL_OP_keymgmt_gen_init_fn rsa_gen_init
;
32 static OSSL_OP_keymgmt_gen_set_params_fn rsa_gen_set_params
;
33 static OSSL_OP_keymgmt_gen_settable_params_fn rsa_gen_settable_params
;
34 static OSSL_OP_keymgmt_gen_fn rsa_gen
;
35 static OSSL_OP_keymgmt_gen_cleanup_fn rsa_gen_cleanup
;
36 static OSSL_OP_keymgmt_free_fn rsa_freedata
;
37 static OSSL_OP_keymgmt_get_params_fn rsa_get_params
;
38 static OSSL_OP_keymgmt_gettable_params_fn rsa_gettable_params
;
39 static OSSL_OP_keymgmt_has_fn rsa_has
;
40 static OSSL_OP_keymgmt_match_fn rsa_match
;
41 static OSSL_OP_keymgmt_validate_fn rsa_validate
;
42 static OSSL_OP_keymgmt_import_fn rsa_import
;
43 static OSSL_OP_keymgmt_import_types_fn rsa_import_types
;
44 static OSSL_OP_keymgmt_export_fn rsa_export
;
45 static OSSL_OP_keymgmt_export_types_fn rsa_export_types
;
47 #define RSA_DEFAULT_MD "SHA256"
48 #define RSA_POSSIBLE_SELECTIONS \
49 (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS)
51 DEFINE_STACK_OF(BIGNUM
)
52 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const
, BIGNUM
)
54 static int export_numbers(OSSL_PARAM_BLD
*tmpl
, const char *key
,
55 STACK_OF(BIGNUM_const
) *numbers
)
62 nnum
= sk_BIGNUM_const_num(numbers
);
64 for (i
= 0; i
< nnum
; i
++) {
65 if (!ossl_param_bld_push_BN(tmpl
, key
,
66 sk_BIGNUM_const_value(numbers
, i
)))
73 static int key_to_params(RSA
*rsa
, OSSL_PARAM_BLD
*tmpl
)
76 const BIGNUM
*rsa_d
= NULL
, *rsa_n
= NULL
, *rsa_e
= NULL
;
77 STACK_OF(BIGNUM_const
) *factors
= sk_BIGNUM_const_new_null();
78 STACK_OF(BIGNUM_const
) *exps
= sk_BIGNUM_const_new_null();
79 STACK_OF(BIGNUM_const
) *coeffs
= sk_BIGNUM_const_new_null();
81 if (rsa
== NULL
|| factors
== NULL
|| exps
== NULL
|| coeffs
== NULL
)
84 RSA_get0_key(rsa
, &rsa_n
, &rsa_e
, &rsa_d
);
85 rsa_get0_all_params(rsa
, factors
, exps
, coeffs
);
88 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_RSA_N
, rsa_n
))
91 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_RSA_E
, rsa_e
))
94 && !ossl_param_bld_push_BN(tmpl
, OSSL_PKEY_PARAM_RSA_D
, rsa_d
))
97 if (!export_numbers(tmpl
, OSSL_PKEY_PARAM_RSA_FACTOR
, factors
)
98 || !export_numbers(tmpl
, OSSL_PKEY_PARAM_RSA_EXPONENT
, exps
)
99 || !export_numbers(tmpl
, OSSL_PKEY_PARAM_RSA_COEFFICIENT
, coeffs
))
104 sk_BIGNUM_const_free(factors
);
105 sk_BIGNUM_const_free(exps
);
106 sk_BIGNUM_const_free(coeffs
);
110 static void *rsa_newdata(void *provctx
)
112 OPENSSL_CTX
*libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
114 return rsa_new_with_ctx(libctx
);
117 static void rsa_freedata(void *keydata
)
122 static int rsa_has(void *keydata
, int selection
)
128 if ((selection
& RSA_POSSIBLE_SELECTIONS
) != 0)
131 if ((selection
& OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS
) != 0)
132 ok
= ok
&& 0; /* This will change with PSS and OAEP */
133 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
134 ok
= ok
&& (RSA_get0_e(rsa
) != NULL
);
135 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
136 ok
= ok
&& (RSA_get0_n(rsa
) != NULL
);
137 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
138 ok
= ok
&& (RSA_get0_d(rsa
) != NULL
);
143 static int rsa_match(const void *keydata1
, const void *keydata2
, int selection
)
145 const RSA
*rsa1
= keydata1
;
146 const RSA
*rsa2
= keydata2
;
149 /* There is always an |e| */
150 ok
= ok
&& BN_cmp(RSA_get0_e(rsa1
), RSA_get0_e(rsa2
)) == 0;
151 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
152 ok
= ok
&& BN_cmp(RSA_get0_n(rsa1
), RSA_get0_n(rsa2
)) == 0;
153 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
154 ok
= ok
&& BN_cmp(RSA_get0_d(rsa1
), RSA_get0_d(rsa2
)) == 0;
158 static int rsa_import(void *keydata
, int selection
, const OSSL_PARAM params
[])
166 /* TODO(3.0) PSS and OAEP should bring on parameters */
168 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
169 ok
= ok
&& rsa_fromdata(rsa
, params
);
174 static int rsa_export(void *keydata
, int selection
,
175 OSSL_CALLBACK
*param_callback
, void *cbarg
)
179 OSSL_PARAM
*params
= NULL
;
185 /* TODO(3.0) PSS and OAEP should bring on parameters */
187 ossl_param_bld_init(&tmpl
);
189 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
190 ok
= ok
&& key_to_params(rsa
, &tmpl
);
193 || (params
= ossl_param_bld_to_param(&tmpl
)) == NULL
)
196 ok
= param_callback(params
, cbarg
);
197 ossl_param_bld_free(params
);
202 * This provider can export everything in an RSA key, so we use the exact
203 * same type description for export as for import. Other providers might
204 * choose to import full keys, but only export the public parts, and will
205 * therefore have the importkey_types and importkey_types functions return
208 static const OSSL_PARAM rsa_key_types
[] = {
209 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N
, NULL
, 0),
210 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E
, NULL
, 0),
211 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D
, NULL
, 0),
212 /* We tolerate up to 10 factors... */
213 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR
, NULL
, 0),
214 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR
, NULL
, 0),
215 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR
, NULL
, 0),
216 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR
, NULL
, 0),
217 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR
, NULL
, 0),
218 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR
, NULL
, 0),
219 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR
, NULL
, 0),
220 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR
, NULL
, 0),
221 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR
, NULL
, 0),
222 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR
, NULL
, 0),
223 /* ..., up to 10 CRT exponents... */
224 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT
, NULL
, 0),
225 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT
, NULL
, 0),
226 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT
, NULL
, 0),
227 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT
, NULL
, 0),
228 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT
, NULL
, 0),
229 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT
, NULL
, 0),
230 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT
, NULL
, 0),
231 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT
, NULL
, 0),
232 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT
, NULL
, 0),
233 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT
, NULL
, 0),
234 /* ..., and up to 9 CRT coefficients */
235 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT
, NULL
, 0),
236 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT
, NULL
, 0),
237 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT
, NULL
, 0),
238 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT
, NULL
, 0),
239 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT
, NULL
, 0),
240 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT
, NULL
, 0),
241 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT
, NULL
, 0),
242 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT
, NULL
, 0),
243 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT
, NULL
, 0),
246 * We lied about the amount of factors, exponents and coefficients, the
247 * export and import functions can really deal with an infinite amount
248 * of these numbers. However, RSA keys with too many primes are futile,
249 * so we at least pretend to have some limits.
252 static const OSSL_PARAM
*rsa_imexport_types(int selection
)
254 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) != 0)
255 return rsa_key_types
;
259 static const OSSL_PARAM
*rsa_import_types(int selection
)
261 return rsa_imexport_types(selection
);
265 static const OSSL_PARAM
*rsa_export_types(int selection
)
267 return rsa_imexport_types(selection
);
270 static int rsa_get_params(void *key
, OSSL_PARAM params
[])
275 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_BITS
)) != NULL
276 && !OSSL_PARAM_set_int(p
, RSA_bits(rsa
)))
278 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_SECURITY_BITS
)) != NULL
279 && !OSSL_PARAM_set_int(p
, RSA_security_bits(rsa
)))
281 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_MAX_SIZE
)) != NULL
282 && !OSSL_PARAM_set_int(p
, RSA_size(rsa
)))
285 # if 0 /* TODO(3.0): PSS support pending */
286 if ((p
= OSSL_PARAM_locate(params
,
287 OSSL_PKEY_PARAM_MANDATORY_DIGEST
)) != NULL
288 && RSA_get0_pss_params(rsa
) != NULL
) {
289 const EVP_MD
*md
, *mgf1md
;
292 if (!rsa_pss_get_param(RSA_get0_pss_params(rsa
),
293 &md
, &mgf1md
, &min_saltlen
)) {
294 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
297 if (!OSSL_PARAM_set_utf8_string(p
, EVP_MD_name(md
)))
301 if ((p
= OSSL_PARAM_locate(params
, OSSL_PKEY_PARAM_DEFAULT_DIGEST
)) != NULL
302 /* TODO(3.0): PSS support pending */
304 && RSA_get0_pss_params(rsa
) == NULL
307 if (!OSSL_PARAM_set_utf8_string(p
, RSA_DEFAULT_MD
))
314 static const OSSL_PARAM rsa_params
[] = {
315 OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS
, NULL
),
316 OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS
, NULL
),
317 OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE
, NULL
),
318 OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST
, NULL
, 0),
322 static const OSSL_PARAM
*rsa_gettable_params(void)
327 static int rsa_validate(void *keydata
, int selection
)
332 if ((selection
& RSA_POSSIBLE_SELECTIONS
) != 0)
335 /* If the whole key is selected, we do a pairwise validation */
336 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
)
337 == OSSL_KEYMGMT_SELECT_KEYPAIR
) {
338 ok
= ok
&& rsa_validate_pairwise(rsa
);
340 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0)
341 ok
= ok
&& rsa_validate_private(rsa
);
342 if ((selection
& OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) != 0)
343 ok
= ok
&& rsa_validate_public(rsa
);
355 /* For generation callback */
360 static int rsa_gencb(int p
, int n
, BN_GENCB
*cb
)
362 struct rsa_gen_ctx
*gctx
= BN_GENCB_get_arg(cb
);
363 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
, OSSL_PARAM_END
};
365 params
[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL
, &p
);
366 params
[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION
, &n
);
368 return gctx
->cb(params
, gctx
->cbarg
);
371 static void *rsa_gen_init(void *provctx
, int selection
)
373 OPENSSL_CTX
*libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
374 struct rsa_gen_ctx
*gctx
= NULL
;
376 if ((selection
& OSSL_KEYMGMT_SELECT_KEYPAIR
) == 0)
379 if ((gctx
= OPENSSL_zalloc(sizeof(*gctx
))) != NULL
) {
380 gctx
->libctx
= libctx
;
381 if ((gctx
->pub_exp
= BN_new()) == NULL
382 || !BN_set_word(gctx
->pub_exp
, RSA_F4
)) {
383 BN_free(gctx
->pub_exp
);
387 gctx
->primes
= RSA_DEFAULT_PRIME_NUM
;
393 static int rsa_gen_set_params(void *genctx
, const OSSL_PARAM params
[])
395 struct rsa_gen_ctx
*gctx
= genctx
;
398 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_RSA_BITS
)) != NULL
399 && !OSSL_PARAM_get_size_t(p
, &gctx
->nbits
))
401 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_RSA_PRIMES
)) != NULL
402 && !OSSL_PARAM_get_size_t(p
, &gctx
->primes
))
404 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_RSA_E
)) != NULL
405 && !OSSL_PARAM_get_BN(p
, &gctx
->pub_exp
))
410 static const OSSL_PARAM
*rsa_gen_settable_params(void *provctx
)
412 static OSSL_PARAM settable
[] = {
413 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_BITS
, NULL
),
414 OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_PRIMES
, NULL
),
415 OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E
, NULL
, 0),
422 static void *rsa_gen(void *genctx
, OSSL_CALLBACK
*osslcb
, void *cbarg
)
424 struct rsa_gen_ctx
*gctx
= genctx
;
426 BN_GENCB
*gencb
= NULL
;
429 || (rsa
= rsa_new_with_ctx(gctx
->libctx
)) == NULL
)
434 gencb
= BN_GENCB_new();
436 BN_GENCB_set(gencb
, rsa_gencb
, genctx
);
438 if (!RSA_generate_multi_prime_key(rsa
, (int)gctx
->nbits
, (int)gctx
->primes
,
439 gctx
->pub_exp
, gencb
)) {
444 BN_GENCB_free(gencb
);
449 static void rsa_gen_cleanup(void *genctx
)
451 struct rsa_gen_ctx
*gctx
= genctx
;
456 BN_clear_free(gctx
->pub_exp
);
460 const OSSL_DISPATCH rsa_keymgmt_functions
[] = {
461 { OSSL_FUNC_KEYMGMT_NEW
, (void (*)(void))rsa_newdata
},
462 { OSSL_FUNC_KEYMGMT_GEN_INIT
, (void (*)(void))rsa_gen_init
},
463 { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS
,
464 (void (*)(void))rsa_gen_set_params
},
465 { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS
,
466 (void (*)(void))rsa_gen_settable_params
},
467 { OSSL_FUNC_KEYMGMT_GEN
, (void (*)(void))rsa_gen
},
468 { OSSL_FUNC_KEYMGMT_GEN_CLEANUP
, (void (*)(void))rsa_gen_cleanup
},
469 { OSSL_FUNC_KEYMGMT_FREE
, (void (*)(void))rsa_freedata
},
470 { OSSL_FUNC_KEYMGMT_GET_PARAMS
, (void (*) (void))rsa_get_params
},
471 { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS
, (void (*) (void))rsa_gettable_params
},
472 { OSSL_FUNC_KEYMGMT_HAS
, (void (*)(void))rsa_has
},
473 { OSSL_FUNC_KEYMGMT_MATCH
, (void (*)(void))rsa_match
},
474 { OSSL_FUNC_KEYMGMT_VALIDATE
, (void (*)(void))rsa_validate
},
475 { OSSL_FUNC_KEYMGMT_IMPORT
, (void (*)(void))rsa_import
},
476 { OSSL_FUNC_KEYMGMT_IMPORT_TYPES
, (void (*)(void))rsa_import_types
},
477 { OSSL_FUNC_KEYMGMT_EXPORT
, (void (*)(void))rsa_export
},
478 { OSSL_FUNC_KEYMGMT_EXPORT_TYPES
, (void (*)(void))rsa_export_types
},