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