1 /* $OpenBSD: rsa_meth.c,v 1.4 2022/01/07 09:55:32 tb Exp $ */ 2 /* 3 * Copyright (c) 2018 Theo Buehler <tb@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <stdlib.h> 19 #include <string.h> 20 21 #include <openssl/err.h> 22 #include <openssl/rsa.h> 23 24 #include "rsa_locl.h" 25 26 RSA_METHOD * 27 RSA_meth_new(const char *name, int flags) 28 { 29 RSA_METHOD *meth; 30 31 if ((meth = calloc(1, sizeof(*meth))) == NULL) 32 return NULL; 33 if ((meth->name = strdup(name)) == NULL) { 34 free(meth); 35 return NULL; 36 } 37 meth->flags = flags; 38 39 return meth; 40 } 41 42 void 43 RSA_meth_free(RSA_METHOD *meth) 44 { 45 if (meth != NULL) { 46 free((char *)meth->name); 47 free(meth); 48 } 49 } 50 51 RSA_METHOD * 52 RSA_meth_dup(const RSA_METHOD *meth) 53 { 54 RSA_METHOD *copy; 55 56 if ((copy = calloc(1, sizeof(*copy))) == NULL) 57 return NULL; 58 memcpy(copy, meth, sizeof(*copy)); 59 if ((copy->name = strdup(meth->name)) == NULL) { 60 free(copy); 61 return NULL; 62 } 63 64 return copy; 65 } 66 67 int 68 RSA_meth_set1_name(RSA_METHOD *meth, const char *name) 69 { 70 char *copy; 71 72 if ((copy = strdup(name)) == NULL) 73 return 0; 74 free((char *)meth->name); 75 meth->name = copy; 76 return 1; 77 } 78 79 int 80 (*RSA_meth_get_finish(const RSA_METHOD *meth))(RSA *rsa) 81 { 82 return meth->finish; 83 } 84 85 int 86 RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc)(int flen, 87 const unsigned char *from, unsigned char *to, RSA *rsa, int padding)) 88 { 89 meth->rsa_priv_enc = priv_enc; 90 return 1; 91 } 92 93 int 94 RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec)(int flen, 95 const unsigned char *from, unsigned char *to, RSA *rsa, int padding)) 96 { 97 meth->rsa_priv_dec = priv_dec; 98 return 1; 99 } 100 101 int 102 RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa)) 103 { 104 meth->finish = finish; 105 return 1; 106 } 107 108 int 109 RSA_meth_set_pub_enc(RSA_METHOD *meth, int (*pub_enc)(int flen, 110 const unsigned char *from, unsigned char *to, RSA *rsa, int padding)) 111 { 112 meth->rsa_pub_enc = pub_enc; 113 return 1; 114 } 115 116 int 117 RSA_meth_set_pub_dec(RSA_METHOD *meth, int (*pub_dec)(int flen, 118 const unsigned char *from, unsigned char *to, RSA *rsa, int padding)) 119 { 120 meth->rsa_pub_dec = pub_dec; 121 return 1; 122 } 123 124 int 125 RSA_meth_set_mod_exp(RSA_METHOD *meth, int (*mod_exp)(BIGNUM *r0, 126 const BIGNUM *i, RSA *rsa, BN_CTX *ctx)) 127 { 128 meth->rsa_mod_exp = mod_exp; 129 return 1; 130 } 131 132 int 133 RSA_meth_set_bn_mod_exp(RSA_METHOD *meth, int (*bn_mod_exp)(BIGNUM *r, 134 const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, 135 BN_MONT_CTX *m_ctx)) 136 { 137 meth->bn_mod_exp = bn_mod_exp; 138 return 1; 139 } 140 141 int 142 RSA_meth_set_init(RSA_METHOD *meth, int (*init)(RSA *rsa)) 143 { 144 meth->init = init; 145 return 1; 146 } 147 148 int 149 RSA_meth_set_keygen(RSA_METHOD *meth, int (*keygen)(RSA *rsa, int bits, 150 BIGNUM *e, BN_GENCB *cb)) 151 { 152 meth->rsa_keygen = keygen; 153 return 1; 154 } 155 156 int 157 RSA_meth_set_flags(RSA_METHOD *meth, int flags) 158 { 159 meth->flags = flags; 160 return 1; 161 } 162 163 int 164 RSA_meth_set0_app_data(RSA_METHOD *meth, void *app_data) 165 { 166 meth->app_data = app_data; 167 return 1; 168 } 169 170 const char * 171 RSA_meth_get0_name(const RSA_METHOD *meth) 172 { 173 return meth->name; 174 } 175 176 int 177 (*RSA_meth_get_pub_enc(const RSA_METHOD *meth))(int flen, 178 const unsigned char *from, unsigned char *to, RSA *rsa, int padding) 179 { 180 return meth->rsa_pub_enc; 181 } 182 183 int 184 (*RSA_meth_get_pub_dec(const RSA_METHOD *meth))(int flen, 185 const unsigned char *from, unsigned char *to, RSA *rsa, int padding) 186 { 187 return meth->rsa_pub_dec; 188 } 189 190 int 191 (*RSA_meth_get_priv_enc(const RSA_METHOD *meth))(int flen, 192 const unsigned char *from, unsigned char *to, RSA *rsa, int padding) 193 { 194 return meth->rsa_priv_enc; 195 } 196 197 int 198 (*RSA_meth_get_priv_dec(const RSA_METHOD *meth))(int flen, 199 const unsigned char *from, unsigned char *to, RSA *rsa, int padding) 200 { 201 return meth->rsa_priv_dec; 202 } 203 204 int 205 (*RSA_meth_get_mod_exp(const RSA_METHOD *meth))(BIGNUM *r0, const BIGNUM *i, 206 RSA *rsa, BN_CTX *ctx) 207 { 208 return meth->rsa_mod_exp; 209 } 210 211 int 212 (*RSA_meth_get_bn_mod_exp(const RSA_METHOD *meth))(BIGNUM *r, 213 const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, 214 BN_MONT_CTX *m_ctx) 215 { 216 return meth->bn_mod_exp; 217 } 218 219 int 220 (*RSA_meth_get_init(const RSA_METHOD *meth))(RSA *rsa) 221 { 222 return meth->init; 223 } 224 225 int 226 (*RSA_meth_get_keygen(const RSA_METHOD *meth))(RSA *rsa, int bits, BIGNUM *e, 227 BN_GENCB *cb) 228 { 229 return meth->rsa_keygen; 230 } 231 232 int 233 RSA_meth_get_flags(const RSA_METHOD *meth) 234 { 235 return meth->flags; 236 } 237 238 void * 239 RSA_meth_get0_app_data(const RSA_METHOD *meth) 240 { 241 return meth->app_data; 242 } 243 244 int 245 (*RSA_meth_get_sign(const RSA_METHOD *meth))(int type, 246 const unsigned char *m, unsigned int m_length, 247 unsigned char *sigret, unsigned int *siglen, 248 const RSA *rsa) 249 { 250 return meth->rsa_sign; 251 } 252 253 int 254 RSA_meth_set_sign(RSA_METHOD *meth, int (*sign)(int type, 255 const unsigned char *m, unsigned int m_length, unsigned char *sigret, 256 unsigned int *siglen, const RSA *rsa)) 257 { 258 meth->rsa_sign = sign; 259 return 1; 260 } 261 262 int 263 (*RSA_meth_get_verify(const RSA_METHOD *meth))(int dtype, 264 const unsigned char *m, unsigned int m_length, const unsigned char *sigbuf, 265 unsigned int siglen, const RSA *rsa) 266 { 267 return meth->rsa_verify; 268 } 269 270 int 271 RSA_meth_set_verify(RSA_METHOD *meth, int (*verify)(int dtype, 272 const unsigned char *m, unsigned int m_length, const unsigned char *sigbuf, 273 unsigned int siglen, const RSA *rsa)) 274 { 275 meth->rsa_verify = verify; 276 return 1; 277 } 278