Add pss field to RSA structure and free it.
[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_DOWN_REF(&r->references, &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     RSA_PSS_PARAMS_free(r->pss);
156     BN_BLINDING_free(r->blinding);
157     BN_BLINDING_free(r->mt_blinding);
158     OPENSSL_free(r->bignum_data);
159     OPENSSL_free(r);
160 }
161
162 int RSA_up_ref(RSA *r)
163 {
164     int i;
165
166     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
167         return 0;
168
169     REF_PRINT_COUNT("RSA", r);
170     REF_ASSERT_ISNT(i < 2);
171     return ((i > 1) ? 1 : 0);
172 }
173
174 int RSA_set_ex_data(RSA *r, int idx, void *arg)
175 {
176     return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
177 }
178
179 void *RSA_get_ex_data(const RSA *r, int idx)
180 {
181     return (CRYPTO_get_ex_data(&r->ex_data, idx));
182 }
183
184 int RSA_security_bits(const RSA *rsa)
185 {
186     return BN_security_bits(BN_num_bits(rsa->n), -1);
187 }
188
189 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
190 {
191     /* If the fields n and e in r are NULL, the corresponding input
192      * parameters MUST be non-NULL for n and e.  d may be
193      * left NULL (in case only the public key is used).
194      */
195     if ((r->n == NULL && n == NULL)
196         || (r->e == NULL && e == NULL))
197         return 0;
198
199     if (n != NULL) {
200         BN_free(r->n);
201         r->n = n;
202     }
203     if (e != NULL) {
204         BN_free(r->e);
205         r->e = e;
206     }
207     if (d != NULL) {
208         BN_free(r->d);
209         r->d = d;
210     }
211
212     return 1;
213 }
214
215 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
216 {
217     /* If the fields p and q in r are NULL, the corresponding input
218      * parameters MUST be non-NULL.
219      */
220     if ((r->p == NULL && p == NULL)
221         || (r->q == NULL && q == NULL))
222         return 0;
223
224     if (p != NULL) {
225         BN_free(r->p);
226         r->p = p;
227     }
228     if (q != NULL) {
229         BN_free(r->q);
230         r->q = q;
231     }
232
233     return 1;
234 }
235
236 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
237 {
238     /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
239      * parameters MUST be non-NULL.
240      */
241     if ((r->dmp1 == NULL && dmp1 == NULL)
242         || (r->dmq1 == NULL && dmq1 == NULL)
243         || (r->iqmp == NULL && iqmp == NULL))
244         return 0;
245
246     if (dmp1 != NULL) {
247         BN_free(r->dmp1);
248         r->dmp1 = dmp1;
249     }
250     if (dmq1 != NULL) {
251         BN_free(r->dmq1);
252         r->dmq1 = dmq1;
253     }
254     if (iqmp != NULL) {
255         BN_free(r->iqmp);
256         r->iqmp = iqmp;
257     }
258
259     return 1;
260 }
261
262 void RSA_get0_key(const RSA *r,
263                   const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
264 {
265     if (n != NULL)
266         *n = r->n;
267     if (e != NULL)
268         *e = r->e;
269     if (d != NULL)
270         *d = r->d;
271 }
272
273 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
274 {
275     if (p != NULL)
276         *p = r->p;
277     if (q != NULL)
278         *q = r->q;
279 }
280
281 void RSA_get0_crt_params(const RSA *r,
282                          const BIGNUM **dmp1, const BIGNUM **dmq1,
283                          const BIGNUM **iqmp)
284 {
285     if (dmp1 != NULL)
286         *dmp1 = r->dmp1;
287     if (dmq1 != NULL)
288         *dmq1 = r->dmq1;
289     if (iqmp != NULL)
290         *iqmp = r->iqmp;
291 }
292
293 void RSA_clear_flags(RSA *r, int flags)
294 {
295     r->flags &= ~flags;
296 }
297
298 int RSA_test_flags(const RSA *r, int flags)
299 {
300     return r->flags & flags;
301 }
302
303 void RSA_set_flags(RSA *r, int flags)
304 {
305     r->flags |= flags;
306 }
307
308 ENGINE *RSA_get0_engine(const RSA *r)
309 {
310     return r->engine;
311 }