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"
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);
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 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))
245 * Is it better to export RSA_PRIME_INFO structure
246 * and related functions to let user pass a triplet?
248 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
249 BIGNUM *coeffs[], int pnum)
251 STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
252 RSA_PRIME_INFO *pinfo;
255 if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
258 prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
259 if (prime_infos == NULL)
262 if (r->prime_infos != NULL)
263 old = r->prime_infos;
265 for (i = 0; i < pnum; i++) {
266 pinfo = rsa_multip_info_new();
269 if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
273 pinfo->r = primes[i];
275 pinfo->t = coeffs[i];
277 rsa_multip_info_free(pinfo);
280 (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
283 r->prime_infos = prime_infos;
285 if (!rsa_multip_calc_product(r)) {
286 r->prime_infos = old;
292 * This is hard to deal with, since the old infos could
293 * also be set by this function and r, d, t should not
294 * be freed in that case. So currently, stay consistent
295 * with other *set0* functions: just free it...
297 sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
300 r->version = RSA_ASN1_VERSION_MULTI;
304 /* r, d, t should not be freed */
305 sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
309 void RSA_get0_key(const RSA *r,
310 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
320 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
328 int RSA_get_multi_prime_extra_count(const RSA *r)
332 pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
338 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
341 RSA_PRIME_INFO *pinfo;
343 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
347 * return other primes
348 * it's caller's responsibility to allocate oth_primes[pnum]
350 for (i = 0; i < pnum; i++) {
351 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
352 primes[i] = pinfo->r;
358 void RSA_get0_crt_params(const RSA *r,
359 const BIGNUM **dmp1, const BIGNUM **dmq1,
370 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
371 const BIGNUM *coeffs[])
375 if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
378 /* return other primes */
379 if (exps != NULL || coeffs != NULL) {
380 RSA_PRIME_INFO *pinfo;
383 /* it's the user's job to guarantee the buffer length */
384 for (i = 0; i < pnum; i++) {
385 pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
389 coeffs[i] = pinfo->t;
396 void RSA_clear_flags(RSA *r, int flags)
401 int RSA_test_flags(const RSA *r, int flags)
403 return r->flags & flags;
406 void RSA_set_flags(RSA *r, int flags)
411 int RSA_get_version(RSA *r)
413 /* { two-prime(0), multi(1) } */
417 ENGINE *RSA_get0_engine(const RSA *r)
422 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
424 /* If key type not RSA or RSA-PSS return error */
425 if (ctx != NULL && ctx->pmeth != NULL
426 && ctx->pmeth->pkey_id != EVP_PKEY_RSA
427 && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
429 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);