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