92d872886b2076ebd1af978f024a2ffe7621ecae
[openssl.git] / crypto / rsa / rsa_lib.c
1 /*
2  * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <stdio.h>
11 #include <openssl/crypto.h>
12 #include "internal/cryptlib.h"
13 #include "e_os.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"
19 #include "rsa_locl.h"
20
21 RSA *RSA_new(void)
22 {
23     return RSA_new_method(NULL);
24 }
25
26 const RSA_METHOD *RSA_get_method(const RSA *rsa)
27 {
28     return rsa->meth;
29 }
30
31 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
32 {
33     /*
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.
36      */
37     const RSA_METHOD *mtmp;
38     mtmp = rsa->meth;
39     if (mtmp->finish)
40         mtmp->finish(rsa);
41 #ifndef OPENSSL_NO_ENGINE
42     ENGINE_finish(rsa->engine);
43     rsa->engine = NULL;
44 #endif
45     rsa->meth = meth;
46     if (meth->init)
47         meth->init(rsa);
48     return 1;
49 }
50
51 RSA *RSA_new_method(ENGINE *engine)
52 {
53     RSA *ret = OPENSSL_zalloc(sizeof(*ret));
54
55     if (ret == NULL) {
56         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
57         return NULL;
58     }
59
60     ret->references = 1;
61     ret->lock = CRYPTO_THREAD_lock_new();
62     if (ret->lock == NULL) {
63         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
64         OPENSSL_free(ret);
65         return NULL;
66     }
67
68     ret->meth = RSA_get_default_method();
69 #ifndef OPENSSL_NO_ENGINE
70     ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
71     if (engine) {
72         if (!ENGINE_init(engine)) {
73             RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
74             goto err;
75         }
76         ret->engine = engine;
77     } else {
78         ret->engine = ENGINE_get_default_RSA();
79     }
80     if (ret->engine) {
81         ret->meth = ENGINE_get_RSA(ret->engine);
82         if (ret->meth == NULL) {
83             RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
84             goto err;
85         }
86     }
87 #endif
88
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)) {
91         goto err;
92     }
93
94     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
95         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
96         goto err;
97     }
98
99     return ret;
100
101 err:
102     RSA_free(ret);
103     return NULL;
104 }
105
106 void RSA_free(RSA *r)
107 {
108     int i;
109
110     if (r == NULL)
111         return;
112
113     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
114     REF_PRINT_COUNT("RSA", r);
115     if (i > 0)
116         return;
117     REF_ASSERT_ISNT(i < 0);
118
119     if (r->meth->finish)
120         r->meth->finish(r);
121 #ifndef OPENSSL_NO_ENGINE
122     ENGINE_finish(r->engine);
123 #endif
124
125     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
126
127     CRYPTO_THREAD_lock_free(r->lock);
128
129     BN_clear_free(r->n);
130     BN_clear_free(r->e);
131     BN_clear_free(r->d);
132     BN_clear_free(r->p);
133     BN_clear_free(r->q);
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);
141     OPENSSL_free(r);
142 }
143
144 int RSA_up_ref(RSA *r)
145 {
146     int i;
147
148     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
149         return 0;
150
151     REF_PRINT_COUNT("RSA", r);
152     REF_ASSERT_ISNT(i < 2);
153     return i > 1 ? 1 : 0;
154 }
155
156 int RSA_set_ex_data(RSA *r, int idx, void *arg)
157 {
158     return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
159 }
160
161 void *RSA_get_ex_data(const RSA *r, int idx)
162 {
163     return CRYPTO_get_ex_data(&r->ex_data, idx);
164 }
165
166 int RSA_security_bits(const RSA *rsa)
167 {
168     return BN_security_bits(BN_num_bits(rsa->n), -1);
169 }
170
171 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
172 {
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).
176      */
177     if ((r->n == NULL && n == NULL)
178         || (r->e == NULL && e == NULL))
179         return 0;
180
181     if (n != NULL) {
182         BN_free(r->n);
183         r->n = n;
184     }
185     if (e != NULL) {
186         BN_free(r->e);
187         r->e = e;
188     }
189     if (d != NULL) {
190         BN_free(r->d);
191         r->d = d;
192     }
193
194     return 1;
195 }
196
197 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
198 {
199     /* If the fields p and q in r are NULL, the corresponding input
200      * parameters MUST be non-NULL.
201      */
202     if ((r->p == NULL && p == NULL)
203         || (r->q == NULL && q == NULL))
204         return 0;
205
206     if (p != NULL) {
207         BN_free(r->p);
208         r->p = p;
209     }
210     if (q != NULL) {
211         BN_free(r->q);
212         r->q = q;
213     }
214
215     return 1;
216 }
217
218 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
219 {
220     /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
221      * parameters MUST be non-NULL.
222      */
223     if ((r->dmp1 == NULL && dmp1 == NULL)
224         || (r->dmq1 == NULL && dmq1 == NULL)
225         || (r->iqmp == NULL && iqmp == NULL))
226         return 0;
227
228     if (dmp1 != NULL) {
229         BN_free(r->dmp1);
230         r->dmp1 = dmp1;
231     }
232     if (dmq1 != NULL) {
233         BN_free(r->dmq1);
234         r->dmq1 = dmq1;
235     }
236     if (iqmp != NULL) {
237         BN_free(r->iqmp);
238         r->iqmp = iqmp;
239     }
240
241     return 1;
242 }
243
244 void RSA_get0_key(const RSA *r,
245                   const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
246 {
247     if (n != NULL)
248         *n = r->n;
249     if (e != NULL)
250         *e = r->e;
251     if (d != NULL)
252         *d = r->d;
253 }
254
255 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
256 {
257     if (p != NULL)
258         *p = r->p;
259     if (q != NULL)
260         *q = r->q;
261 }
262
263 void RSA_get0_crt_params(const RSA *r,
264                          const BIGNUM **dmp1, const BIGNUM **dmq1,
265                          const BIGNUM **iqmp)
266 {
267     if (dmp1 != NULL)
268         *dmp1 = r->dmp1;
269     if (dmq1 != NULL)
270         *dmq1 = r->dmq1;
271     if (iqmp != NULL)
272         *iqmp = r->iqmp;
273 }
274
275 void RSA_clear_flags(RSA *r, int flags)
276 {
277     r->flags &= ~flags;
278 }
279
280 int RSA_test_flags(const RSA *r, int flags)
281 {
282     return r->flags & flags;
283 }
284
285 void RSA_set_flags(RSA *r, int flags)
286 {
287     r->flags |= flags;
288 }
289
290 ENGINE *RSA_get0_engine(const RSA *r)
291 {
292     return r->engine;
293 }
294
295 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
296 {
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)
301         return -1;
302      return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
303 }