Coverage Report

Created: 2025-04-22 06:18

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