]>
Commit | Line | Data |
---|---|---|
2039c421 | 1 | /* |
83cf7abf | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
2a7b6f39 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
2039c421 RS |
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> |
89abd1b6 MC |
12 | #include <openssl/core_names.h> |
13 | #include <openssl/engine.h> | |
14 | #include <openssl/evp.h> | |
b39fc560 | 15 | #include "internal/cryptlib.h" |
cd420b0b | 16 | #include "internal/refcount.h" |
25f2138b | 17 | #include "crypto/bn.h" |
25f2138b | 18 | #include "crypto/evp.h" |
c3a4fa4c | 19 | #include "crypto/rsa.h" |
706457b7 | 20 | #include "rsa_local.h" |
d02b48c6 | 21 | |
6b691a5c | 22 | RSA *RSA_new(void) |
0f113f3e | 23 | { |
076fc555 | 24 | return RSA_new_method(NULL); |
0f113f3e | 25 | } |
ce8b2574 | 26 | |
29c1f061 | 27 | const RSA_METHOD *RSA_get_method(const RSA *rsa) |
0f113f3e MC |
28 | { |
29 | return rsa->meth; | |
30 | } | |
cb78486d GT |
31 | |
32 | int RSA_set_method(RSA *rsa, const RSA_METHOD *meth) | |
0f113f3e MC |
33 | { |
34 | /* | |
35 | * NB: The caller is specifically setting a method, so it's not up to us | |
36 | * to deal with which ENGINE it comes from. | |
37 | */ | |
38 | const RSA_METHOD *mtmp; | |
39 | mtmp = rsa->meth; | |
40 | if (mtmp->finish) | |
41 | mtmp->finish(rsa); | |
0b13e9f0 | 42 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd RS |
43 | ENGINE_finish(rsa->engine); |
44 | rsa->engine = NULL; | |
0b13e9f0 | 45 | #endif |
0f113f3e MC |
46 | rsa->meth = meth; |
47 | if (meth->init) | |
48 | meth->init(rsa); | |
49 | return 1; | |
50 | } | |
ce8b2574 | 51 | |
5270e702 | 52 | RSA *RSA_new_method(ENGINE *engine) |
0f113f3e | 53 | { |
11ed851d | 54 | RSA *ret = OPENSSL_zalloc(sizeof(*ret)); |
d02b48c6 | 55 | |
0f113f3e MC |
56 | if (ret == NULL) { |
57 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); | |
58 | return NULL; | |
59 | } | |
d02b48c6 | 60 | |
11ed851d F |
61 | ret->references = 1; |
62 | ret->lock = CRYPTO_THREAD_lock_new(); | |
63 | if (ret->lock == NULL) { | |
64 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE); | |
65 | OPENSSL_free(ret); | |
66 | return NULL; | |
67 | } | |
68 | ||
0f113f3e | 69 | ret->meth = RSA_get_default_method(); |
0b13e9f0 | 70 | #ifndef OPENSSL_NO_ENGINE |
11ed851d | 71 | ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; |
0f113f3e MC |
72 | if (engine) { |
73 | if (!ENGINE_init(engine)) { | |
74 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); | |
11ed851d | 75 | goto err; |
0f113f3e MC |
76 | } |
77 | ret->engine = engine; | |
90862ab4 | 78 | } else { |
0f113f3e | 79 | ret->engine = ENGINE_get_default_RSA(); |
90862ab4 | 80 | } |
0f113f3e MC |
81 | if (ret->engine) { |
82 | ret->meth = ENGINE_get_RSA(ret->engine); | |
7c96dbcd | 83 | if (ret->meth == NULL) { |
0f113f3e | 84 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB); |
11ed851d | 85 | goto err; |
0f113f3e MC |
86 | } |
87 | } | |
0b13e9f0 | 88 | #endif |
0c9de428 | 89 | |
0f113f3e | 90 | ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; |
a3327784 | 91 | #ifndef FIPS_MODE |
0f113f3e | 92 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) { |
11ed851d | 93 | goto err; |
d188a536 | 94 | } |
a3327784 | 95 | #endif |
d188a536 AG |
96 | |
97 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { | |
11ed851d F |
98 | RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL); |
99 | goto err; | |
0f113f3e | 100 | } |
d188a536 AG |
101 | |
102 | return ret; | |
11ed851d | 103 | |
544648a8 | 104 | err: |
11ed851d F |
105 | RSA_free(ret); |
106 | return NULL; | |
0f113f3e | 107 | } |
d02b48c6 | 108 | |
6b691a5c | 109 | void RSA_free(RSA *r) |
0f113f3e MC |
110 | { |
111 | int i; | |
d02b48c6 | 112 | |
0f113f3e MC |
113 | if (r == NULL) |
114 | return; | |
d02b48c6 | 115 | |
2f545ae4 | 116 | CRYPTO_DOWN_REF(&r->references, &i, r->lock); |
f3f1cf84 | 117 | REF_PRINT_COUNT("RSA", r); |
0f113f3e MC |
118 | if (i > 0) |
119 | return; | |
f3f1cf84 | 120 | REF_ASSERT_ISNT(i < 0); |
d02b48c6 | 121 | |
0c5d725e | 122 | if (r->meth != NULL && r->meth->finish != NULL) |
0f113f3e | 123 | r->meth->finish(r); |
0b13e9f0 | 124 | #ifndef OPENSSL_NO_ENGINE |
412bafdc | 125 | ENGINE_finish(r->engine); |
0b13e9f0 | 126 | #endif |
d02b48c6 | 127 | |
a3327784 | 128 | #ifndef FIPS_MODE |
0f113f3e | 129 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data); |
a3327784 | 130 | #endif |
7abe8305 | 131 | |
d188a536 AG |
132 | CRYPTO_THREAD_lock_free(r->lock); |
133 | ||
c033101d MB |
134 | BN_free(r->n); |
135 | BN_free(r->e); | |
23a1d5e9 RS |
136 | BN_clear_free(r->d); |
137 | BN_clear_free(r->p); | |
138 | BN_clear_free(r->q); | |
139 | BN_clear_free(r->dmp1); | |
140 | BN_clear_free(r->dmq1); | |
141 | BN_clear_free(r->iqmp); | |
d771441d | 142 | RSA_PSS_PARAMS_free(r->pss); |
665d899f | 143 | sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free); |
23a1d5e9 RS |
144 | BN_BLINDING_free(r->blinding); |
145 | BN_BLINDING_free(r->mt_blinding); | |
4c42ebd2 | 146 | OPENSSL_free(r->bignum_data); |
0f113f3e MC |
147 | OPENSSL_free(r); |
148 | } | |
d02b48c6 | 149 | |
6ac4e8bd | 150 | int RSA_up_ref(RSA *r) |
0f113f3e | 151 | { |
d188a536 AG |
152 | int i; |
153 | ||
2f545ae4 | 154 | if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0) |
d188a536 | 155 | return 0; |
f3f1cf84 RS |
156 | |
157 | REF_PRINT_COUNT("RSA", r); | |
158 | REF_ASSERT_ISNT(i < 2); | |
8686c474 | 159 | return i > 1 ? 1 : 0; |
0f113f3e | 160 | } |
5cbc2e8b | 161 | |
a3327784 | 162 | #ifndef FIPS_MODE |
dd9d233e | 163 | int RSA_set_ex_data(RSA *r, int idx, void *arg) |
0f113f3e | 164 | { |
8686c474 | 165 | return CRYPTO_set_ex_data(&r->ex_data, idx, arg); |
0f113f3e | 166 | } |
58964a49 | 167 | |
29c1f061 | 168 | void *RSA_get_ex_data(const RSA *r, int idx) |
0f113f3e | 169 | { |
8686c474 | 170 | return CRYPTO_get_ex_data(&r->ex_data, idx); |
0f113f3e | 171 | } |
a3327784 | 172 | #endif |
58964a49 | 173 | |
97b0b713 P |
174 | /* |
175 | * Define a scaling constant for our fixed point arithmetic. | |
176 | * This value must be a power of two because the base two logarithm code | |
177 | * makes this assumption. The exponent must also be a multiple of three so | |
178 | * that the scale factor has an exact cube root. Finally, the scale factor | |
179 | * should not be so large that a multiplication of two scaled numbers | |
180 | * overflows a 64 bit unsigned integer. | |
181 | */ | |
182 | static const unsigned int scale = 1 << 18; | |
183 | static const unsigned int cbrt_scale = 1 << (2 * 18 / 3); | |
184 | ||
185 | /* Define some constants, none exceed 32 bits */ | |
186 | static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */ | |
187 | static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */ | |
188 | static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */ | |
189 | static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */ | |
190 | ||
191 | /* | |
2beb004b | 192 | * Multiply two scaled integers together and rescale the result. |
97b0b713 P |
193 | */ |
194 | static ossl_inline uint64_t mul2(uint64_t a, uint64_t b) | |
195 | { | |
196 | return a * b / scale; | |
197 | } | |
198 | ||
199 | /* | |
200 | * Calculate the cube root of a 64 bit scaled integer. | |
201 | * Although the cube root of a 64 bit number does fit into a 32 bit unsigned | |
202 | * integer, this is not guaranteed after scaling, so this function has a | |
203 | * 64 bit return. This uses the shifting nth root algorithm with some | |
204 | * algebraic simplifications. | |
205 | */ | |
206 | static uint64_t icbrt64(uint64_t x) | |
207 | { | |
208 | uint64_t r = 0; | |
209 | uint64_t b; | |
210 | int s; | |
211 | ||
212 | for (s = 63; s >= 0; s -= 3) { | |
213 | r <<= 1; | |
214 | b = 3 * r * (r + 1) + 1; | |
215 | if ((x >> s) >= b) { | |
216 | x -= b << s; | |
217 | r++; | |
218 | } | |
219 | } | |
220 | return r * cbrt_scale; | |
221 | } | |
222 | ||
223 | /* | |
224 | * Calculate the natural logarithm of a 64 bit scaled integer. | |
225 | * This is done by calculating a base two logarithm and scaling. | |
226 | * The maximum logarithm (base 2) is 64 and this reduces base e, so | |
227 | * a 32 bit result should not overflow. The argument passed must be | |
228 | * greater than unity so we don't need to handle negative results. | |
229 | */ | |
230 | static uint32_t ilog_e(uint64_t v) | |
231 | { | |
232 | uint32_t i, r = 0; | |
233 | ||
234 | /* | |
235 | * Scale down the value into the range 1 .. 2. | |
236 | * | |
237 | * If fractional numbers need to be processed, another loop needs | |
238 | * to go here that checks v < scale and if so multiplies it by 2 and | |
239 | * reduces r by scale. This also means making r signed. | |
240 | */ | |
241 | while (v >= 2 * scale) { | |
242 | v >>= 1; | |
243 | r += scale; | |
244 | } | |
245 | for (i = scale / 2; i != 0; i /= 2) { | |
246 | v = mul2(v, v); | |
247 | if (v >= 2 * scale) { | |
248 | v >>= 1; | |
249 | r += i; | |
250 | } | |
251 | } | |
252 | r = (r * (uint64_t)scale) / log_e; | |
253 | return r; | |
254 | } | |
255 | ||
256 | /* | |
257 | * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC | |
258 | * Modulus Lengths. | |
259 | * | |
260 | * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)} | |
261 | * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)} | |
262 | * The two cube roots are merged together here. | |
263 | */ | |
8240d5fa | 264 | uint16_t rsa_compute_security_bits(int n) |
97b0b713 P |
265 | { |
266 | uint64_t x; | |
267 | uint32_t lx; | |
268 | uint16_t y; | |
269 | ||
270 | /* Look for common values as listed in SP 800-56B rev 2 Appendix D */ | |
271 | switch (n) { | |
272 | case 2048: | |
273 | return 112; | |
274 | case 3072: | |
275 | return 128; | |
276 | case 4096: | |
277 | return 152; | |
278 | case 6144: | |
279 | return 176; | |
280 | case 8192: | |
281 | return 200; | |
282 | } | |
283 | /* | |
284 | * The first incorrect result (i.e. not accurate or off by one low) occurs | |
285 | * for n = 699668. The true value here is 1200. Instead of using this n | |
286 | * as the check threshold, the smallest n such that the correct result is | |
287 | * 1200 is used instead. | |
288 | */ | |
289 | if (n >= 687737) | |
290 | return 1200; | |
291 | if (n < 8) | |
292 | return 0; | |
293 | ||
294 | x = n * (uint64_t)log_2; | |
295 | lx = ilog_e(x); | |
296 | y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690) | |
297 | / log_2); | |
298 | return (y + 4) & ~7; | |
299 | } | |
300 | ||
2514fa79 | 301 | int RSA_security_bits(const RSA *rsa) |
0f113f3e | 302 | { |
0122add6 AP |
303 | int bits = BN_num_bits(rsa->n); |
304 | ||
305 | if (rsa->version == RSA_ASN1_VERSION_MULTI) { | |
306 | /* This ought to mean that we have private key at hand. */ | |
307 | int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos); | |
308 | ||
309 | if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits)) | |
310 | return 0; | |
311 | } | |
97b0b713 | 312 | return rsa_compute_security_bits(bits); |
0f113f3e | 313 | } |
9862e9aa RL |
314 | |
315 | int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) | |
316 | { | |
fd809cfd | 317 | /* If the fields n and e in r are NULL, the corresponding input |
1da12e34 RL |
318 | * parameters MUST be non-NULL for n and e. d may be |
319 | * left NULL (in case only the public key is used). | |
1da12e34 | 320 | */ |
b84e1226 MC |
321 | if ((r->n == NULL && n == NULL) |
322 | || (r->e == NULL && e == NULL)) | |
9862e9aa RL |
323 | return 0; |
324 | ||
1da12e34 RL |
325 | if (n != NULL) { |
326 | BN_free(r->n); | |
327 | r->n = n; | |
328 | } | |
329 | if (e != NULL) { | |
330 | BN_free(r->e); | |
331 | r->e = e; | |
332 | } | |
333 | if (d != NULL) { | |
c033101d | 334 | BN_clear_free(r->d); |
1da12e34 | 335 | r->d = d; |
311e903d | 336 | BN_set_flags(r->d, BN_FLG_CONSTTIME); |
1da12e34 | 337 | } |
29be6023 | 338 | r->dirty_cnt++; |
9862e9aa RL |
339 | |
340 | return 1; | |
341 | } | |
342 | ||
343 | int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) | |
344 | { | |
fd809cfd | 345 | /* If the fields p and q in r are NULL, the corresponding input |
1da12e34 | 346 | * parameters MUST be non-NULL. |
1da12e34 | 347 | */ |
b84e1226 MC |
348 | if ((r->p == NULL && p == NULL) |
349 | || (r->q == NULL && q == NULL)) | |
9862e9aa RL |
350 | return 0; |
351 | ||
1da12e34 | 352 | if (p != NULL) { |
c033101d | 353 | BN_clear_free(r->p); |
1da12e34 | 354 | r->p = p; |
311e903d | 355 | BN_set_flags(r->p, BN_FLG_CONSTTIME); |
1da12e34 RL |
356 | } |
357 | if (q != NULL) { | |
c033101d | 358 | BN_clear_free(r->q); |
1da12e34 | 359 | r->q = q; |
311e903d | 360 | BN_set_flags(r->q, BN_FLG_CONSTTIME); |
1da12e34 | 361 | } |
29be6023 | 362 | r->dirty_cnt++; |
9862e9aa RL |
363 | |
364 | return 1; | |
365 | } | |
366 | ||
367 | int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) | |
368 | { | |
fd809cfd | 369 | /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input |
1da12e34 | 370 | * parameters MUST be non-NULL. |
1da12e34 | 371 | */ |
b84e1226 MC |
372 | if ((r->dmp1 == NULL && dmp1 == NULL) |
373 | || (r->dmq1 == NULL && dmq1 == NULL) | |
374 | || (r->iqmp == NULL && iqmp == NULL)) | |
9862e9aa RL |
375 | return 0; |
376 | ||
1da12e34 | 377 | if (dmp1 != NULL) { |
c033101d | 378 | BN_clear_free(r->dmp1); |
1da12e34 | 379 | r->dmp1 = dmp1; |
311e903d | 380 | BN_set_flags(r->dmp1, BN_FLG_CONSTTIME); |
1da12e34 RL |
381 | } |
382 | if (dmq1 != NULL) { | |
c033101d | 383 | BN_clear_free(r->dmq1); |
1da12e34 | 384 | r->dmq1 = dmq1; |
311e903d | 385 | BN_set_flags(r->dmq1, BN_FLG_CONSTTIME); |
1da12e34 RL |
386 | } |
387 | if (iqmp != NULL) { | |
c033101d | 388 | BN_clear_free(r->iqmp); |
1da12e34 | 389 | r->iqmp = iqmp; |
311e903d | 390 | BN_set_flags(r->iqmp, BN_FLG_CONSTTIME); |
1da12e34 | 391 | } |
29be6023 | 392 | r->dirty_cnt++; |
9862e9aa RL |
393 | |
394 | return 1; | |
395 | } | |
396 | ||
665d899f PY |
397 | /* |
398 | * Is it better to export RSA_PRIME_INFO structure | |
399 | * and related functions to let user pass a triplet? | |
400 | */ | |
401 | int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[], | |
402 | BIGNUM *coeffs[], int pnum) | |
403 | { | |
404 | STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL; | |
405 | RSA_PRIME_INFO *pinfo; | |
406 | int i; | |
407 | ||
408 | if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0) | |
409 | return 0; | |
410 | ||
411 | prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum); | |
412 | if (prime_infos == NULL) | |
413 | return 0; | |
414 | ||
415 | if (r->prime_infos != NULL) | |
416 | old = r->prime_infos; | |
417 | ||
418 | for (i = 0; i < pnum; i++) { | |
419 | pinfo = rsa_multip_info_new(); | |
420 | if (pinfo == NULL) | |
421 | goto err; | |
422 | if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) { | |
d2baf88c CPG |
423 | BN_clear_free(pinfo->r); |
424 | BN_clear_free(pinfo->d); | |
425 | BN_clear_free(pinfo->t); | |
665d899f PY |
426 | pinfo->r = primes[i]; |
427 | pinfo->d = exps[i]; | |
428 | pinfo->t = coeffs[i]; | |
d2baf88c CPG |
429 | BN_set_flags(pinfo->r, BN_FLG_CONSTTIME); |
430 | BN_set_flags(pinfo->d, BN_FLG_CONSTTIME); | |
431 | BN_set_flags(pinfo->t, BN_FLG_CONSTTIME); | |
665d899f PY |
432 | } else { |
433 | rsa_multip_info_free(pinfo); | |
434 | goto err; | |
435 | } | |
436 | (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo); | |
437 | } | |
438 | ||
439 | r->prime_infos = prime_infos; | |
440 | ||
441 | if (!rsa_multip_calc_product(r)) { | |
442 | r->prime_infos = old; | |
443 | goto err; | |
444 | } | |
445 | ||
446 | if (old != NULL) { | |
447 | /* | |
448 | * This is hard to deal with, since the old infos could | |
449 | * also be set by this function and r, d, t should not | |
450 | * be freed in that case. So currently, stay consistent | |
451 | * with other *set0* functions: just free it... | |
452 | */ | |
453 | sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free); | |
454 | } | |
455 | ||
456 | r->version = RSA_ASN1_VERSION_MULTI; | |
29be6023 | 457 | r->dirty_cnt++; |
665d899f PY |
458 | |
459 | return 1; | |
460 | err: | |
461 | /* r, d, t should not be freed */ | |
462 | sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex); | |
463 | return 0; | |
464 | } | |
465 | ||
fd809cfd RL |
466 | void RSA_get0_key(const RSA *r, |
467 | const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) | |
9862e9aa RL |
468 | { |
469 | if (n != NULL) | |
470 | *n = r->n; | |
471 | if (e != NULL) | |
472 | *e = r->e; | |
473 | if (d != NULL) | |
474 | *d = r->d; | |
475 | } | |
476 | ||
fd809cfd | 477 | void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) |
9862e9aa RL |
478 | { |
479 | if (p != NULL) | |
480 | *p = r->p; | |
481 | if (q != NULL) | |
482 | *q = r->q; | |
483 | } | |
484 | ||
665d899f PY |
485 | int RSA_get_multi_prime_extra_count(const RSA *r) |
486 | { | |
487 | int pnum; | |
488 | ||
489 | pnum = sk_RSA_PRIME_INFO_num(r->prime_infos); | |
490 | if (pnum <= 0) | |
491 | pnum = 0; | |
492 | return pnum; | |
493 | } | |
494 | ||
495 | int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[]) | |
496 | { | |
497 | int pnum, i; | |
498 | RSA_PRIME_INFO *pinfo; | |
499 | ||
500 | if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0) | |
501 | return 0; | |
502 | ||
503 | /* | |
504 | * return other primes | |
505 | * it's caller's responsibility to allocate oth_primes[pnum] | |
506 | */ | |
507 | for (i = 0; i < pnum; i++) { | |
508 | pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i); | |
509 | primes[i] = pinfo->r; | |
510 | } | |
511 | ||
512 | return 1; | |
513 | } | |
514 | ||
9862e9aa | 515 | void RSA_get0_crt_params(const RSA *r, |
fd809cfd RL |
516 | const BIGNUM **dmp1, const BIGNUM **dmq1, |
517 | const BIGNUM **iqmp) | |
9862e9aa RL |
518 | { |
519 | if (dmp1 != NULL) | |
520 | *dmp1 = r->dmp1; | |
521 | if (dmq1 != NULL) | |
522 | *dmq1 = r->dmq1; | |
523 | if (iqmp != NULL) | |
524 | *iqmp = r->iqmp; | |
525 | } | |
526 | ||
665d899f PY |
527 | int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[], |
528 | const BIGNUM *coeffs[]) | |
529 | { | |
530 | int pnum; | |
531 | ||
532 | if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0) | |
533 | return 0; | |
534 | ||
535 | /* return other primes */ | |
536 | if (exps != NULL || coeffs != NULL) { | |
537 | RSA_PRIME_INFO *pinfo; | |
538 | int i; | |
539 | ||
540 | /* it's the user's job to guarantee the buffer length */ | |
541 | for (i = 0; i < pnum; i++) { | |
542 | pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i); | |
543 | if (exps != NULL) | |
544 | exps[i] = pinfo->d; | |
545 | if (coeffs != NULL) | |
546 | coeffs[i] = pinfo->t; | |
547 | } | |
548 | } | |
549 | ||
550 | return 1; | |
551 | } | |
552 | ||
6692ff77 DMSP |
553 | const BIGNUM *RSA_get0_n(const RSA *r) |
554 | { | |
555 | return r->n; | |
556 | } | |
557 | ||
558 | const BIGNUM *RSA_get0_e(const RSA *r) | |
559 | { | |
560 | return r->e; | |
561 | } | |
562 | ||
563 | const BIGNUM *RSA_get0_d(const RSA *r) | |
564 | { | |
565 | return r->d; | |
566 | } | |
567 | ||
568 | const BIGNUM *RSA_get0_p(const RSA *r) | |
569 | { | |
570 | return r->p; | |
571 | } | |
572 | ||
573 | const BIGNUM *RSA_get0_q(const RSA *r) | |
574 | { | |
575 | return r->q; | |
576 | } | |
577 | ||
578 | const BIGNUM *RSA_get0_dmp1(const RSA *r) | |
579 | { | |
580 | return r->dmp1; | |
581 | } | |
582 | ||
583 | const BIGNUM *RSA_get0_dmq1(const RSA *r) | |
584 | { | |
585 | return r->dmq1; | |
586 | } | |
587 | ||
588 | const BIGNUM *RSA_get0_iqmp(const RSA *r) | |
589 | { | |
590 | return r->iqmp; | |
591 | } | |
592 | ||
677add38 RL |
593 | const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r) |
594 | { | |
595 | return r->pss; | |
596 | } | |
597 | ||
9862e9aa RL |
598 | void RSA_clear_flags(RSA *r, int flags) |
599 | { | |
600 | r->flags &= ~flags; | |
601 | } | |
602 | ||
603 | int RSA_test_flags(const RSA *r, int flags) | |
604 | { | |
605 | return r->flags & flags; | |
606 | } | |
607 | ||
608 | void RSA_set_flags(RSA *r, int flags) | |
609 | { | |
610 | r->flags |= flags; | |
611 | } | |
612 | ||
665d899f PY |
613 | int RSA_get_version(RSA *r) |
614 | { | |
615 | /* { two-prime(0), multi(1) } */ | |
616 | return r->version; | |
617 | } | |
618 | ||
e0685d24 | 619 | ENGINE *RSA_get0_engine(const RSA *r) |
9862e9aa RL |
620 | { |
621 | return r->engine; | |
622 | } | |
e5e04ee3 DSH |
623 | |
624 | int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2) | |
625 | { | |
626 | /* If key type not RSA or RSA-PSS return error */ | |
627 | if (ctx != NULL && ctx->pmeth != NULL | |
628 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA | |
629 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS) | |
630 | return -1; | |
631 | return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2); | |
632 | } | |
c3a4fa4c RL |
633 | |
634 | DEFINE_STACK_OF(BIGNUM) | |
635 | ||
636 | int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes, | |
637 | const STACK_OF(BIGNUM) *exps, | |
638 | const STACK_OF(BIGNUM) *coeffs) | |
639 | { | |
640 | STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL; | |
641 | int pnum; | |
642 | ||
643 | if (primes == NULL || exps == NULL || coeffs == NULL) | |
644 | return 0; | |
645 | ||
646 | pnum = sk_BIGNUM_num(primes); | |
647 | if (pnum < 2 | |
648 | || pnum != sk_BIGNUM_num(exps) | |
649 | || pnum != sk_BIGNUM_num(coeffs) + 1) | |
650 | return 0; | |
651 | ||
652 | if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0), | |
653 | sk_BIGNUM_value(primes, 1)) | |
654 | || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0), | |
655 | sk_BIGNUM_value(exps, 1), | |
656 | sk_BIGNUM_value(coeffs, 0))) | |
657 | return 0; | |
658 | ||
659 | old_infos = r->prime_infos; | |
660 | ||
661 | if (pnum > 2) { | |
662 | int i; | |
663 | ||
664 | prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum); | |
665 | if (prime_infos == NULL) | |
666 | return 0; | |
667 | ||
668 | for (i = 2; i < pnum; i++) { | |
669 | BIGNUM *prime = sk_BIGNUM_value(primes, i); | |
670 | BIGNUM *exp = sk_BIGNUM_value(exps, i); | |
671 | BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1); | |
672 | RSA_PRIME_INFO *pinfo = NULL; | |
673 | ||
674 | if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL)) | |
675 | goto err; | |
676 | ||
677 | /* Using rsa_multip_info_new() is wasteful, so allocate directly */ | |
678 | if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) { | |
679 | ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE); | |
680 | goto err; | |
681 | } | |
682 | ||
683 | pinfo->r = prime; | |
684 | pinfo->d = exp; | |
685 | pinfo->t = coeff; | |
686 | BN_set_flags(pinfo->r, BN_FLG_CONSTTIME); | |
687 | BN_set_flags(pinfo->d, BN_FLG_CONSTTIME); | |
688 | BN_set_flags(pinfo->t, BN_FLG_CONSTTIME); | |
689 | (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo); | |
690 | } | |
691 | ||
692 | r->prime_infos = prime_infos; | |
693 | ||
694 | if (!rsa_multip_calc_product(r)) { | |
695 | r->prime_infos = old_infos; | |
696 | goto err; | |
697 | } | |
698 | } | |
699 | ||
700 | if (old_infos != NULL) { | |
701 | /* | |
702 | * This is hard to deal with, since the old infos could | |
703 | * also be set by this function and r, d, t should not | |
704 | * be freed in that case. So currently, stay consistent | |
705 | * with other *set0* functions: just free it... | |
706 | */ | |
707 | sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free); | |
708 | } | |
709 | ||
710 | r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT; | |
711 | r->dirty_cnt++; | |
712 | ||
713 | return 1; | |
714 | err: | |
715 | /* r, d, t should not be freed */ | |
716 | sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex); | |
717 | return 0; | |
718 | } | |
719 | ||
720 | DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM) | |
721 | ||
722 | int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes, | |
723 | STACK_OF(BIGNUM_const) *exps, | |
724 | STACK_OF(BIGNUM_const) *coeffs) | |
725 | { | |
726 | RSA_PRIME_INFO *pinfo; | |
727 | int i, pnum; | |
728 | ||
729 | if (r == NULL) | |
730 | return 0; | |
731 | ||
732 | pnum = RSA_get_multi_prime_extra_count(r); | |
733 | ||
734 | sk_BIGNUM_const_push(primes, RSA_get0_p(r)); | |
735 | sk_BIGNUM_const_push(primes, RSA_get0_q(r)); | |
736 | sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r)); | |
737 | sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r)); | |
738 | sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r)); | |
739 | for (i = 0; i < pnum; i++) { | |
740 | pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i); | |
741 | sk_BIGNUM_const_push(primes, pinfo->r); | |
742 | sk_BIGNUM_const_push(exps, pinfo->d); | |
743 | sk_BIGNUM_const_push(coeffs, pinfo->t); | |
744 | } | |
745 | ||
746 | return 1; | |
747 | } | |
89abd1b6 MC |
748 | |
749 | int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode) | |
750 | { | |
751 | OSSL_PARAM pad_params[2], *p = pad_params; | |
752 | ||
753 | if (ctx == NULL) { | |
754 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
755 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
756 | return -2; | |
757 | } | |
758 | ||
759 | /* If key type not RSA or RSA-PSS return error */ | |
760 | if (ctx->pmeth != NULL | |
761 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA | |
762 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS) | |
763 | return -1; | |
764 | ||
765 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
766 | if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
767 | || ctx->op.ciph.ciphprovctx == NULL) | |
768 | return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING, | |
769 | pad_mode, NULL); | |
770 | ||
771 | *p++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, &pad_mode); | |
772 | *p++ = OSSL_PARAM_construct_end(); | |
773 | ||
774 | return EVP_PKEY_CTX_set_params(ctx, pad_params); | |
775 | } | |
776 | ||
777 | int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode) | |
778 | { | |
779 | OSSL_PARAM pad_params[2], *p = pad_params; | |
780 | ||
781 | if (ctx == NULL || pad_mode == NULL) { | |
782 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
783 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
784 | return -2; | |
785 | } | |
786 | ||
787 | /* If key type not RSA or RSA-PSS return error */ | |
788 | if (ctx->pmeth != NULL | |
789 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA | |
790 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS) | |
791 | return -1; | |
792 | ||
793 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
794 | if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
795 | || ctx->op.ciph.ciphprovctx == NULL) | |
796 | return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0, | |
797 | pad_mode); | |
798 | ||
799 | *p++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, pad_mode); | |
800 | *p++ = OSSL_PARAM_construct_end(); | |
801 | ||
802 | if (!EVP_PKEY_CTX_get_params(ctx, pad_params)) | |
803 | return 0; | |
804 | ||
805 | return 1; | |
806 | ||
807 | } | |
808 | ||
809 | int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) | |
810 | { | |
811 | const char *name; | |
812 | ||
813 | if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { | |
814 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
815 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
816 | return -2; | |
817 | } | |
818 | ||
819 | /* If key type not RSA return error */ | |
820 | if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA) | |
821 | return -1; | |
822 | ||
823 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
824 | if (ctx->op.ciph.ciphprovctx == NULL) | |
825 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, | |
826 | EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md); | |
827 | ||
828 | name = (md == NULL) ? "" : EVP_MD_name(md); | |
829 | ||
830 | return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL); | |
831 | } | |
832 | ||
833 | int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname, | |
834 | const char *mdprops) | |
835 | { | |
836 | OSSL_PARAM rsa_params[3], *p = rsa_params; | |
837 | ||
838 | if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { | |
839 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
840 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
841 | return -2; | |
842 | } | |
843 | ||
844 | /* If key type not RSA return error */ | |
845 | if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA) | |
846 | return -1; | |
847 | ||
848 | ||
849 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, | |
850 | /* | |
851 | * Cast away the const. This is read | |
852 | * only so should be safe | |
853 | */ | |
854 | (char *)mdname, | |
855 | strlen(mdname) + 1); | |
856 | if (mdprops != NULL) { | |
857 | *p++ = OSSL_PARAM_construct_utf8_string( | |
858 | OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, | |
859 | /* | |
860 | * Cast away the const. This is read | |
861 | * only so should be safe | |
862 | */ | |
863 | (char *)mdprops, | |
864 | strlen(mdprops) + 1); | |
865 | } | |
866 | *p++ = OSSL_PARAM_construct_end(); | |
867 | ||
868 | return EVP_PKEY_CTX_set_params(ctx, rsa_params); | |
869 | } | |
870 | ||
871 | int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name, | |
872 | size_t namelen) | |
873 | { | |
874 | OSSL_PARAM rsa_params[2], *p = rsa_params; | |
875 | ||
876 | if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { | |
877 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
878 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
879 | return -2; | |
880 | } | |
881 | ||
882 | /* If key type not RSA return error */ | |
883 | if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA) | |
884 | return -1; | |
885 | ||
886 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, | |
887 | name, namelen); | |
888 | *p++ = OSSL_PARAM_construct_end(); | |
889 | ||
890 | if (!EVP_PKEY_CTX_get_params(ctx, rsa_params)) | |
891 | return -1; | |
892 | ||
893 | return 1; | |
894 | } | |
895 | ||
896 | int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md) | |
897 | { | |
898 | /* 80 should be big enough */ | |
899 | char name[80] = ""; | |
900 | ||
901 | if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { | |
902 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
903 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
904 | return -2; | |
905 | } | |
906 | ||
907 | /* If key type not RSA return error */ | |
908 | if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA) | |
909 | return -1; | |
910 | ||
911 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
912 | if (ctx->op.ciph.ciphprovctx == NULL) | |
913 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, | |
914 | EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md); | |
915 | ||
916 | if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0) | |
917 | return -1; | |
918 | ||
919 | /* May be NULL meaning "unknown" */ | |
920 | *md = EVP_get_digestbyname(name); | |
921 | ||
922 | return 1; | |
923 | } | |
924 | ||
925 | int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) | |
926 | { | |
927 | const char *name; | |
928 | ||
929 | if (ctx == NULL | |
930 | || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
931 | && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) { | |
932 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
933 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
934 | return -2; | |
935 | } | |
936 | ||
937 | /* If key type not RSA return error */ | |
938 | if (ctx->pmeth != NULL | |
939 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA | |
940 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS) | |
941 | return -1; | |
942 | ||
943 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
944 | if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
945 | && ctx->op.ciph.ciphprovctx == NULL) | |
946 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
947 | && ctx->op.sig.sigprovctx == NULL)) | |
948 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, | |
949 | EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, | |
950 | EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md); | |
951 | ||
952 | name = (md == NULL) ? "" : EVP_MD_name(md); | |
953 | ||
954 | return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL); | |
955 | } | |
956 | ||
957 | int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname, | |
958 | const char *mdprops) | |
959 | { | |
960 | OSSL_PARAM rsa_params[3], *p = rsa_params; | |
961 | ||
962 | if (ctx == NULL | |
963 | || mdname == NULL | |
964 | || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
965 | && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) { | |
966 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
967 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
968 | return -2; | |
969 | } | |
970 | ||
971 | /* If key type not RSA return error */ | |
972 | if (ctx->pmeth != NULL | |
973 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA | |
974 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS) | |
975 | return -1; | |
976 | ||
977 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, | |
978 | /* | |
979 | * Cast away the const. This is read | |
980 | * only so should be safe | |
981 | */ | |
982 | (char *)mdname, | |
983 | strlen(mdname) + 1); | |
984 | if (mdprops != NULL) { | |
985 | *p++ = OSSL_PARAM_construct_utf8_string( | |
986 | OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, | |
987 | /* | |
988 | * Cast away the const. This is read | |
989 | * only so should be safe | |
990 | */ | |
991 | (char *)mdprops, | |
992 | strlen(mdprops) + 1); | |
993 | } | |
994 | *p++ = OSSL_PARAM_construct_end(); | |
995 | ||
996 | return EVP_PKEY_CTX_set_params(ctx, rsa_params); | |
997 | } | |
998 | ||
999 | int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name, | |
1000 | size_t namelen) | |
1001 | { | |
1002 | OSSL_PARAM rsa_params[2], *p = rsa_params; | |
1003 | ||
1004 | if (ctx == NULL | |
1005 | || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
1006 | && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) { | |
1007 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1008 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
1009 | return -2; | |
1010 | } | |
1011 | ||
1012 | /* If key type not RSA or RSA-PSS return error */ | |
1013 | if (ctx->pmeth != NULL | |
1014 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA | |
1015 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS) | |
1016 | return -1; | |
1017 | ||
1018 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, | |
1019 | name, namelen); | |
1020 | *p++ = OSSL_PARAM_construct_end(); | |
1021 | ||
1022 | if (!EVP_PKEY_CTX_get_params(ctx, rsa_params)) | |
1023 | return -1; | |
1024 | ||
1025 | return 1; | |
1026 | } | |
1027 | ||
1028 | int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md) | |
1029 | { | |
1030 | /* 80 should be big enough */ | |
1031 | char name[80] = ""; | |
1032 | ||
1033 | if (ctx == NULL | |
1034 | || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
1035 | && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) { | |
1036 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1037 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
1038 | return -2; | |
1039 | } | |
1040 | ||
1041 | /* If key type not RSA or RSA-PSS return error */ | |
1042 | if (ctx->pmeth != NULL | |
1043 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA | |
1044 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS) | |
1045 | return -1; | |
1046 | ||
1047 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
1048 | if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
1049 | && ctx->op.ciph.ciphprovctx == NULL) | |
1050 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
1051 | && ctx->op.sig.sigprovctx == NULL)) | |
1052 | return EVP_PKEY_CTX_ctrl(ctx, -1, | |
1053 | EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, | |
1054 | EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md); | |
1055 | ||
1056 | if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0) | |
1057 | return -1; | |
1058 | ||
1059 | /* May be NULL meaning "unknown" */ | |
1060 | *md = EVP_get_digestbyname(name); | |
1061 | ||
1062 | return 1; | |
1063 | } | |
1064 | ||
1065 | int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen) | |
1066 | { | |
1067 | OSSL_PARAM rsa_params[2], *p = rsa_params; | |
1068 | ||
1069 | if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { | |
1070 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1071 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
1072 | return -2; | |
1073 | } | |
1074 | ||
1075 | /* If key type not RSA return error */ | |
1076 | if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA) | |
1077 | return -1; | |
1078 | ||
1079 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
1080 | if (ctx->op.ciph.ciphprovctx == NULL) | |
1081 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, | |
1082 | EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen, | |
1083 | (void *)label); | |
1084 | ||
1085 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, | |
1086 | /* | |
1087 | * Cast away the const. This is read | |
1088 | * only so should be safe | |
1089 | */ | |
1090 | (void *)label, | |
1091 | (size_t)llen); | |
1092 | *p++ = OSSL_PARAM_construct_end(); | |
1093 | ||
1094 | if (!EVP_PKEY_CTX_set_params(ctx, rsa_params)) | |
1095 | return 0; | |
1096 | ||
1097 | OPENSSL_free(label); | |
1098 | return 1; | |
1099 | } | |
1100 | ||
1101 | int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label) | |
1102 | { | |
1103 | OSSL_PARAM rsa_params[3], *p = rsa_params; | |
1104 | size_t labellen; | |
1105 | ||
1106 | if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { | |
1107 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1108 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
1109 | return -2; | |
1110 | } | |
1111 | ||
1112 | /* If key type not RSA return error */ | |
1113 | if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA) | |
1114 | return -1; | |
1115 | ||
1116 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
1117 | if (ctx->op.ciph.ciphprovctx == NULL) | |
1118 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, | |
1119 | EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0, | |
1120 | (void *)label); | |
1121 | ||
1122 | *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, | |
1123 | (void **)label, 0); | |
1124 | *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN, | |
1125 | &labellen); | |
1126 | *p++ = OSSL_PARAM_construct_end(); | |
1127 | ||
1128 | if (!EVP_PKEY_CTX_get_params(ctx, rsa_params)) | |
1129 | return -1; | |
1130 | ||
1131 | if (labellen > INT_MAX) | |
1132 | return -1; | |
1133 | ||
1134 | return (int)labellen; | |
1135 | } |