1 /* $OpenBSD: rsa_lib.c,v 1.40 2020/01/17 10:40:03 inoguchi Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 #include <stdio.h> 60 61 #include <openssl/opensslconf.h> 62 63 #include <openssl/bn.h> 64 #include <openssl/crypto.h> 65 #include <openssl/err.h> 66 #include <openssl/evp.h> 67 #include <openssl/lhash.h> 68 #include <openssl/rsa.h> 69 70 #include "evp_locl.h" 71 72 #ifndef OPENSSL_NO_ENGINE 73 #include <openssl/engine.h> 74 #endif 75 76 static const RSA_METHOD *default_RSA_meth = NULL; 77 78 RSA * 79 RSA_new(void) 80 { 81 RSA *r = RSA_new_method(NULL); 82 83 return r; 84 } 85 86 void 87 RSA_set_default_method(const RSA_METHOD *meth) 88 { 89 default_RSA_meth = meth; 90 } 91 92 const RSA_METHOD * 93 RSA_get_default_method(void) 94 { 95 if (default_RSA_meth == NULL) 96 default_RSA_meth = RSA_PKCS1_SSLeay(); 97 98 return default_RSA_meth; 99 } 100 101 const RSA_METHOD * 102 RSA_get_method(const RSA *rsa) 103 { 104 return rsa->meth; 105 } 106 107 int 108 RSA_set_method(RSA *rsa, const RSA_METHOD *meth) 109 { 110 /* 111 * NB: The caller is specifically setting a method, so it's not up to us 112 * to deal with which ENGINE it comes from. 113 */ 114 const RSA_METHOD *mtmp; 115 116 mtmp = rsa->meth; 117 if (mtmp->finish) 118 mtmp->finish(rsa); 119 #ifndef OPENSSL_NO_ENGINE 120 ENGINE_finish(rsa->engine); 121 rsa->engine = NULL; 122 #endif 123 rsa->meth = meth; 124 if (meth->init) 125 meth->init(rsa); 126 return 1; 127 } 128 129 RSA * 130 RSA_new_method(ENGINE *engine) 131 { 132 RSA *ret; 133 134 if ((ret = calloc(1, sizeof(RSA))) == NULL) { 135 RSAerror(ERR_R_MALLOC_FAILURE); 136 return NULL; 137 } 138 139 ret->meth = RSA_get_default_method(); 140 141 #ifndef OPENSSL_NO_ENGINE 142 if (engine != NULL) { 143 if (!ENGINE_init(engine)) { 144 RSAerror(ERR_R_ENGINE_LIB); 145 goto err; 146 } 147 ret->engine = engine; 148 } else { 149 ret->engine = ENGINE_get_default_RSA(); 150 } 151 152 if (ret->engine != NULL) { 153 if ((ret->meth = ENGINE_get_RSA(ret->engine)) == NULL) { 154 RSAerror(ERR_R_ENGINE_LIB); 155 goto err; 156 } 157 } 158 #endif 159 160 ret->references = 1; 161 ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; 162 163 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) 164 goto err; 165 166 if (ret->meth->init != NULL && !ret->meth->init(ret)) { 167 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data); 168 goto err; 169 } 170 171 return ret; 172 173 err: 174 #ifndef OPENSSL_NO_ENGINE 175 ENGINE_finish(ret->engine); 176 #endif 177 free(ret); 178 179 return NULL; 180 } 181 182 void 183 RSA_free(RSA *r) 184 { 185 int i; 186 187 if (r == NULL) 188 return; 189 190 i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_RSA); 191 if (i > 0) 192 return; 193 194 if (r->meth->finish) 195 r->meth->finish(r); 196 #ifndef OPENSSL_NO_ENGINE 197 ENGINE_finish(r->engine); 198 #endif 199 200 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data); 201 202 BN_clear_free(r->n); 203 BN_clear_free(r->e); 204 BN_clear_free(r->d); 205 BN_clear_free(r->p); 206 BN_clear_free(r->q); 207 BN_clear_free(r->dmp1); 208 BN_clear_free(r->dmq1); 209 BN_clear_free(r->iqmp); 210 BN_BLINDING_free(r->blinding); 211 BN_BLINDING_free(r->mt_blinding); 212 RSA_PSS_PARAMS_free(r->pss); 213 free(r); 214 } 215 216 int 217 RSA_up_ref(RSA *r) 218 { 219 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA); 220 return i > 1 ? 1 : 0; 221 } 222 223 int 224 RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func, 225 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) 226 { 227 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp, 228 new_func, dup_func, free_func); 229 } 230 231 int 232 RSA_set_ex_data(RSA *r, int idx, void *arg) 233 { 234 return CRYPTO_set_ex_data(&r->ex_data, idx, arg); 235 } 236 237 void * 238 RSA_get_ex_data(const RSA *r, int idx) 239 { 240 return CRYPTO_get_ex_data(&r->ex_data, idx); 241 } 242 243 void 244 RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) 245 { 246 if (n != NULL) 247 *n = r->n; 248 if (e != NULL) 249 *e = r->e; 250 if (d != NULL) 251 *d = r->d; 252 } 253 254 int 255 RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) 256 { 257 if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL)) 258 return 0; 259 260 if (n != NULL) { 261 BN_free(r->n); 262 r->n = n; 263 } 264 if (e != NULL) { 265 BN_free(r->e); 266 r->e = e; 267 } 268 if (d != NULL) { 269 BN_free(r->d); 270 r->d = d; 271 } 272 273 return 1; 274 } 275 276 void 277 RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1, 278 const BIGNUM **iqmp) 279 { 280 if (dmp1 != NULL) 281 *dmp1 = r->dmp1; 282 if (dmq1 != NULL) 283 *dmq1 = r->dmq1; 284 if (iqmp != NULL) 285 *iqmp = r->iqmp; 286 } 287 288 int 289 RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) 290 { 291 if ((r->dmp1 == NULL && dmp1 == NULL) || 292 (r->dmq1 == NULL && dmq1 == NULL) || 293 (r->iqmp == NULL && iqmp == NULL)) 294 return 0; 295 296 if (dmp1 != NULL) { 297 BN_free(r->dmp1); 298 r->dmp1 = dmp1; 299 } 300 if (dmq1 != NULL) { 301 BN_free(r->dmq1); 302 r->dmq1 = dmq1; 303 } 304 if (iqmp != NULL) { 305 BN_free(r->iqmp); 306 r->iqmp = iqmp; 307 } 308 309 return 1; 310 } 311 312 void 313 RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) 314 { 315 if (p != NULL) 316 *p = r->p; 317 if (q != NULL) 318 *q = r->q; 319 } 320 321 int 322 RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) 323 { 324 if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL)) 325 return 0; 326 327 if (p != NULL) { 328 BN_free(r->p); 329 r->p = p; 330 } 331 if (q != NULL) { 332 BN_free(r->q); 333 r->q = q; 334 } 335 336 return 1; 337 } 338 339 void 340 RSA_clear_flags(RSA *r, int flags) 341 { 342 r->flags &= ~flags; 343 } 344 345 int 346 RSA_test_flags(const RSA *r, int flags) 347 { 348 return r->flags & flags; 349 } 350 351 void 352 RSA_set_flags(RSA *r, int flags) 353 { 354 r->flags |= flags; 355 } 356 357 int 358 RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2) 359 { 360 /* Return an error if the key type is not RSA or RSA-PSS. */ 361 if (ctx != NULL && ctx->pmeth != NULL && 362 ctx->pmeth->pkey_id != EVP_PKEY_RSA && 363 ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS) 364 return -1; 365 366 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2); 367 } 368