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