48e9100a97c11cc8ab3b8a7e1b41d7132f14c659
[openssl.git] / crypto / rsa / rsa_lib.c
1 /*
2  * Copyright 1995-2016 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 <openssl/lhash.h>
14 #include "internal/bn_int.h"
15 #include <openssl/engine.h>
16 #include "rsa_locl.h"
17
18 static const RSA_METHOD *default_RSA_meth = NULL;
19
20 RSA *RSA_new(void)
21 {
22     RSA *r = RSA_new_method(NULL);
23
24     return r;
25 }
26
27 void RSA_set_default_method(const RSA_METHOD *meth)
28 {
29     default_RSA_meth = meth;
30 }
31
32 const RSA_METHOD *RSA_get_default_method(void)
33 {
34     if (default_RSA_meth == NULL) {
35 #ifdef RSA_NULL
36         default_RSA_meth = RSA_null_method();
37 #else
38         default_RSA_meth = RSA_PKCS1_OpenSSL();
39 #endif
40     }
41
42     return default_RSA_meth;
43 }
44
45 const RSA_METHOD *RSA_get_method(const RSA *rsa)
46 {
47     return rsa->meth;
48 }
49
50 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
51 {
52     /*
53      * NB: The caller is specifically setting a method, so it's not up to us
54      * to deal with which ENGINE it comes from.
55      */
56     const RSA_METHOD *mtmp;
57     mtmp = rsa->meth;
58     if (mtmp->finish)
59         mtmp->finish(rsa);
60 #ifndef OPENSSL_NO_ENGINE
61     ENGINE_finish(rsa->engine);
62     rsa->engine = NULL;
63 #endif
64     rsa->meth = meth;
65     if (meth->init)
66         meth->init(rsa);
67     return 1;
68 }
69
70 RSA *RSA_new_method(ENGINE *engine)
71 {
72     RSA *ret = OPENSSL_zalloc(sizeof(*ret));
73
74     if (ret == NULL) {
75         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
76         return NULL;
77     }
78
79     ret->references = 1;
80     ret->lock = CRYPTO_THREAD_lock_new();
81     if (ret->lock == NULL) {
82         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
83         OPENSSL_free(ret);
84         return NULL;
85     }
86
87     ret->meth = RSA_get_default_method();
88 #ifndef OPENSSL_NO_ENGINE
89     ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
90     if (engine) {
91         if (!ENGINE_init(engine)) {
92             RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
93             goto err;
94         }
95         ret->engine = engine;
96     } else
97         ret->engine = ENGINE_get_default_RSA();
98     if (ret->engine) {
99         ret->meth = ENGINE_get_RSA(ret->engine);
100         if (ret->meth == NULL) {
101             RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
102             goto err;
103         }
104     }
105 #endif
106
107     ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
108     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
109         goto err;
110     }
111
112     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
113         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
114         goto err;
115     }
116
117     return ret;
118
119 err:
120     RSA_free(ret);
121     return NULL;
122 }
123
124 void RSA_free(RSA *r)
125 {
126     int i;
127
128     if (r == NULL)
129         return;
130
131     CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
132     REF_PRINT_COUNT("RSA", r);
133     if (i > 0)
134         return;
135     REF_ASSERT_ISNT(i < 0);
136
137     if (r->meth->finish)
138         r->meth->finish(r);
139 #ifndef OPENSSL_NO_ENGINE
140     ENGINE_finish(r->engine);
141 #endif
142
143     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
144
145     CRYPTO_THREAD_lock_free(r->lock);
146
147     BN_clear_free(r->n);
148     BN_clear_free(r->e);
149     BN_clear_free(r->d);
150     BN_clear_free(r->p);
151     BN_clear_free(r->q);
152     BN_clear_free(r->dmp1);
153     BN_clear_free(r->dmq1);
154     BN_clear_free(r->iqmp);
155     BN_BLINDING_free(r->blinding);
156     BN_BLINDING_free(r->mt_blinding);
157     OPENSSL_free(r->bignum_data);
158     OPENSSL_free(r);
159 }
160
161 int RSA_up_ref(RSA *r)
162 {
163     int i;
164
165     if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0)
166         return 0;
167
168     REF_PRINT_COUNT("RSA", r);
169     REF_ASSERT_ISNT(i < 2);
170     return ((i > 1) ? 1 : 0);
171 }
172
173 int RSA_set_ex_data(RSA *r, int idx, void *arg)
174 {
175     return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
176 }
177
178 void *RSA_get_ex_data(const RSA *r, int idx)
179 {
180     return (CRYPTO_get_ex_data(&r->ex_data, idx));
181 }
182
183 int RSA_security_bits(const RSA *rsa)
184 {
185     return BN_security_bits(BN_num_bits(rsa->n), -1);
186 }
187
188 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
189 {
190     /* If the fields n and e in r are NULL, the corresponding input
191      * parameters MUST be non-NULL for n and e.  d may be
192      * left NULL (in case only the public key is used).
193      */
194     if ((r->n == NULL && n == NULL)
195         || (r->e == NULL && e == NULL))
196         return 0;
197
198     if (n != NULL) {
199         BN_free(r->n);
200         r->n = n;
201     }
202     if (e != NULL) {
203         BN_free(r->e);
204         r->e = e;
205     }
206     if (d != NULL) {
207         BN_free(r->d);
208         r->d = d;
209     }
210
211     return 1;
212 }
213
214 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
215 {
216     /* If the fields p and q in r are NULL, the corresponding input
217      * parameters MUST be non-NULL.
218      */
219     if ((r->p == NULL && p == NULL)
220         || (r->q == NULL && q == NULL))
221         return 0;
222
223     if (p != NULL) {
224         BN_free(r->p);
225         r->p = p;
226     }
227     if (q != NULL) {
228         BN_free(r->q);
229         r->q = q;
230     }
231
232     return 1;
233 }
234
235 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
236 {
237     /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
238      * parameters MUST be non-NULL.
239      */
240     if ((r->dmp1 == NULL && dmp1 == NULL)
241         || (r->dmq1 == NULL && dmq1 == NULL)
242         || (r->iqmp == NULL && iqmp == NULL))
243         return 0;
244
245     if (dmp1 != NULL) {
246         BN_free(r->dmp1);
247         r->dmp1 = dmp1;
248     }
249     if (dmq1 != NULL) {
250         BN_free(r->dmq1);
251         r->dmq1 = dmq1;
252     }
253     if (iqmp != NULL) {
254         BN_free(r->iqmp);
255         r->iqmp = iqmp;
256     }
257
258     return 1;
259 }
260
261 void RSA_get0_key(const RSA *r,
262                   const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
263 {
264     if (n != NULL)
265         *n = r->n;
266     if (e != NULL)
267         *e = r->e;
268     if (d != NULL)
269         *d = r->d;
270 }
271
272 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
273 {
274     if (p != NULL)
275         *p = r->p;
276     if (q != NULL)
277         *q = r->q;
278 }
279
280 void RSA_get0_crt_params(const RSA *r,
281                          const BIGNUM **dmp1, const BIGNUM **dmq1,
282                          const BIGNUM **iqmp)
283 {
284     if (dmp1 != NULL)
285         *dmp1 = r->dmp1;
286     if (dmq1 != NULL)
287         *dmq1 = r->dmq1;
288     if (iqmp != NULL)
289         *iqmp = r->iqmp;
290 }
291
292 void RSA_clear_flags(RSA *r, int flags)
293 {
294     r->flags &= ~flags;
295 }
296
297 int RSA_test_flags(const RSA *r, int flags)
298 {
299     return r->flags & flags;
300 }
301
302 void RSA_set_flags(RSA *r, int flags)
303 {
304     r->flags |= flags;
305 }
306
307 ENGINE *RSA_get0_engine(const RSA *r)
308 {
309     return r->engine;
310 }