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