1/* 2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved. 3 * 4 * Licensed under the Apache License 2.0 (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/* 11 * RSA low level APIs are deprecated for public use, but still ok for 12 * internal use. 13 */ 14#include "internal/deprecated.h" 15 16#include <string.h> 17#include "rsa_local.h" 18#include <openssl/err.h> 19 20RSA_METHOD *RSA_meth_new(const char *name, int flags) 21{ 22 RSA_METHOD *meth = OPENSSL_zalloc(sizeof(*meth)); 23 24 if (meth != NULL) { 25 meth->flags = flags; 26 27 meth->name = OPENSSL_strdup(name); 28 if (meth->name != NULL) 29 return meth; 30 31 OPENSSL_free(meth); 32 } 33 34 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE); 35 return NULL; 36} 37 38void RSA_meth_free(RSA_METHOD *meth) 39{ 40 if (meth != NULL) { 41 OPENSSL_free(meth->name); 42 OPENSSL_free(meth); 43 } 44} 45 46RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth) 47{ 48 RSA_METHOD *ret = OPENSSL_malloc(sizeof(*ret)); 49 50 if (ret != NULL) { 51 memcpy(ret, meth, sizeof(*meth)); 52 53 ret->name = OPENSSL_strdup(meth->name); 54 if (ret->name != NULL) 55 return ret; 56 57 OPENSSL_free(ret); 58 } 59 60 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE); 61 return NULL; 62} 63 64const char *RSA_meth_get0_name(const RSA_METHOD *meth) 65{ 66 return meth->name; 67} 68 69int RSA_meth_set1_name(RSA_METHOD *meth, const char *name) 70{ 71 char *tmpname = OPENSSL_strdup(name); 72 73 if (tmpname == NULL) { 74 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE); 75 return 0; 76 } 77 78 OPENSSL_free(meth->name); 79 meth->name = tmpname; 80 81 return 1; 82} 83 84int RSA_meth_get_flags(const RSA_METHOD *meth) 85{ 86 return meth->flags; 87} 88 89int RSA_meth_set_flags(RSA_METHOD *meth, int flags) 90{ 91 meth->flags = flags; 92 return 1; 93} 94 95void *RSA_meth_get0_app_data(const RSA_METHOD *meth) 96{ 97 return meth->app_data; 98} 99 100int RSA_meth_set0_app_data(RSA_METHOD *meth, void *app_data) 101{ 102 meth->app_data = app_data; 103 return 1; 104} 105 106int (*RSA_meth_get_pub_enc(const RSA_METHOD *meth)) 107 (int flen, const unsigned char *from, 108 unsigned char *to, RSA *rsa, int padding) 109{ 110 return meth->rsa_pub_enc; 111} 112 113int RSA_meth_set_pub_enc(RSA_METHOD *meth, 114 int (*pub_enc) (int flen, const unsigned char *from, 115 unsigned char *to, RSA *rsa, 116 int padding)) 117{ 118 meth->rsa_pub_enc = pub_enc; 119 return 1; 120} 121 122int (*RSA_meth_get_pub_dec(const RSA_METHOD *meth)) 123 (int flen, const unsigned char *from, 124 unsigned char *to, RSA *rsa, int padding) 125{ 126 return meth->rsa_pub_dec; 127} 128 129int RSA_meth_set_pub_dec(RSA_METHOD *meth, 130 int (*pub_dec) (int flen, const unsigned char *from, 131 unsigned char *to, RSA *rsa, 132 int padding)) 133{ 134 meth->rsa_pub_dec = pub_dec; 135 return 1; 136} 137 138int (*RSA_meth_get_priv_enc(const RSA_METHOD *meth)) 139 (int flen, const unsigned char *from, 140 unsigned char *to, RSA *rsa, int padding) 141{ 142 return meth->rsa_priv_enc; 143} 144 145int RSA_meth_set_priv_enc(RSA_METHOD *meth, 146 int (*priv_enc) (int flen, const unsigned char *from, 147 unsigned char *to, RSA *rsa, 148 int padding)) 149{ 150 meth->rsa_priv_enc = priv_enc; 151 return 1; 152} 153 154int (*RSA_meth_get_priv_dec(const RSA_METHOD *meth)) 155 (int flen, const unsigned char *from, 156 unsigned char *to, RSA *rsa, int padding) 157{ 158 return meth->rsa_priv_dec; 159} 160 161int RSA_meth_set_priv_dec(RSA_METHOD *meth, 162 int (*priv_dec) (int flen, const unsigned char *from, 163 unsigned char *to, RSA *rsa, 164 int padding)) 165{ 166 meth->rsa_priv_dec = priv_dec; 167 return 1; 168} 169 170 /* Can be null */ 171int (*RSA_meth_get_mod_exp(const RSA_METHOD *meth)) 172 (BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx) 173{ 174 return meth->rsa_mod_exp; 175} 176 177int RSA_meth_set_mod_exp(RSA_METHOD *meth, 178 int (*mod_exp) (BIGNUM *r0, const BIGNUM *i, RSA *rsa, 179 BN_CTX *ctx)) 180{ 181 meth->rsa_mod_exp = mod_exp; 182 return 1; 183} 184 185 /* Can be null */ 186int (*RSA_meth_get_bn_mod_exp(const RSA_METHOD *meth)) 187 (BIGNUM *r, const BIGNUM *a, const BIGNUM *p, 188 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx) 189{ 190 return meth->bn_mod_exp; 191} 192 193int RSA_meth_set_bn_mod_exp(RSA_METHOD *meth, 194 int (*bn_mod_exp) (BIGNUM *r, 195 const BIGNUM *a, 196 const BIGNUM *p, 197 const BIGNUM *m, 198 BN_CTX *ctx, 199 BN_MONT_CTX *m_ctx)) 200{ 201 meth->bn_mod_exp = bn_mod_exp; 202 return 1; 203} 204 205 /* called at new */ 206int (*RSA_meth_get_init(const RSA_METHOD *meth)) (RSA *rsa) 207{ 208 return meth->init; 209} 210 211int RSA_meth_set_init(RSA_METHOD *meth, int (*init) (RSA *rsa)) 212{ 213 meth->init = init; 214 return 1; 215} 216 217 /* called at free */ 218int (*RSA_meth_get_finish(const RSA_METHOD *meth)) (RSA *rsa) 219{ 220 return meth->finish; 221} 222 223int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa)) 224{ 225 meth->finish = finish; 226 return 1; 227} 228 229int (*RSA_meth_get_sign(const RSA_METHOD *meth)) 230 (int type, 231 const unsigned char *m, unsigned int m_length, 232 unsigned char *sigret, unsigned int *siglen, 233 const RSA *rsa) 234{ 235 return meth->rsa_sign; 236} 237 238int RSA_meth_set_sign(RSA_METHOD *meth, 239 int (*sign) (int type, const unsigned char *m, 240 unsigned int m_length, 241 unsigned char *sigret, unsigned int *siglen, 242 const RSA *rsa)) 243{ 244 meth->rsa_sign = sign; 245 return 1; 246} 247 248int (*RSA_meth_get_verify(const RSA_METHOD *meth)) 249 (int dtype, const unsigned char *m, 250 unsigned int m_length, const unsigned char *sigbuf, 251 unsigned int siglen, const RSA *rsa) 252{ 253 return meth->rsa_verify; 254} 255 256int RSA_meth_set_verify(RSA_METHOD *meth, 257 int (*verify) (int dtype, const unsigned char *m, 258 unsigned int m_length, 259 const unsigned char *sigbuf, 260 unsigned int siglen, const RSA *rsa)) 261{ 262 meth->rsa_verify = verify; 263 return 1; 264} 265 266int (*RSA_meth_get_keygen(const RSA_METHOD *meth)) 267 (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb) 268{ 269 return meth->rsa_keygen; 270} 271 272int RSA_meth_set_keygen(RSA_METHOD *meth, 273 int (*keygen) (RSA *rsa, int bits, BIGNUM *e, 274 BN_GENCB *cb)) 275{ 276 meth->rsa_keygen = keygen; 277 return 1; 278} 279 280int (*RSA_meth_get_multi_prime_keygen(const RSA_METHOD *meth)) 281 (RSA *rsa, int bits, int primes, BIGNUM *e, BN_GENCB *cb) 282{ 283 return meth->rsa_multi_prime_keygen; 284} 285 286int RSA_meth_set_multi_prime_keygen(RSA_METHOD *meth, 287 int (*keygen) (RSA *rsa, int bits, 288 int primes, BIGNUM *e, 289 BN_GENCB *cb)) 290{ 291 meth->rsa_multi_prime_keygen = keygen; 292 return 1; 293} 294