]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rsa/rsa_lib.c
Fix the build and tests following constification of DH, DSA, RSA
[thirdparty/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/rand.h>
16 #include <openssl/engine.h>
17 #include "rsa_locl.h"
18
19 static const RSA_METHOD *default_RSA_meth = NULL;
20
21 RSA *RSA_new(void)
22 {
23 RSA *r = RSA_new_method(NULL);
24
25 return r;
26 }
27
28 void RSA_set_default_method(const RSA_METHOD *meth)
29 {
30 default_RSA_meth = meth;
31 }
32
33 const RSA_METHOD *RSA_get_default_method(void)
34 {
35 if (default_RSA_meth == NULL) {
36 #ifdef RSA_NULL
37 default_RSA_meth = RSA_null_method();
38 #else
39 default_RSA_meth = RSA_PKCS1_OpenSSL();
40 #endif
41 }
42
43 return default_RSA_meth;
44 }
45
46 const RSA_METHOD *RSA_get_method(const RSA *rsa)
47 {
48 return rsa->meth;
49 }
50
51 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
52 {
53 /*
54 * NB: The caller is specifically setting a method, so it's not up to us
55 * to deal with which ENGINE it comes from.
56 */
57 const RSA_METHOD *mtmp;
58 mtmp = rsa->meth;
59 if (mtmp->finish)
60 mtmp->finish(rsa);
61 #ifndef OPENSSL_NO_ENGINE
62 ENGINE_finish(rsa->engine);
63 rsa->engine = NULL;
64 #endif
65 rsa->meth = meth;
66 if (meth->init)
67 meth->init(rsa);
68 return 1;
69 }
70
71 RSA *RSA_new_method(ENGINE *engine)
72 {
73 RSA *ret = OPENSSL_zalloc(sizeof(*ret));
74
75 if (ret == NULL) {
76 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
77 return NULL;
78 }
79
80 ret->references = 1;
81 ret->lock = CRYPTO_THREAD_lock_new();
82 if (ret->lock == NULL) {
83 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
84 OPENSSL_free(ret);
85 return NULL;
86 }
87
88 ret->meth = RSA_get_default_method();
89 #ifndef OPENSSL_NO_ENGINE
90 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
91 if (engine) {
92 if (!ENGINE_init(engine)) {
93 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
94 goto err;
95 }
96 ret->engine = engine;
97 } else
98 ret->engine = ENGINE_get_default_RSA();
99 if (ret->engine) {
100 ret->meth = ENGINE_get_RSA(ret->engine);
101 if (ret->meth == NULL) {
102 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
103 goto err;
104 }
105 }
106 #endif
107
108 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
109 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
110 goto err;
111 }
112
113 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
114 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
115 goto err;
116 }
117
118 return ret;
119
120 err:
121 RSA_free(ret);
122 return NULL;
123 }
124
125 void RSA_free(RSA *r)
126 {
127 int i;
128
129 if (r == NULL)
130 return;
131
132 CRYPTO_atomic_add(&r->references, -1, &i, r->lock);
133 REF_PRINT_COUNT("RSA", r);
134 if (i > 0)
135 return;
136 REF_ASSERT_ISNT(i < 0);
137
138 if (r->meth->finish)
139 r->meth->finish(r);
140 #ifndef OPENSSL_NO_ENGINE
141 ENGINE_finish(r->engine);
142 #endif
143
144 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
145
146 CRYPTO_THREAD_lock_free(r->lock);
147
148 BN_clear_free(r->n);
149 BN_clear_free(r->e);
150 BN_clear_free(r->d);
151 BN_clear_free(r->p);
152 BN_clear_free(r->q);
153 BN_clear_free(r->dmp1);
154 BN_clear_free(r->dmq1);
155 BN_clear_free(r->iqmp);
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_atomic_add(&r->references, 1, &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_memory_lock(RSA *r)
185 {
186 int i, j, k, off;
187 char *p;
188 BIGNUM *bn, **t[6], *b;
189 BN_ULONG *ul;
190
191 if (r->d == NULL)
192 return (1);
193 t[0] = &r->d;
194 t[1] = &r->p;
195 t[2] = &r->q;
196 t[3] = &r->dmp1;
197 t[4] = &r->dmq1;
198 t[5] = &r->iqmp;
199 k = bn_sizeof_BIGNUM() * 6;
200 off = k / sizeof(BN_ULONG) + 1;
201 j = 1;
202 for (i = 0; i < 6; i++)
203 j += bn_get_top(*t[i]);
204 if ((p = OPENSSL_malloc((off + j) * sizeof(*p))) == NULL) {
205 RSAerr(RSA_F_RSA_MEMORY_LOCK, ERR_R_MALLOC_FAILURE);
206 return (0);
207 }
208 memset(p, 0, sizeof(*p) * (off + j));
209 bn = (BIGNUM *)p;
210 ul = (BN_ULONG *)&(p[off]);
211 for (i = 0; i < 6; i++) {
212 b = *(t[i]);
213 *(t[i]) = bn_array_el(bn, i);
214 memcpy(bn_array_el(bn, i), b, bn_sizeof_BIGNUM());
215 memcpy(ul, bn_get_words(b), sizeof(*ul) * bn_get_top(b));
216 bn_set_static_words(bn_array_el(bn, i), ul, bn_get_top(b));
217 ul += bn_get_top(b);
218 BN_clear_free(b);
219 }
220
221 /* I should fix this so it can still be done */
222 r->flags &= ~(RSA_FLAG_CACHE_PRIVATE | RSA_FLAG_CACHE_PUBLIC);
223
224 r->bignum_data = p;
225 return (1);
226 }
227
228 int RSA_security_bits(const RSA *rsa)
229 {
230 return BN_security_bits(BN_num_bits(rsa->n), -1);
231 }
232
233 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
234 {
235 /* If the fields n and e in r are NULL, the corresponding input
236 * parameters MUST be non-NULL for n and e. d may be
237 * left NULL (in case only the public key is used).
238 */
239 if ((r->n == NULL && n == NULL)
240 || (r->e == NULL && e == NULL))
241 return 0;
242
243 if (n != NULL) {
244 BN_free(r->n);
245 r->n = n;
246 }
247 if (e != NULL) {
248 BN_free(r->e);
249 r->e = e;
250 }
251 if (d != NULL) {
252 BN_free(r->d);
253 r->d = d;
254 }
255
256 return 1;
257 }
258
259 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
260 {
261 /* If the fields p and q in r are NULL, the corresponding input
262 * parameters MUST be non-NULL.
263 */
264 if ((r->p == NULL && p == NULL)
265 || (r->q == NULL && q == NULL))
266 return 0;
267
268 if (p != NULL) {
269 BN_free(r->p);
270 r->p = p;
271 }
272 if (q != NULL) {
273 BN_free(r->q);
274 r->q = q;
275 }
276
277 return 1;
278 }
279
280 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
281 {
282 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
283 * parameters MUST be non-NULL.
284 */
285 if ((r->dmp1 == NULL && dmp1 == NULL)
286 || (r->dmq1 == NULL && dmq1 == NULL)
287 || (r->iqmp == NULL && iqmp == NULL))
288 return 0;
289
290 if (dmp1 != NULL) {
291 BN_free(r->dmp1);
292 r->dmp1 = dmp1;
293 }
294 if (dmq1 != NULL) {
295 BN_free(r->dmq1);
296 r->dmq1 = dmq1;
297 }
298 if (iqmp != NULL) {
299 BN_free(r->iqmp);
300 r->iqmp = iqmp;
301 }
302
303 return 1;
304 }
305
306 void RSA_get0_key(const RSA *r,
307 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
308 {
309 if (n != NULL)
310 *n = r->n;
311 if (e != NULL)
312 *e = r->e;
313 if (d != NULL)
314 *d = r->d;
315 }
316
317 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
318 {
319 if (p != NULL)
320 *p = r->p;
321 if (q != NULL)
322 *q = r->q;
323 }
324
325 void RSA_get0_crt_params(const RSA *r,
326 const BIGNUM **dmp1, const BIGNUM **dmq1,
327 const BIGNUM **iqmp)
328 {
329 if (dmp1 != NULL)
330 *dmp1 = r->dmp1;
331 if (dmq1 != NULL)
332 *dmq1 = r->dmq1;
333 if (iqmp != NULL)
334 *iqmp = r->iqmp;
335 }
336
337 void RSA_clear_flags(RSA *r, int flags)
338 {
339 r->flags &= ~flags;
340 }
341
342 int RSA_test_flags(const RSA *r, int flags)
343 {
344 return r->flags & flags;
345 }
346
347 void RSA_set_flags(RSA *r, int flags)
348 {
349 r->flags |= flags;
350 }
351
352 ENGINE *RSA_get0_engine(RSA *r)
353 {
354 return r->engine;
355 }