2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 #include <openssl/crypto.h>
12 #include "internal/cryptlib.h"
13 #include "internal/refcount.h"
14 #include "internal/bn_int.h"
15 #include <openssl/engine.h>
16 #include <openssl/evp.h>
17 #include "internal/evp_int.h"
22 return RSA_new_method(NULL);
25 const RSA_METHOD *RSA_get_method(const RSA *rsa)
30 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
33 * NB: The caller is specifically setting a method, so it's not up to us
34 * to deal with which ENGINE it comes from.
36 const RSA_METHOD *mtmp;
40 #ifndef OPENSSL_NO_ENGINE
41 ENGINE_finish(rsa->engine);
50 RSA *RSA_new_method(ENGINE *engine)
52 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
55 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
60 ret->lock = CRYPTO_THREAD_lock_new();
61 if (ret->lock == NULL) {
62 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
67 ret->meth = RSA_get_default_method();
68 #ifndef OPENSSL_NO_ENGINE
69 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
71 if (!ENGINE_init(engine)) {
72 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
77 ret->engine = ENGINE_get_default_RSA();
80 ret->meth = ENGINE_get_RSA(ret->engine);
81 if (ret->meth == NULL) {
82 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
88 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
89 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
93 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
94 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
105 void RSA_free(RSA *r)
112 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
113 REF_PRINT_COUNT("RSA", r);
116 REF_ASSERT_ISNT(i < 0);
118 if (r->meth != NULL && r->meth->finish != NULL)
120 #ifndef OPENSSL_NO_ENGINE
121 ENGINE_finish(r->engine);
124 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
126 CRYPTO_THREAD_lock_free(r->lock);
133 BN_clear_free(r->dmp1);
134 BN_clear_free(r->dmq1);
135 BN_clear_free(r->iqmp);
136 RSA_PSS_PARAMS_free(r->pss);
137 sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
138 BN_BLINDING_free(r->blinding);
139 BN_BLINDING_free(r->mt_blinding);
140 OPENSSL_free(r->bignum_data);
144 int RSA_up_ref(RSA *r)
148 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
151 REF_PRINT_COUNT("RSA", r);
152 REF_ASSERT_ISNT(i < 2);
153 return i > 1 ? 1 : 0;
156 int RSA_set_ex_data(RSA *r, int idx, void *arg)
158 return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
161 void *RSA_get_ex_data(const RSA *r, int idx)
163 return CRYPTO_get_ex_data(&r->ex_data, idx);
166 int RSA_security_bits(const RSA *rsa)
168 int bits = BN_num_bits(rsa->n);
170 if (rsa->version == RSA_ASN1_VERSION_MULTI) {
171 /* This ought to mean that we have private key at hand. */
172 int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
174 if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
177 return BN_security_bits(bits, -1);
180 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
182 /* If the fields n and e in r are NULL, the corresponding input
183 * parameters MUST be non-NULL for n and e. d may be
184 * left NULL (in case only the public key is used).
186 if ((r->n == NULL && n == NULL)
187 || (r->e == NULL && e == NULL))
206 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
208 /* If the fields p and q in r are NULL, the corresponding input
209 * parameters MUST be non-NULL.
211 if ((r->p == NULL && p == NULL)
212 || (r->q == NULL && q == NULL))
227 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
229 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
230 * parameters MUST be non-NULL.
232 if ((r->dmp1 == NULL && dmp1 == NULL)
233 || (r->dmq1 == NULL && dmq1 == NULL)
234 || (r->iqmp == NULL && iqmp == NULL))
238 BN_clear_free(r->dmp1);
242 BN_clear_free(r->dmq1);
246 BN_clear_free(r->iqmp);
254 * Is it better to export RSA_PRIME_INFO structure
255 * and related functions to let user pass a triplet?
257 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
258 BIGNUM *coeffs[], int pnum)
260 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
261 RSA_PRIME_INFO *pinfo;
264 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
267 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
268 if (prime_infos == NULL)
271 if (r->prime_infos != NULL)
272 old = r->prime_infos;
274 for (i = 0; i < pnum; i++) {
275 pinfo = rsa_multip_info_new();
278 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
282 pinfo->r = primes[i];
284 pinfo->t = coeffs[i];
286 rsa_multip_info_free(pinfo);
289 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
292 r->prime_infos = prime_infos;
294 if (!rsa_multip_calc_product(r)) {
295 r->prime_infos = old;
301 * This is hard to deal with, since the old infos could
302 * also be set by this function and r, d, t should not
303 * be freed in that case. So currently, stay consistent
304 * with other *set0* functions: just free it...
306 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
309 r->version = RSA_ASN1_VERSION_MULTI;
313 /* r, d, t should not be freed */
314 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
318 void RSA_get0_key(const RSA *r,
319 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
329 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
337 int RSA_get_multi_prime_extra_count(const RSA *r)
341 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
347 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
350 RSA_PRIME_INFO *pinfo;
352 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
356 * return other primes
357 * it's caller's responsibility to allocate oth_primes[pnum]
359 for (i = 0; i < pnum; i++) {
360 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
361 primes[i] = pinfo->r;
367 void RSA_get0_crt_params(const RSA *r,
368 const BIGNUM **dmp1, const BIGNUM **dmq1,
379 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
380 const BIGNUM *coeffs[])
384 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
387 /* return other primes */
388 if (exps != NULL || coeffs != NULL) {
389 RSA_PRIME_INFO *pinfo;
392 /* it's the user's job to guarantee the buffer length */
393 for (i = 0; i < pnum; i++) {
394 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
398 coeffs[i] = pinfo->t;
405 const BIGNUM *RSA_get0_n(const RSA *r)
410 const BIGNUM *RSA_get0_e(const RSA *r)
415 const BIGNUM *RSA_get0_d(const RSA *r)
420 const BIGNUM *RSA_get0_p(const RSA *r)
425 const BIGNUM *RSA_get0_q(const RSA *r)
430 const BIGNUM *RSA_get0_dmp1(const RSA *r)
435 const BIGNUM *RSA_get0_dmq1(const RSA *r)
440 const BIGNUM *RSA_get0_iqmp(const RSA *r)
445 void RSA_clear_flags(RSA *r, int flags)
450 int RSA_test_flags(const RSA *r, int flags)
452 return r->flags & flags;
455 void RSA_set_flags(RSA *r, int flags)
460 int RSA_get_version(RSA *r)
462 /* { two-prime(0), multi(1) } */
466 ENGINE *RSA_get0_engine(const RSA *r)
471 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
473 /* If key type not RSA or RSA-PSS return error */
474 if (ctx != NULL && ctx->pmeth != NULL
475 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
476 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
478 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);