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