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