2 * Copyright 1995-2016 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 <openssl/lhash.h>
14 #include "internal/bn_int.h"
15 #include <openssl/engine.h>
16 #include <openssl/evp.h>
17 #include "internal/evp_int.h"
20 static const RSA_METHOD *default_RSA_meth = NULL;
24 RSA *r = RSA_new_method(NULL);
29 void RSA_set_default_method(const RSA_METHOD *meth)
31 default_RSA_meth = meth;
34 const RSA_METHOD *RSA_get_default_method(void)
36 if (default_RSA_meth == NULL) {
38 default_RSA_meth = RSA_null_method();
40 default_RSA_meth = RSA_PKCS1_OpenSSL();
44 return default_RSA_meth;
47 const RSA_METHOD *RSA_get_method(const RSA *rsa)
52 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
55 * NB: The caller is specifically setting a method, so it's not up to us
56 * to deal with which ENGINE it comes from.
58 const RSA_METHOD *mtmp;
62 #ifndef OPENSSL_NO_ENGINE
63 ENGINE_finish(rsa->engine);
72 RSA *RSA_new_method(ENGINE *engine)
74 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
77 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
82 ret->lock = CRYPTO_THREAD_lock_new();
83 if (ret->lock == NULL) {
84 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
89 ret->meth = RSA_get_default_method();
90 #ifndef OPENSSL_NO_ENGINE
91 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
93 if (!ENGINE_init(engine)) {
94 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
99 ret->engine = ENGINE_get_default_RSA();
101 ret->meth = ENGINE_get_RSA(ret->engine);
102 if (ret->meth == NULL) {
103 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
109 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
110 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
114 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
115 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
126 void RSA_free(RSA *r)
133 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
134 REF_PRINT_COUNT("RSA", r);
137 REF_ASSERT_ISNT(i < 0);
141 #ifndef OPENSSL_NO_ENGINE
142 ENGINE_finish(r->engine);
145 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
147 CRYPTO_THREAD_lock_free(r->lock);
154 BN_clear_free(r->dmp1);
155 BN_clear_free(r->dmq1);
156 BN_clear_free(r->iqmp);
157 RSA_PSS_PARAMS_free(r->pss);
158 BN_BLINDING_free(r->blinding);
159 BN_BLINDING_free(r->mt_blinding);
160 OPENSSL_free(r->bignum_data);
164 int RSA_up_ref(RSA *r)
168 if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
171 REF_PRINT_COUNT("RSA", r);
172 REF_ASSERT_ISNT(i < 2);
173 return ((i > 1) ? 1 : 0);
176 int RSA_set_ex_data(RSA *r, int idx, void *arg)
178 return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
181 void *RSA_get_ex_data(const RSA *r, int idx)
183 return (CRYPTO_get_ex_data(&r->ex_data, idx));
186 int RSA_security_bits(const RSA *rsa)
188 return BN_security_bits(BN_num_bits(rsa->n), -1);
191 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
193 /* If the fields n and e in r are NULL, the corresponding input
194 * parameters MUST be non-NULL for n and e. d may be
195 * left NULL (in case only the public key is used).
197 if ((r->n == NULL && n == NULL)
198 || (r->e == NULL && e == NULL))
217 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
219 /* If the fields p and q in r are NULL, the corresponding input
220 * parameters MUST be non-NULL.
222 if ((r->p == NULL && p == NULL)
223 || (r->q == NULL && q == NULL))
238 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
240 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
241 * parameters MUST be non-NULL.
243 if ((r->dmp1 == NULL && dmp1 == NULL)
244 || (r->dmq1 == NULL && dmq1 == NULL)
245 || (r->iqmp == NULL && iqmp == NULL))
264 void RSA_get0_key(const RSA *r,
265 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
275 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
283 void RSA_get0_crt_params(const RSA *r,
284 const BIGNUM **dmp1, const BIGNUM **dmq1,
295 void RSA_clear_flags(RSA *r, int flags)
300 int RSA_test_flags(const RSA *r, int flags)
302 return r->flags & flags;
305 void RSA_set_flags(RSA *r, int flags)
310 ENGINE *RSA_get0_engine(const RSA *r)
315 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
317 /* If key type not RSA or RSA-PSS return error */
318 if (ctx != NULL && ctx->pmeth != NULL
319 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
320 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
322 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);