/src/openssl/crypto/rsa/rsa_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2018 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 "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 | 540k | { |
22 | 540k | return RSA_new_method(NULL); |
23 | 540k | } |
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 | 0 | /* |
33 | 0 | * NB: The caller is specifically setting a method, so it's not up to us |
34 | 0 | * to deal with which ENGINE it comes from. |
35 | 0 | */ |
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 | 540k | { |
52 | 540k | RSA *ret = OPENSSL_zalloc(sizeof(*ret)); |
53 | 540k | |
54 | 540k | if (ret == NULL) { |
55 | 0 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); |
56 | 0 | return NULL; |
57 | 0 | } |
58 | 540k | |
59 | 540k | ret->references = 1; |
60 | 540k | ret->lock = CRYPTO_THREAD_lock_new(); |
61 | 540k | 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 | 540k | |
67 | 540k | ret->meth = RSA_get_default_method(); |
68 | 540k | #ifndef OPENSSL_NO_ENGINE |
69 | 540k | ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; |
70 | 540k | 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 | 540k | } else { |
77 | 540k | ret->engine = ENGINE_get_default_RSA(); |
78 | 540k | } |
79 | 540k | 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 | 540k | } |
86 | 540k | #endif |
87 | 540k | |
88 | 540k | ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; |
89 | 540k | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) { |
90 | 0 | goto err; |
91 | 0 | } |
92 | 540k | |
93 | 540k | 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 | 540k | |
98 | 540k | return ret; |
99 | 0 | |
100 | 0 | err: |
101 | 0 | RSA_free(ret); |
102 | 0 | return NULL; |
103 | 540k | } |
104 | | |
105 | | void RSA_free(RSA *r) |
106 | 727k | { |
107 | 727k | int i; |
108 | 727k | |
109 | 727k | if (r == NULL) |
110 | 727k | return; |
111 | 663k | |
112 | 663k | CRYPTO_DOWN_REF(&r->references, &i, r->lock); |
113 | 663k | REF_PRINT_COUNT("RSA", r); |
114 | 663k | if (i > 0) |
115 | 123k | return; |
116 | 540k | REF_ASSERT_ISNT(i < 0); |
117 | 540k | |
118 | 540k | if (r->meth->finish) |
119 | 540k | r->meth->finish(r); |
120 | 540k | #ifndef OPENSSL_NO_ENGINE |
121 | 540k | ENGINE_finish(r->engine); |
122 | 540k | #endif |
123 | 540k | |
124 | 540k | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data); |
125 | 540k | |
126 | 540k | CRYPTO_THREAD_lock_free(r->lock); |
127 | 540k | |
128 | 540k | BN_clear_free(r->n); |
129 | 540k | BN_clear_free(r->e); |
130 | 540k | BN_clear_free(r->d); |
131 | 540k | BN_clear_free(r->p); |
132 | 540k | BN_clear_free(r->q); |
133 | 540k | BN_clear_free(r->dmp1); |
134 | 540k | BN_clear_free(r->dmq1); |
135 | 540k | BN_clear_free(r->iqmp); |
136 | 540k | RSA_PSS_PARAMS_free(r->pss); |
137 | 540k | sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free); |
138 | 540k | BN_BLINDING_free(r->blinding); |
139 | 540k | BN_BLINDING_free(r->mt_blinding); |
140 | 540k | OPENSSL_free(r->bignum_data); |
141 | 540k | OPENSSL_free(r); |
142 | 540k | } |
143 | | |
144 | | int RSA_up_ref(RSA *r) |
145 | 123k | { |
146 | 123k | int i; |
147 | 123k | |
148 | 123k | if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0) |
149 | 0 | return 0; |
150 | 123k | |
151 | 123k | REF_PRINT_COUNT("RSA", r); |
152 | 123k | REF_ASSERT_ISNT(i < 2); |
153 | 123k | return i > 1 ? 1 : 0; |
154 | 123k | } |
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 | 0 |
|
170 | 0 | if (rsa->version == RSA_ASN1_VERSION_MULTI) { |
171 | 0 | /* 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 | 0 |
|
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 | 0 | /* If the fields n and e in r are NULL, the corresponding input |
183 | 0 | * parameters MUST be non-NULL for n and e. d may be |
184 | 0 | * left NULL (in case only the public key is used). |
185 | 0 | */ |
186 | 0 | if ((r->n == NULL && n == NULL) |
187 | 0 | || (r->e == NULL && e == NULL)) |
188 | 0 | return 0; |
189 | 0 | |
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_free(r->d); |
200 | 0 | r->d = d; |
201 | 0 | } |
202 | 0 |
|
203 | 0 | return 1; |
204 | 0 | } |
205 | | |
206 | | int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) |
207 | 0 | { |
208 | 0 | /* If the fields p and q in r are NULL, the corresponding input |
209 | 0 | * parameters MUST be non-NULL. |
210 | 0 | */ |
211 | 0 | if ((r->p == NULL && p == NULL) |
212 | 0 | || (r->q == NULL && q == NULL)) |
213 | 0 | return 0; |
214 | 0 | |
215 | 0 | if (p != NULL) { |
216 | 0 | BN_free(r->p); |
217 | 0 | r->p = p; |
218 | 0 | } |
219 | 0 | if (q != NULL) { |
220 | 0 | BN_free(r->q); |
221 | 0 | r->q = q; |
222 | 0 | } |
223 | 0 |
|
224 | 0 | return 1; |
225 | 0 | } |
226 | | |
227 | | int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) |
228 | 0 | { |
229 | 0 | /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input |
230 | 0 | * parameters MUST be non-NULL. |
231 | 0 | */ |
232 | 0 | if ((r->dmp1 == NULL && dmp1 == NULL) |
233 | 0 | || (r->dmq1 == NULL && dmq1 == NULL) |
234 | 0 | || (r->iqmp == NULL && iqmp == NULL)) |
235 | 0 | return 0; |
236 | 0 | |
237 | 0 | if (dmp1 != NULL) { |
238 | 0 | BN_free(r->dmp1); |
239 | 0 | r->dmp1 = dmp1; |
240 | 0 | } |
241 | 0 | if (dmq1 != NULL) { |
242 | 0 | BN_free(r->dmq1); |
243 | 0 | r->dmq1 = dmq1; |
244 | 0 | } |
245 | 0 | if (iqmp != NULL) { |
246 | 0 | BN_free(r->iqmp); |
247 | 0 | r->iqmp = iqmp; |
248 | 0 | } |
249 | 0 |
|
250 | 0 | return 1; |
251 | 0 | } |
252 | | |
253 | | /* |
254 | | * Is it better to export RSA_PRIME_INFO structure |
255 | | * and related functions to let user pass a triplet? |
256 | | */ |
257 | | int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[], |
258 | | BIGNUM *coeffs[], int pnum) |
259 | 0 | { |
260 | 0 | STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL; |
261 | 0 | RSA_PRIME_INFO *pinfo; |
262 | 0 | int i; |
263 | 0 |
|
264 | 0 | if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0) |
265 | 0 | return 0; |
266 | 0 | |
267 | 0 | prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum); |
268 | 0 | if (prime_infos == NULL) |
269 | 0 | return 0; |
270 | 0 | |
271 | 0 | if (r->prime_infos != NULL) |
272 | 0 | old = r->prime_infos; |
273 | 0 |
|
274 | 0 | for (i = 0; i < pnum; i++) { |
275 | 0 | pinfo = rsa_multip_info_new(); |
276 | 0 | if (pinfo == NULL) |
277 | 0 | goto err; |
278 | 0 | if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) { |
279 | 0 | BN_free(pinfo->r); |
280 | 0 | BN_free(pinfo->d); |
281 | 0 | BN_free(pinfo->t); |
282 | 0 | pinfo->r = primes[i]; |
283 | 0 | pinfo->d = exps[i]; |
284 | 0 | pinfo->t = coeffs[i]; |
285 | 0 | } else { |
286 | 0 | rsa_multip_info_free(pinfo); |
287 | 0 | goto err; |
288 | 0 | } |
289 | 0 | (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo); |
290 | 0 | } |
291 | 0 |
|
292 | 0 | r->prime_infos = prime_infos; |
293 | 0 |
|
294 | 0 | if (!rsa_multip_calc_product(r)) { |
295 | 0 | r->prime_infos = old; |
296 | 0 | goto err; |
297 | 0 | } |
298 | 0 | |
299 | 0 | if (old != NULL) { |
300 | 0 | /* |
301 | 0 | * This is hard to deal with, since the old infos could |
302 | 0 | * also be set by this function and r, d, t should not |
303 | 0 | * be freed in that case. So currently, stay consistent |
304 | 0 | * with other *set0* functions: just free it... |
305 | 0 | */ |
306 | 0 | sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free); |
307 | 0 | } |
308 | 0 |
|
309 | 0 | r->version = RSA_ASN1_VERSION_MULTI; |
310 | 0 |
|
311 | 0 | return 1; |
312 | 0 | err: |
313 | 0 | /* r, d, t should not be freed */ |
314 | 0 | sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex); |
315 | 0 | return 0; |
316 | 0 | } |
317 | | |
318 | | void RSA_get0_key(const RSA *r, |
319 | | const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) |
320 | 58.5k | { |
321 | 58.5k | if (n != NULL) |
322 | 58.5k | *n = r->n; |
323 | 58.5k | if (e != NULL) |
324 | 58.5k | *e = r->e; |
325 | 58.5k | if (d != NULL) |
326 | 58.5k | *d = r->d; |
327 | 58.5k | } |
328 | | |
329 | | void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) |
330 | 34.5k | { |
331 | 34.5k | if (p != NULL) |
332 | 34.5k | *p = r->p; |
333 | 34.5k | if (q != NULL) |
334 | 34.5k | *q = r->q; |
335 | 34.5k | } |
336 | | |
337 | | int RSA_get_multi_prime_extra_count(const RSA *r) |
338 | 0 | { |
339 | 0 | int pnum; |
340 | 0 |
|
341 | 0 | pnum = sk_RSA_PRIME_INFO_num(r->prime_infos); |
342 | 0 | if (pnum <= 0) |
343 | 0 | pnum = 0; |
344 | 0 | return pnum; |
345 | 0 | } |
346 | | |
347 | | int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[]) |
348 | 0 | { |
349 | 0 | int pnum, i; |
350 | 0 | RSA_PRIME_INFO *pinfo; |
351 | 0 |
|
352 | 0 | if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0) |
353 | 0 | return 0; |
354 | 0 | |
355 | 0 | /* |
356 | 0 | * return other primes |
357 | 0 | * it's caller's responsibility to allocate oth_primes[pnum] |
358 | 0 | */ |
359 | 0 | for (i = 0; i < pnum; i++) { |
360 | 0 | pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i); |
361 | 0 | primes[i] = pinfo->r; |
362 | 0 | } |
363 | 0 |
|
364 | 0 | return 1; |
365 | 0 | } |
366 | | |
367 | | void RSA_get0_crt_params(const RSA *r, |
368 | | const BIGNUM **dmp1, const BIGNUM **dmq1, |
369 | | const BIGNUM **iqmp) |
370 | 0 | { |
371 | 0 | if (dmp1 != NULL) |
372 | 0 | *dmp1 = r->dmp1; |
373 | 0 | if (dmq1 != NULL) |
374 | 0 | *dmq1 = r->dmq1; |
375 | 0 | if (iqmp != NULL) |
376 | 0 | *iqmp = r->iqmp; |
377 | 0 | } |
378 | | |
379 | | int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[], |
380 | | const BIGNUM *coeffs[]) |
381 | 0 | { |
382 | 0 | int pnum; |
383 | 0 |
|
384 | 0 | if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0) |
385 | 0 | return 0; |
386 | 0 | |
387 | 0 | /* return other primes */ |
388 | 0 | if (exps != NULL || coeffs != NULL) { |
389 | 0 | RSA_PRIME_INFO *pinfo; |
390 | 0 | int i; |
391 | 0 |
|
392 | 0 | /* it's the user's job to guarantee the buffer length */ |
393 | 0 | for (i = 0; i < pnum; i++) { |
394 | 0 | pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i); |
395 | 0 | if (exps != NULL) |
396 | 0 | exps[i] = pinfo->d; |
397 | 0 | if (coeffs != NULL) |
398 | 0 | coeffs[i] = pinfo->t; |
399 | 0 | } |
400 | 0 | } |
401 | 0 |
|
402 | 0 | return 1; |
403 | 0 | } |
404 | | |
405 | | const BIGNUM *RSA_get0_n(const RSA *r) |
406 | 0 | { |
407 | 0 | return r->n; |
408 | 0 | } |
409 | | |
410 | | const BIGNUM *RSA_get0_e(const RSA *r) |
411 | 0 | { |
412 | 0 | return r->e; |
413 | 0 | } |
414 | | |
415 | | const BIGNUM *RSA_get0_d(const RSA *r) |
416 | 0 | { |
417 | 0 | return r->d; |
418 | 0 | } |
419 | | |
420 | | const BIGNUM *RSA_get0_p(const RSA *r) |
421 | 0 | { |
422 | 0 | return r->p; |
423 | 0 | } |
424 | | |
425 | | const BIGNUM *RSA_get0_q(const RSA *r) |
426 | 0 | { |
427 | 0 | return r->q; |
428 | 0 | } |
429 | | |
430 | | const BIGNUM *RSA_get0_dmp1(const RSA *r) |
431 | 0 | { |
432 | 0 | return r->dmp1; |
433 | 0 | } |
434 | | |
435 | | const BIGNUM *RSA_get0_dmq1(const RSA *r) |
436 | 0 | { |
437 | 0 | return r->dmq1; |
438 | 0 | } |
439 | | |
440 | | const BIGNUM *RSA_get0_iqmp(const RSA *r) |
441 | 0 | { |
442 | 0 | return r->iqmp; |
443 | 0 | } |
444 | | |
445 | | void RSA_clear_flags(RSA *r, int flags) |
446 | 0 | { |
447 | 0 | r->flags &= ~flags; |
448 | 0 | } |
449 | | |
450 | | int RSA_test_flags(const RSA *r, int flags) |
451 | 0 | { |
452 | 0 | return r->flags & flags; |
453 | 0 | } |
454 | | |
455 | | void RSA_set_flags(RSA *r, int flags) |
456 | 0 | { |
457 | 0 | r->flags |= flags; |
458 | 0 | } |
459 | | |
460 | | int RSA_get_version(RSA *r) |
461 | 0 | { |
462 | 0 | /* { two-prime(0), multi(1) } */ |
463 | 0 | return r->version; |
464 | 0 | } |
465 | | |
466 | | ENGINE *RSA_get0_engine(const RSA *r) |
467 | 0 | { |
468 | 0 | return r->engine; |
469 | 0 | } |
470 | | |
471 | | int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2) |
472 | 0 | { |
473 | 0 | /* If key type not RSA or RSA-PSS return error */ |
474 | 0 | if (ctx != NULL && ctx->pmeth != NULL |
475 | 0 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA |
476 | 0 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS) |
477 | 0 | return -1; |
478 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2); |
479 | 0 | } |