]>
Commit | Line | Data |
---|---|---|
2039c421 RS |
1 | /* |
2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. | |
d02b48c6 | 3 | * |
2039c421 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
ec577822 | 11 | #include <openssl/crypto.h> |
b39fc560 | 12 | #include "internal/cryptlib.h" |
ec577822 | 13 | #include <openssl/lhash.h> |
18125f7f | 14 | #include "internal/bn_int.h" |
721688c2 | 15 | #include <openssl/rand.h> |
3c27208f | 16 | #include <openssl/engine.h> |
9862e9aa | 17 | #include "rsa_locl.h" |
d02b48c6 | 18 | |
0f113f3e | 19 | static const RSA_METHOD *default_RSA_meth = NULL; |
d02b48c6 | 20 | |
6b691a5c | 21 | RSA *RSA_new(void) |
0f113f3e MC |
22 | { |
23 | RSA *r = RSA_new_method(NULL); | |
c554155b | 24 | |
0f113f3e MC |
25 | return r; |
26 | } | |
d02b48c6 | 27 | |
cb78486d | 28 | void RSA_set_default_method(const RSA_METHOD *meth) |
0f113f3e MC |
29 | { |
30 | default_RSA_meth = meth; | |
31 | } | |
d02b48c6 | 32 | |
cb78486d | 33 | const RSA_METHOD *RSA_get_default_method(void) |
0f113f3e MC |
34 | { |
35 | if (default_RSA_meth == NULL) { | |
deb4d50e | 36 | #ifdef RSA_NULL |
0f113f3e | 37 | default_RSA_meth = RSA_null_method(); |
deb4d50e | 38 | #else |
b0700d2c | 39 | default_RSA_meth = RSA_PKCS1_OpenSSL(); |
deb4d50e | 40 | #endif |
0f113f3e | 41 | } |
deb4d50e | 42 | |
0f113f3e MC |
43 | return default_RSA_meth; |
44 | } | |
ce8b2574 | 45 | |
29c1f061 | 46 | const RSA_METHOD *RSA_get_method(const RSA *rsa) |
0f113f3e MC |
47 | { |
48 | return rsa->meth; | |
49 | } | |
cb78486d GT |
50 | |
51 | int RSA_set_method(RSA *rsa, const RSA_METHOD *meth) | |
0f113f3e MC |
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); | |
0b13e9f0 | 61 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd RS |
62 | ENGINE_finish(rsa->engine); |
63 | rsa->engine = NULL; | |
0b13e9f0 | 64 | #endif |
0f113f3e MC |
65 | rsa->meth = meth; |
66 | if (meth->init) | |
67 | meth->init(rsa); | |
68 | return 1; | |
69 | } | |
ce8b2574 | 70 | |
5270e702 | 71 | RSA *RSA_new_method(ENGINE *engine) |
0f113f3e MC |
72 | { |
73 | RSA *ret; | |
d02b48c6 | 74 | |
64b25758 | 75 | ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e MC |
76 | if (ret == NULL) { |
77 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); | |
78 | return NULL; | |
79 | } | |
d02b48c6 | 80 | |
0f113f3e | 81 | ret->meth = RSA_get_default_method(); |
0b13e9f0 | 82 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
83 | if (engine) { |
84 | if (!ENGINE_init(engine)) { | |
85 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); | |
86 | OPENSSL_free(ret); | |
87 | return NULL; | |
88 | } | |
89 | ret->engine = engine; | |
90 | } else | |
91 | ret->engine = ENGINE_get_default_RSA(); | |
92 | if (ret->engine) { | |
93 | ret->meth = ENGINE_get_RSA(ret->engine); | |
7c96dbcd | 94 | if (ret->meth == NULL) { |
0f113f3e MC |
95 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); |
96 | ENGINE_finish(ret->engine); | |
97 | OPENSSL_free(ret); | |
98 | return NULL; | |
99 | } | |
100 | } | |
0b13e9f0 | 101 | #endif |
0c9de428 | 102 | |
0f113f3e | 103 | ret->references = 1; |
0f113f3e MC |
104 | ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; |
105 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) { | |
fdb2c6e4 | 106 | #ifndef OPENSSL_NO_ENGINE |
412bafdc | 107 | ENGINE_finish(ret->engine); |
fdb2c6e4 | 108 | #endif |
0f113f3e | 109 | OPENSSL_free(ret); |
d188a536 | 110 | return NULL; |
0f113f3e | 111 | } |
fdb2c6e4 | 112 | |
d188a536 AG |
113 | ret->lock = CRYPTO_THREAD_lock_new(); |
114 | if (ret->lock == NULL) { | |
0b13e9f0 | 115 | #ifndef OPENSSL_NO_ENGINE |
412bafdc | 116 | ENGINE_finish(ret->engine); |
0b13e9f0 | 117 | #endif |
0f113f3e MC |
118 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data); |
119 | OPENSSL_free(ret); | |
d188a536 AG |
120 | return NULL; |
121 | } | |
122 | ||
123 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { | |
124 | RSA_free(ret); | |
0f113f3e MC |
125 | ret = NULL; |
126 | } | |
d188a536 AG |
127 | |
128 | return ret; | |
0f113f3e | 129 | } |
d02b48c6 | 130 | |
6b691a5c | 131 | void RSA_free(RSA *r) |
0f113f3e MC |
132 | { |
133 | int i; | |
d02b48c6 | 134 | |
0f113f3e MC |
135 | if (r == NULL) |
136 | return; | |
d02b48c6 | 137 | |
d188a536 | 138 | CRYPTO_atomic_add(&r->references, -1, &i, r->lock); |
f3f1cf84 | 139 | REF_PRINT_COUNT("RSA", r); |
0f113f3e MC |
140 | if (i > 0) |
141 | return; | |
f3f1cf84 | 142 | REF_ASSERT_ISNT(i < 0); |
d02b48c6 | 143 | |
0f113f3e MC |
144 | if (r->meth->finish) |
145 | r->meth->finish(r); | |
0b13e9f0 | 146 | #ifndef OPENSSL_NO_ENGINE |
412bafdc | 147 | ENGINE_finish(r->engine); |
0b13e9f0 | 148 | #endif |
d02b48c6 | 149 | |
0f113f3e | 150 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data); |
7abe8305 | 151 | |
d188a536 AG |
152 | CRYPTO_THREAD_lock_free(r->lock); |
153 | ||
23a1d5e9 RS |
154 | BN_clear_free(r->n); |
155 | BN_clear_free(r->e); | |
156 | BN_clear_free(r->d); | |
157 | BN_clear_free(r->p); | |
158 | BN_clear_free(r->q); | |
159 | BN_clear_free(r->dmp1); | |
160 | BN_clear_free(r->dmq1); | |
161 | BN_clear_free(r->iqmp); | |
162 | BN_BLINDING_free(r->blinding); | |
163 | BN_BLINDING_free(r->mt_blinding); | |
4c42ebd2 | 164 | OPENSSL_free(r->bignum_data); |
0f113f3e MC |
165 | OPENSSL_free(r); |
166 | } | |
d02b48c6 | 167 | |
6ac4e8bd | 168 | int RSA_up_ref(RSA *r) |
0f113f3e | 169 | { |
d188a536 AG |
170 | int i; |
171 | ||
172 | if (CRYPTO_atomic_add(&r->references, 1, &i, r->lock) <= 0) | |
173 | return 0; | |
f3f1cf84 RS |
174 | |
175 | REF_PRINT_COUNT("RSA", r); | |
176 | REF_ASSERT_ISNT(i < 2); | |
0f113f3e MC |
177 | return ((i > 1) ? 1 : 0); |
178 | } | |
5cbc2e8b | 179 | |
dd9d233e | 180 | int RSA_set_ex_data(RSA *r, int idx, void *arg) |
0f113f3e MC |
181 | { |
182 | return (CRYPTO_set_ex_data(&r->ex_data, idx, arg)); | |
183 | } | |
58964a49 | 184 | |
29c1f061 | 185 | void *RSA_get_ex_data(const RSA *r, int idx) |
0f113f3e MC |
186 | { |
187 | return (CRYPTO_get_ex_data(&r->ex_data, idx)); | |
188 | } | |
58964a49 | 189 | |
6b691a5c | 190 | int RSA_memory_lock(RSA *r) |
0f113f3e MC |
191 | { |
192 | int i, j, k, off; | |
193 | char *p; | |
194 | BIGNUM *bn, **t[6], *b; | |
195 | BN_ULONG *ul; | |
196 | ||
197 | if (r->d == NULL) | |
198 | return (1); | |
199 | t[0] = &r->d; | |
200 | t[1] = &r->p; | |
201 | t[2] = &r->q; | |
202 | t[3] = &r->dmp1; | |
203 | t[4] = &r->dmq1; | |
204 | t[5] = &r->iqmp; | |
205 | k = bn_sizeof_BIGNUM() * 6; | |
206 | off = k / sizeof(BN_ULONG) + 1; | |
207 | j = 1; | |
208 | for (i = 0; i < 6; i++) | |
209 | j += bn_get_top(*t[i]); | |
b51bce94 | 210 | if ((p = OPENSSL_malloc((off + j) * sizeof(*p))) == NULL) { |
0f113f3e MC |
211 | RSAerr(RSA_F_RSA_MEMORY_LOCK, ERR_R_MALLOC_FAILURE); |
212 | return (0); | |
213 | } | |
16f8d4eb | 214 | memset(p, 0, sizeof(*p) * (off + j)); |
0f113f3e MC |
215 | bn = (BIGNUM *)p; |
216 | ul = (BN_ULONG *)&(p[off]); | |
217 | for (i = 0; i < 6; i++) { | |
218 | b = *(t[i]); | |
219 | *(t[i]) = bn_array_el(bn, i); | |
16f8d4eb RS |
220 | memcpy(bn_array_el(bn, i), b, bn_sizeof_BIGNUM()); |
221 | memcpy(ul, bn_get_words(b), sizeof(*ul) * bn_get_top(b)); | |
0f113f3e MC |
222 | bn_set_static_words(bn_array_el(bn, i), ul, bn_get_top(b)); |
223 | ul += bn_get_top(b); | |
224 | BN_clear_free(b); | |
225 | } | |
dfeab068 | 226 | |
0f113f3e MC |
227 | /* I should fix this so it can still be done */ |
228 | r->flags &= ~(RSA_FLAG_CACHE_PRIVATE | RSA_FLAG_CACHE_PUBLIC); | |
dfeab068 | 229 | |
0f113f3e MC |
230 | r->bignum_data = p; |
231 | return (1); | |
232 | } | |
2514fa79 DSH |
233 | |
234 | int RSA_security_bits(const RSA *rsa) | |
0f113f3e MC |
235 | { |
236 | return BN_security_bits(BN_num_bits(rsa->n), -1); | |
237 | } | |
9862e9aa RL |
238 | |
239 | int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) | |
240 | { | |
1da12e34 RL |
241 | /* If the fields in r are NULL, the corresponding input |
242 | * parameters MUST be non-NULL for n and e. d may be | |
243 | * left NULL (in case only the public key is used). | |
244 | * | |
245 | * It is an error to give the results from get0 on r | |
246 | * as input parameters. | |
247 | */ | |
248 | if (n == r->n || e == r->e | |
249 | || (r->d != NULL && d == r->d)) | |
9862e9aa RL |
250 | return 0; |
251 | ||
1da12e34 RL |
252 | if (n != NULL) { |
253 | BN_free(r->n); | |
254 | r->n = n; | |
255 | } | |
256 | if (e != NULL) { | |
257 | BN_free(r->e); | |
258 | r->e = e; | |
259 | } | |
260 | if (d != NULL) { | |
261 | BN_free(r->d); | |
262 | r->d = d; | |
263 | } | |
9862e9aa RL |
264 | |
265 | return 1; | |
266 | } | |
267 | ||
268 | int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) | |
269 | { | |
1da12e34 RL |
270 | /* If the fields in r are NULL, the corresponding input |
271 | * parameters MUST be non-NULL. | |
272 | * | |
273 | * It is an error to give the results from get0 on r | |
274 | * as input parameters. | |
275 | */ | |
276 | if (p == r->p || q == r->q) | |
9862e9aa RL |
277 | return 0; |
278 | ||
1da12e34 RL |
279 | if (p != NULL) { |
280 | BN_free(r->p); | |
281 | r->p = p; | |
282 | } | |
283 | if (q != NULL) { | |
284 | BN_free(r->q); | |
285 | r->q = q; | |
286 | } | |
9862e9aa RL |
287 | |
288 | return 1; | |
289 | } | |
290 | ||
291 | int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) | |
292 | { | |
1da12e34 RL |
293 | /* If the fields in r are NULL, the corresponding input |
294 | * parameters MUST be non-NULL. | |
295 | * | |
296 | * It is an error to give the results from get0 on r | |
297 | * as input parameters. | |
298 | */ | |
299 | if (dmp1 == r->dmp1 || dmq1 == r->dmq1 || iqmp == r->iqmp) | |
9862e9aa RL |
300 | return 0; |
301 | ||
1da12e34 RL |
302 | if (dmp1 != NULL) { |
303 | BN_free(r->dmp1); | |
304 | r->dmp1 = dmp1; | |
305 | } | |
306 | if (dmq1 != NULL) { | |
307 | BN_free(r->dmq1); | |
308 | r->dmq1 = dmq1; | |
309 | } | |
310 | if (iqmp != NULL) { | |
311 | BN_free(r->iqmp); | |
312 | r->iqmp = iqmp; | |
313 | } | |
9862e9aa RL |
314 | |
315 | return 1; | |
316 | } | |
317 | ||
318 | void RSA_get0_key(const RSA *r, BIGNUM **n, BIGNUM **e, BIGNUM **d) | |
319 | { | |
320 | if (n != NULL) | |
321 | *n = r->n; | |
322 | if (e != NULL) | |
323 | *e = r->e; | |
324 | if (d != NULL) | |
325 | *d = r->d; | |
326 | } | |
327 | ||
328 | void RSA_get0_factors(const RSA *r, BIGNUM **p, BIGNUM **q) | |
329 | { | |
330 | if (p != NULL) | |
331 | *p = r->p; | |
332 | if (q != NULL) | |
333 | *q = r->q; | |
334 | } | |
335 | ||
336 | void RSA_get0_crt_params(const RSA *r, | |
337 | BIGNUM **dmp1, BIGNUM **dmq1, BIGNUM **iqmp) | |
338 | { | |
339 | if (dmp1 != NULL) | |
340 | *dmp1 = r->dmp1; | |
341 | if (dmq1 != NULL) | |
342 | *dmq1 = r->dmq1; | |
343 | if (iqmp != NULL) | |
344 | *iqmp = r->iqmp; | |
345 | } | |
346 | ||
347 | void RSA_clear_flags(RSA *r, int flags) | |
348 | { | |
349 | r->flags &= ~flags; | |
350 | } | |
351 | ||
352 | int RSA_test_flags(const RSA *r, int flags) | |
353 | { | |
354 | return r->flags & flags; | |
355 | } | |
356 | ||
357 | void RSA_set_flags(RSA *r, int flags) | |
358 | { | |
359 | r->flags |= flags; | |
360 | } | |
361 | ||
362 | ENGINE *RSA_get0_engine(RSA *r) | |
363 | { | |
364 | return r->engine; | |
365 | } |