/src/openssl111/crypto/rsa/rsa_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2019 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 "internal/refcount.h" |
14 | | #include "crypto/bn.h" |
15 | | #include <openssl/engine.h> |
16 | | #include <openssl/evp.h> |
17 | | #include "crypto/evp.h" |
18 | | #include "rsa_local.h" |
19 | | |
20 | | RSA *RSA_new(void) |
21 | 2.38k | { |
22 | 2.38k | return RSA_new_method(NULL); |
23 | 2.38k | } |
24 | | |
25 | | const RSA_METHOD *RSA_get_method(const RSA *rsa) |
26 | 0 | { |
27 | 0 | return rsa->meth; |
28 | 0 | } |
29 | | |
30 | | int RSA_set_method(RSA *rsa, const RSA_METHOD *meth) |
31 | 0 | { |
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 | 0 | const RSA_METHOD *mtmp; |
37 | 0 | mtmp = rsa->meth; |
38 | 0 | if (mtmp->finish) |
39 | 0 | mtmp->finish(rsa); |
40 | 0 | #ifndef OPENSSL_NO_ENGINE |
41 | 0 | ENGINE_finish(rsa->engine); |
42 | 0 | rsa->engine = NULL; |
43 | 0 | #endif |
44 | 0 | rsa->meth = meth; |
45 | 0 | if (meth->init) |
46 | 0 | meth->init(rsa); |
47 | 0 | return 1; |
48 | 0 | } |
49 | | |
50 | | RSA *RSA_new_method(ENGINE *engine) |
51 | 2.38k | { |
52 | 2.38k | RSA *ret = OPENSSL_zalloc(sizeof(*ret)); |
53 | | |
54 | 2.38k | if (ret == NULL) { |
55 | 0 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); |
56 | 0 | return NULL; |
57 | 0 | } |
58 | | |
59 | 2.38k | ret->references = 1; |
60 | 2.38k | ret->lock = CRYPTO_THREAD_lock_new(); |
61 | 2.38k | if (ret->lock == NULL) { |
62 | 0 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); |
63 | 0 | OPENSSL_free(ret); |
64 | 0 | return NULL; |
65 | 0 | } |
66 | | |
67 | 2.38k | ret->meth = RSA_get_default_method(); |
68 | 2.38k | #ifndef OPENSSL_NO_ENGINE |
69 | 2.38k | ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; |
70 | 2.38k | if (engine) { |
71 | 0 | if (!ENGINE_init(engine)) { |
72 | 0 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); |
73 | 0 | goto err; |
74 | 0 | } |
75 | 0 | ret->engine = engine; |
76 | 2.38k | } else { |
77 | 2.38k | ret->engine = ENGINE_get_default_RSA(); |
78 | 2.38k | } |
79 | 2.38k | if (ret->engine) { |
80 | 0 | ret->meth = ENGINE_get_RSA(ret->engine); |
81 | 0 | if (ret->meth == NULL) { |
82 | 0 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); |
83 | 0 | goto err; |
84 | 0 | } |
85 | 0 | } |
86 | 2.38k | #endif |
87 | | |
88 | 2.38k | ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; |
89 | 2.38k | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) { |
90 | 0 | goto err; |
91 | 0 | } |
92 | | |
93 | 2.38k | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { |
94 | 0 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL); |
95 | 0 | goto err; |
96 | 0 | } |
97 | | |
98 | 2.38k | return ret; |
99 | | |
100 | 0 | err: |
101 | 0 | RSA_free(ret); |
102 | 0 | return NULL; |
103 | 2.38k | } |
104 | | |
105 | | void RSA_free(RSA *r) |
106 | 3.45k | { |
107 | 3.45k | int i; |
108 | | |
109 | 3.45k | if (r == NULL) |
110 | 1.07k | return; |
111 | | |
112 | 2.38k | CRYPTO_DOWN_REF(&r->references, &i, r->lock); |
113 | 2.38k | REF_PRINT_COUNT("RSA", r); |
114 | 2.38k | if (i > 0) |
115 | 0 | return; |
116 | 2.38k | REF_ASSERT_ISNT(i < 0); |
117 | | |
118 | 2.38k | if (r->meth != NULL && r->meth->finish != NULL) |
119 | 2.38k | r->meth->finish(r); |
120 | 2.38k | #ifndef OPENSSL_NO_ENGINE |
121 | 2.38k | ENGINE_finish(r->engine); |
122 | 2.38k | #endif |
123 | | |
124 | 2.38k | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data); |
125 | | |
126 | 2.38k | CRYPTO_THREAD_lock_free(r->lock); |
127 | | |
128 | 2.38k | BN_free(r->n); |
129 | 2.38k | BN_free(r->e); |
130 | 2.38k | BN_clear_free(r->d); |
131 | 2.38k | BN_clear_free(r->p); |
132 | 2.38k | BN_clear_free(r->q); |
133 | 2.38k | BN_clear_free(r->dmp1); |
134 | 2.38k | BN_clear_free(r->dmq1); |
135 | 2.38k | BN_clear_free(r->iqmp); |
136 | 2.38k | RSA_PSS_PARAMS_free(r->pss); |
137 | 2.38k | sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free); |
138 | 2.38k | BN_BLINDING_free(r->blinding); |
139 | 2.38k | BN_BLINDING_free(r->mt_blinding); |
140 | 2.38k | OPENSSL_free(r->bignum_data); |
141 | 2.38k | OPENSSL_free(r); |
142 | 2.38k | } |
143 | | |
144 | | int RSA_up_ref(RSA *r) |
145 | 0 | { |
146 | 0 | int i; |
147 | |
|
148 | 0 | if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0) |
149 | 0 | return 0; |
150 | | |
151 | 0 | REF_PRINT_COUNT("RSA", r); |
152 | 0 | REF_ASSERT_ISNT(i < 2); |
153 | 0 | return i > 1 ? 1 : 0; |
154 | 0 | } |
155 | | |
156 | | int RSA_set_ex_data(RSA *r, int idx, void *arg) |
157 | 0 | { |
158 | 0 | return CRYPTO_set_ex_data(&r->ex_data, idx, arg); |
159 | 0 | } |
160 | | |
161 | | void *RSA_get_ex_data(const RSA *r, int idx) |
162 | 0 | { |
163 | 0 | return CRYPTO_get_ex_data(&r->ex_data, idx); |
164 | 0 | } |
165 | | |
166 | | int RSA_security_bits(const RSA *rsa) |
167 | 0 | { |
168 | 0 | int bits = BN_num_bits(rsa->n); |
169 | |
|
170 | 0 | if (rsa->version == RSA_ASN1_VERSION_MULTI) { |
171 | | /* This ought to mean that we have private key at hand. */ |
172 | 0 | int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos); |
173 | |
|
174 | 0 | if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits)) |
175 | 0 | return 0; |
176 | 0 | } |
177 | 0 | return BN_security_bits(bits, -1); |
178 | 0 | } |
179 | | |
180 | | int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) |
181 | 0 | { |
182 | | /* If the fields n and e in r are NULL, the corresponding input |
183 | | * parameters MUST be non-NULL for n and e. d may be |
184 | | * left NULL (in case only the public key is used). |
185 | | */ |
186 | 0 | if ((r->n == NULL && n == NULL) |
187 | 0 | || (r->e == NULL && e == NULL)) |
188 | 0 | return 0; |
189 | | |
190 | 0 | if (n != NULL) { |
191 | 0 | BN_free(r->n); |
192 | 0 | r->n = n; |
193 | 0 | } |
194 | 0 | if (e != NULL) { |
195 | 0 | BN_free(r->e); |
196 | 0 | r->e = e; |
197 | 0 | } |
198 | 0 | if (d != NULL) { |
199 | 0 | BN_clear_free(r->d); |
200 | 0 | r->d = d; |
201 | 0 | BN_set_flags(r->d, BN_FLG_CONSTTIME); |
202 | 0 | } |
203 | |
|
204 | 0 | return 1; |
205 | 0 | } |
206 | | |
207 | | int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) |
208 | 0 | { |
209 | | /* If the fields p and q in r are NULL, the corresponding input |
210 | | * parameters MUST be non-NULL. |
211 | | */ |
212 | 0 | if ((r->p == NULL && p == NULL) |
213 | 0 | || (r->q == NULL && q == NULL)) |
214 | 0 | return 0; |
215 | | |
216 | 0 | if (p != NULL) { |
217 | 0 | BN_clear_free(r->p); |
218 | 0 | r->p = p; |
219 | 0 | BN_set_flags(r->p, BN_FLG_CONSTTIME); |
220 | 0 | } |
221 | 0 | if (q != NULL) { |
222 | 0 | BN_clear_free(r->q); |
223 | 0 | r->q = q; |
224 | 0 | BN_set_flags(r->q, BN_FLG_CONSTTIME); |
225 | 0 | } |
226 | |
|
227 | 0 | return 1; |
228 | 0 | } |
229 | | |
230 | | int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) |
231 | 0 | { |
232 | | /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input |
233 | | * parameters MUST be non-NULL. |
234 | | */ |
235 | 0 | if ((r->dmp1 == NULL && dmp1 == NULL) |
236 | 0 | || (r->dmq1 == NULL && dmq1 == NULL) |
237 | 0 | || (r->iqmp == NULL && iqmp == NULL)) |
238 | 0 | return 0; |
239 | | |
240 | 0 | if (dmp1 != NULL) { |
241 | 0 | BN_clear_free(r->dmp1); |
242 | 0 | r->dmp1 = dmp1; |
243 | 0 | BN_set_flags(r->dmp1, BN_FLG_CONSTTIME); |
244 | 0 | } |
245 | 0 | if (dmq1 != NULL) { |
246 | 0 | BN_clear_free(r->dmq1); |
247 | 0 | r->dmq1 = dmq1; |
248 | 0 | BN_set_flags(r->dmq1, BN_FLG_CONSTTIME); |
249 | 0 | } |
250 | 0 | if (iqmp != NULL) { |
251 | 0 | BN_clear_free(r->iqmp); |
252 | 0 | r->iqmp = iqmp; |
253 | 0 | BN_set_flags(r->iqmp, BN_FLG_CONSTTIME); |
254 | 0 | } |
255 | |
|
256 | 0 | return 1; |
257 | 0 | } |
258 | | |
259 | | /* |
260 | | * Is it better to export RSA_PRIME_INFO structure |
261 | | * and related functions to let user pass a triplet? |
262 | | */ |
263 | | int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[], |
264 | | BIGNUM *coeffs[], int pnum) |
265 | 0 | { |
266 | 0 | STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL; |
267 | 0 | RSA_PRIME_INFO *pinfo; |
268 | 0 | int i; |
269 | |
|
270 | 0 | if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0) |
271 | 0 | return 0; |
272 | | |
273 | 0 | prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum); |
274 | 0 | if (prime_infos == NULL) |
275 | 0 | return 0; |
276 | | |
277 | 0 | if (r->prime_infos != NULL) |
278 | 0 | old = r->prime_infos; |
279 | |
|
280 | 0 | for (i = 0; i < pnum; i++) { |
281 | 0 | pinfo = rsa_multip_info_new(); |
282 | 0 | if (pinfo == NULL) |
283 | 0 | goto err; |
284 | 0 | if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) { |
285 | 0 | BN_clear_free(pinfo->r); |
286 | 0 | BN_clear_free(pinfo->d); |
287 | 0 | BN_clear_free(pinfo->t); |
288 | 0 | pinfo->r = primes[i]; |
289 | 0 | pinfo->d = exps[i]; |
290 | 0 | pinfo->t = coeffs[i]; |
291 | 0 | BN_set_flags(pinfo->r, BN_FLG_CONSTTIME); |
292 | 0 | BN_set_flags(pinfo->d, BN_FLG_CONSTTIME); |
293 | 0 | BN_set_flags(pinfo->t, BN_FLG_CONSTTIME); |
294 | 0 | } else { |
295 | 0 | rsa_multip_info_free(pinfo); |
296 | 0 | goto err; |
297 | 0 | } |
298 | 0 | (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo); |
299 | 0 | } |
300 | | |
301 | 0 | r->prime_infos = prime_infos; |
302 | |
|
303 | 0 | if (!rsa_multip_calc_product(r)) { |
304 | 0 | r->prime_infos = old; |
305 | 0 | goto err; |
306 | 0 | } |
307 | | |
308 | 0 | if (old != NULL) { |
309 | | /* |
310 | | * This is hard to deal with, since the old infos could |
311 | | * also be set by this function and r, d, t should not |
312 | | * be freed in that case. So currently, stay consistent |
313 | | * with other *set0* functions: just free it... |
314 | | */ |
315 | 0 | sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free); |
316 | 0 | } |
317 | |
|
318 | 0 | r->version = RSA_ASN1_VERSION_MULTI; |
319 | |
|
320 | 0 | return 1; |
321 | 0 | err: |
322 | | /* r, d, t should not be freed */ |
323 | 0 | sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex); |
324 | 0 | return 0; |
325 | 0 | } |
326 | | |
327 | | void RSA_get0_key(const RSA *r, |
328 | | const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) |
329 | 0 | { |
330 | 0 | if (n != NULL) |
331 | 0 | *n = r->n; |
332 | 0 | if (e != NULL) |
333 | 0 | *e = r->e; |
334 | 0 | if (d != NULL) |
335 | 0 | *d = r->d; |
336 | 0 | } |
337 | | |
338 | | void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) |
339 | 0 | { |
340 | 0 | if (p != NULL) |
341 | 0 | *p = r->p; |
342 | 0 | if (q != NULL) |
343 | 0 | *q = r->q; |
344 | 0 | } |
345 | | |
346 | | int RSA_get_multi_prime_extra_count(const RSA *r) |
347 | 0 | { |
348 | 0 | int pnum; |
349 | |
|
350 | 0 | pnum = sk_RSA_PRIME_INFO_num(r->prime_infos); |
351 | 0 | if (pnum <= 0) |
352 | 0 | pnum = 0; |
353 | 0 | return pnum; |
354 | 0 | } |
355 | | |
356 | | int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[]) |
357 | 0 | { |
358 | 0 | int pnum, i; |
359 | 0 | RSA_PRIME_INFO *pinfo; |
360 | |
|
361 | 0 | if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0) |
362 | 0 | return 0; |
363 | | |
364 | | /* |
365 | | * return other primes |
366 | | * it's caller's responsibility to allocate oth_primes[pnum] |
367 | | */ |
368 | 0 | for (i = 0; i < pnum; i++) { |
369 | 0 | pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i); |
370 | 0 | primes[i] = pinfo->r; |
371 | 0 | } |
372 | |
|
373 | 0 | return 1; |
374 | 0 | } |
375 | | |
376 | | void RSA_get0_crt_params(const RSA *r, |
377 | | const BIGNUM **dmp1, const BIGNUM **dmq1, |
378 | | const BIGNUM **iqmp) |
379 | 0 | { |
380 | 0 | if (dmp1 != NULL) |
381 | 0 | *dmp1 = r->dmp1; |
382 | 0 | if (dmq1 != NULL) |
383 | 0 | *dmq1 = r->dmq1; |
384 | 0 | if (iqmp != NULL) |
385 | 0 | *iqmp = r->iqmp; |
386 | 0 | } |
387 | | |
388 | | int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[], |
389 | | const BIGNUM *coeffs[]) |
390 | 0 | { |
391 | 0 | int pnum; |
392 | |
|
393 | 0 | if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0) |
394 | 0 | return 0; |
395 | | |
396 | | /* return other primes */ |
397 | 0 | if (exps != NULL || coeffs != NULL) { |
398 | 0 | RSA_PRIME_INFO *pinfo; |
399 | 0 | int i; |
400 | | |
401 | | /* it's the user's job to guarantee the buffer length */ |
402 | 0 | for (i = 0; i < pnum; i++) { |
403 | 0 | pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i); |
404 | 0 | if (exps != NULL) |
405 | 0 | exps[i] = pinfo->d; |
406 | 0 | if (coeffs != NULL) |
407 | 0 | coeffs[i] = pinfo->t; |
408 | 0 | } |
409 | 0 | } |
410 | |
|
411 | 0 | return 1; |
412 | 0 | } |
413 | | |
414 | | const BIGNUM *RSA_get0_n(const RSA *r) |
415 | 0 | { |
416 | 0 | return r->n; |
417 | 0 | } |
418 | | |
419 | | const BIGNUM *RSA_get0_e(const RSA *r) |
420 | 0 | { |
421 | 0 | return r->e; |
422 | 0 | } |
423 | | |
424 | | const BIGNUM *RSA_get0_d(const RSA *r) |
425 | 0 | { |
426 | 0 | return r->d; |
427 | 0 | } |
428 | | |
429 | | const BIGNUM *RSA_get0_p(const RSA *r) |
430 | 0 | { |
431 | 0 | return r->p; |
432 | 0 | } |
433 | | |
434 | | const BIGNUM *RSA_get0_q(const RSA *r) |
435 | 0 | { |
436 | 0 | return r->q; |
437 | 0 | } |
438 | | |
439 | | const BIGNUM *RSA_get0_dmp1(const RSA *r) |
440 | 0 | { |
441 | 0 | return r->dmp1; |
442 | 0 | } |
443 | | |
444 | | const BIGNUM *RSA_get0_dmq1(const RSA *r) |
445 | 0 | { |
446 | 0 | return r->dmq1; |
447 | 0 | } |
448 | | |
449 | | const BIGNUM *RSA_get0_iqmp(const RSA *r) |
450 | 0 | { |
451 | 0 | return r->iqmp; |
452 | 0 | } |
453 | | |
454 | | const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r) |
455 | 0 | { |
456 | 0 | return r->pss; |
457 | 0 | } |
458 | | |
459 | | void RSA_clear_flags(RSA *r, int flags) |
460 | 0 | { |
461 | 0 | r->flags &= ~flags; |
462 | 0 | } |
463 | | |
464 | | int RSA_test_flags(const RSA *r, int flags) |
465 | 0 | { |
466 | 0 | return r->flags & flags; |
467 | 0 | } |
468 | | |
469 | | void RSA_set_flags(RSA *r, int flags) |
470 | 0 | { |
471 | 0 | r->flags |= flags; |
472 | 0 | } |
473 | | |
474 | | int RSA_get_version(RSA *r) |
475 | 0 | { |
476 | | /* { two-prime(0), multi(1) } */ |
477 | 0 | return r->version; |
478 | 0 | } |
479 | | |
480 | | ENGINE *RSA_get0_engine(const RSA *r) |
481 | 0 | { |
482 | 0 | return r->engine; |
483 | 0 | } |
484 | | |
485 | | int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2) |
486 | 0 | { |
487 | | /* If key type not RSA or RSA-PSS return error */ |
488 | 0 | if (ctx != NULL && ctx->pmeth != NULL |
489 | 0 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA |
490 | 0 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS) |
491 | 0 | return -1; |
492 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2); |
493 | 0 | } |