Better checks for malloc failure in various METHOD functions
[openssl.git] / crypto / dsa / dsa_meth.c
1 /*
2  * Copyright 2016 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 /*
11  * Licensed under the OpenSSL licenses, (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * https://www.openssl.org/source/license.html
15  * or in the file LICENSE in the source distribution.
16  */
17
18 #include "dsa_locl.h"
19 #include <string.h>
20
21 DSA_METHOD *DSA_meth_new(const char *name, int flags)
22 {
23     DSA_METHOD *dsam = OPENSSL_zalloc(sizeof(DSA_METHOD));
24
25     if (dsam != NULL) {
26         dsam->name = OPENSSL_strdup(name);
27         if (dsam->name == NULL) {
28             OPENSSL_free(dsam);
29             return NULL;
30         }
31         dsam->flags = flags;
32     }
33
34     return dsam;
35 }
36
37 void DSA_meth_free(DSA_METHOD *dsam)
38 {
39     if (dsam != NULL) {
40         if (dsam->name != NULL)
41             OPENSSL_free(dsam->name);
42         OPENSSL_free(dsam);
43     }
44 }
45
46 DSA_METHOD *DSA_meth_dup(const DSA_METHOD *dsam)
47 {
48     DSA_METHOD *ret;
49
50     ret = OPENSSL_malloc(sizeof(DSA_METHOD));
51
52     if (ret != NULL) {
53         memcpy(ret, dsam, sizeof(*dsam));
54         ret->name = OPENSSL_strdup(dsam->name);
55         if (ret->name == NULL) {
56             OPENSSL_free(ret);
57             return NULL;
58         }
59     }
60
61     return ret;
62 }
63
64 const char *DSA_meth_get0_name(const DSA_METHOD *dsam)
65 {
66     return dsam->name;
67 }
68
69 int DSA_meth_set1_name(DSA_METHOD *dsam, const char *name)
70 {
71     char *tmpname;
72
73     tmpname = OPENSSL_strdup(name);
74     if (tmpname == NULL)
75         return 0;
76
77     OPENSSL_free(dsam->name);
78     dsam->name = tmpname;
79
80     return 1;
81 }
82
83 int DSA_meth_get_flags(DSA_METHOD *dsam)
84 {
85     return dsam->flags;
86 }
87
88 int DSA_meth_set_flags(DSA_METHOD *dsam, int flags)
89 {
90     dsam->flags = flags;
91     return 1;
92 }
93
94 void *DSA_meth_get0_app_data(const DSA_METHOD *dsam)
95 {
96     return dsam->app_data;
97 }
98
99 int DSA_meth_set0_app_data(DSA_METHOD *dsam, void *app_data)
100 {
101     dsam->app_data = app_data;
102     return 1;
103 }
104
105 DSA_SIG *(*DSA_meth_get_sign(const DSA_METHOD *dsam))
106         (const unsigned char *, int, DSA *)
107 {
108     return dsam->dsa_do_sign;
109 }
110
111 int DSA_meth_set_sign(DSA_METHOD *dsam,
112                        DSA_SIG *(*sign) (const unsigned char *, int, DSA *))
113 {
114     dsam->dsa_do_sign = sign;
115     return 1;
116 }
117
118 int (*DSA_meth_get_sign_setup(const DSA_METHOD *dsam))
119         (DSA *, BN_CTX *, BIGNUM **, BIGNUM **)
120 {
121     return dsam->dsa_sign_setup;
122 }
123
124 int DSA_meth_set_sign_setup(DSA_METHOD *dsam,
125         int (*sign_setup) (DSA *, BN_CTX *, BIGNUM **, BIGNUM **))
126 {
127     dsam->dsa_sign_setup = sign_setup;
128     return 1;
129 }
130
131 int (*DSA_meth_get_verify(const DSA_METHOD *dsam))
132         (const unsigned char *, int , DSA_SIG *, DSA *)
133 {
134     return dsam->dsa_do_verify;
135 }
136
137 int DSA_meth_set_verify(DSA_METHOD *dsam,
138     int (*verify) (const unsigned char *, int, DSA_SIG *, DSA *))
139 {
140     dsam->dsa_do_verify = verify;
141     return 1;
142 }
143
144 int (*DSA_meth_get_mod_exp(const DSA_METHOD *dsam))
145         (DSA *, BIGNUM *, BIGNUM *, BIGNUM *, BIGNUM *, BIGNUM *, BIGNUM *,
146          BN_CTX *, BN_MONT_CTX *)
147 {
148     return dsam->dsa_mod_exp;
149 }
150
151 int DSA_meth_set_mod_exp(DSA_METHOD *dsam,
152     int (*mod_exp) (DSA *, BIGNUM *, BIGNUM *, BIGNUM *, BIGNUM *, BIGNUM *,
153                     BIGNUM *, BN_CTX *, BN_MONT_CTX *))
154 {
155     dsam->dsa_mod_exp = mod_exp;
156     return 1;
157 }
158
159 int (*DSA_meth_get_bn_mod_exp(const DSA_METHOD *dsam))
160     (DSA *, BIGNUM *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *,
161      BN_MONT_CTX *)
162 {
163     return dsam->bn_mod_exp;
164 }
165
166 int DSA_meth_set_bn_mod_exp(DSA_METHOD *dsam,
167     int (*bn_mod_exp) (DSA *, BIGNUM *, BIGNUM *, const BIGNUM *,
168                        const BIGNUM *, BN_CTX *, BN_MONT_CTX *))
169 {
170     dsam->bn_mod_exp = bn_mod_exp;
171     return 1;
172 }
173
174 int (*DSA_meth_get_init(const DSA_METHOD *dsam))(DSA *)
175 {
176     return dsam->init;
177 }
178
179 int DSA_meth_set_init(DSA_METHOD *dsam, int (*init)(DSA *))
180 {
181     dsam->init = init;
182     return 1;
183 }
184
185 int (*DSA_meth_get_finish(const DSA_METHOD *dsam)) (DSA *)
186 {
187     return dsam->finish;
188 }
189
190 int DSA_meth_set_finish(DSA_METHOD *dsam, int (*finish) (DSA *))
191 {
192     dsam->finish = finish;
193     return 1;
194 }
195
196 int (*DSA_meth_get_paramgen(const DSA_METHOD *dsam))
197         (DSA *, int, const unsigned char *, int, int *, unsigned long *,
198          BN_GENCB *)
199 {
200     return dsam->dsa_paramgen;
201 }
202
203 int DSA_meth_set_paramgen(DSA_METHOD *dsam,
204         int (*paramgen) (DSA *, int, const unsigned char *, int, int *,
205                          unsigned long *, BN_GENCB *))
206 {
207     dsam->dsa_paramgen = paramgen;
208     return 1;
209 }
210
211 int (*DSA_meth_get_keygen(const DSA_METHOD *dsam)) (DSA *)
212 {
213     return dsam->dsa_keygen;
214 }
215
216 int DSA_meth_set_keygen(DSA_METHOD *dsam, int (*keygen) (DSA *))
217 {
218     dsam->dsa_keygen = keygen;
219     return 1;
220 }