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