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