2 * Copyright 1995-2017 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"
14 #include <openssl/lhash.h>
15 #include "internal/bn_int.h"
16 #include <openssl/engine.h>
17 #include <openssl/evp.h>
18 #include "internal/evp_int.h"
23 return RSA_new_method(NULL);
26 const RSA_METHOD *RSA_get_method(const RSA *rsa)
31 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
34 * NB: The caller is specifically setting a method, so it's not up to us
35 * to deal with which ENGINE it comes from.
37 const RSA_METHOD *mtmp;
41 #ifndef OPENSSL_NO_ENGINE
42 ENGINE_finish(rsa->engine);
51 RSA *RSA_new_method(ENGINE *engine)
53 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
56 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
61 ret->lock = CRYPTO_THREAD_lock_new();
62 if (ret->lock == NULL) {
63 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
68 ret->meth = RSA_get_default_method();
69 #ifndef OPENSSL_NO_ENGINE
70 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
72 if (!ENGINE_init(engine)) {
73 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
78 ret->engine = ENGINE_get_default_RSA();
81 ret->meth = ENGINE_get_RSA(ret->engine);
82 if (ret->meth == NULL) {
83 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
89 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
90 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
94 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
95 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
106 void RSA_free(RSA *r)
113 CRYPTO_DOWN_REF(&r->references, &i, r->lock);
114 REF_PRINT_COUNT("RSA", r);
117 REF_ASSERT_ISNT(i < 0);
121 #ifndef OPENSSL_NO_ENGINE
122 ENGINE_finish(r->engine);
125 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
127 CRYPTO_THREAD_lock_free(r->lock);
134 BN_clear_free(r->dmp1);
135 BN_clear_free(r->dmq1);
136 BN_clear_free(r->iqmp);
137 RSA_PSS_PARAMS_free(r->pss);
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 return BN_security_bits(BN_num_bits(rsa->n), -1);
171 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
173 /* If the fields n and e in r are NULL, the corresponding input
174 * parameters MUST be non-NULL for n and e. d may be
175 * left NULL (in case only the public key is used).
177 if ((r->n == NULL && n == NULL)
178 || (r->e == NULL && e == NULL))
197 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
199 /* If the fields p and q in r are NULL, the corresponding input
200 * parameters MUST be non-NULL.
202 if ((r->p == NULL && p == NULL)
203 || (r->q == NULL && q == NULL))
218 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
220 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
221 * parameters MUST be non-NULL.
223 if ((r->dmp1 == NULL && dmp1 == NULL)
224 || (r->dmq1 == NULL && dmq1 == NULL)
225 || (r->iqmp == NULL && iqmp == NULL))
244 void RSA_get0_key(const RSA *r,
245 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
255 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
263 void RSA_get0_crt_params(const RSA *r,
264 const BIGNUM **dmp1, const BIGNUM **dmq1,
275 void RSA_clear_flags(RSA *r, int flags)
280 int RSA_test_flags(const RSA *r, int flags)
282 return r->flags & flags;
285 void RSA_set_flags(RSA *r, int flags)
290 ENGINE *RSA_get0_engine(const RSA *r)
295 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
297 /* If key type not RSA or RSA-PSS return error */
298 if (ctx != NULL && ctx->pmeth != NULL
299 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
300 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
302 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);