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 { 22 return RSA_new_method(NULL); 23 } 24 25 const RSA_METHOD *RSA_get_method(const RSA *rsa) 26 { 27 return rsa->meth; 28 } 29 30 int 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 50 RSA *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 105 void 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 144 int 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 156 int RSA_set_ex_data(RSA *r, int idx, void *arg) 157 { 158 return CRYPTO_set_ex_data(&r->ex_data, idx, arg); 159 } 160 161 void *RSA_get_ex_data(const RSA *r, int idx) 162 { 163 return CRYPTO_get_ex_data(&r->ex_data, idx); 164 } 165 166 int 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 180 int 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 } 202 203 return 1; 204 } 205 206 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) 207 { 208 /* If the fields p and q in r are NULL, the corresponding input 209 * parameters MUST be non-NULL. 210 */ 211 if ((r->p == NULL && p == NULL) 212 || (r->q == NULL && q == NULL)) 213 return 0; 214 215 if (p != NULL) { 216 BN_clear_free(r->p); 217 r->p = p; 218 } 219 if (q != NULL) { 220 BN_clear_free(r->q); 221 r->q = q; 222 } 223 224 return 1; 225 } 226 227 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) 228 { 229 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input 230 * parameters MUST be non-NULL. 231 */ 232 if ((r->dmp1 == NULL && dmp1 == NULL) 233 || (r->dmq1 == NULL && dmq1 == NULL) 234 || (r->iqmp == NULL && iqmp == NULL)) 235 return 0; 236 237 if (dmp1 != NULL) { 238 BN_clear_free(r->dmp1); 239 r->dmp1 = dmp1; 240 } 241 if (dmq1 != NULL) { 242 BN_clear_free(r->dmq1); 243 r->dmq1 = dmq1; 244 } 245 if (iqmp != NULL) { 246 BN_clear_free(r->iqmp); 247 r->iqmp = iqmp; 248 } 249 250 return 1; 251 } 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 { 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 318 void RSA_get0_key(const RSA *r, 319 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) 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 329 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) 330 { 331 if (p != NULL) 332 *p = r->p; 333 if (q != NULL) 334 *q = r->q; 335 } 336 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 367 void RSA_get0_crt_params(const RSA *r, 368 const BIGNUM **dmp1, const BIGNUM **dmq1, 369 const BIGNUM **iqmp) 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 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 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 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 460 int RSA_get_version(RSA *r) 461 { 462 /* { two-prime(0), multi(1) } */ 463 return r->version; 464 } 465 466 ENGINE *RSA_get0_engine(const RSA *r) 467 { 468 return r->engine; 469 } 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 } 480