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 #include <libcrypto-compat.h>
12
13
14 #if OPENSSL_VERSION_NUMBER < 0x10100000L
15
16 #include <string.h>
17 #include <openssl/engine.h>
18 #include <openssl/bn.h> /* BN_* */
19
20
OPENSSL_zalloc(size_t num)21 static void *OPENSSL_zalloc(size_t num)
22 {
23 void *ret = OPENSSL_malloc(num);
24
25 if (ret != NULL)
26 memset(ret, 0, num);
27 return ret;
28 }
29
RSA_set0_key(RSA * r,BIGNUM * n,BIGNUM * e,BIGNUM * d)30 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
31 {
32 /* If the fields n and e in r are NULL, the corresponding input
33 * parameters MUST be non-NULL for n and e. d may be
34 * left NULL (in case only the public key is used).
35 */
36 if ((r->n == NULL && n == NULL)
37 || (r->e == NULL && e == NULL))
38 return 0;
39
40 if (n != NULL) {
41 BN_free(r->n);
42 r->n = n;
43 }
44 if (e != NULL) {
45 BN_free(r->e);
46 r->e = e;
47 }
48 if (d != NULL) {
49 BN_free(r->d);
50 r->d = d;
51 }
52
53 return 1;
54 }
55
RSA_set0_factors(RSA * r,BIGNUM * p,BIGNUM * q)56 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
57 {
58 /* If the fields p and q in r are NULL, the corresponding input
59 * parameters MUST be non-NULL.
60 */
61 if ((r->p == NULL && p == NULL)
62 || (r->q == NULL && q == NULL))
63 return 0;
64
65 if (p != NULL) {
66 BN_free(r->p);
67 r->p = p;
68 }
69 if (q != NULL) {
70 BN_free(r->q);
71 r->q = q;
72 }
73
74 return 1;
75 }
76
RSA_set0_crt_params(RSA * r,BIGNUM * dmp1,BIGNUM * dmq1,BIGNUM * iqmp)77 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
78 {
79 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
80 * parameters MUST be non-NULL.
81 */
82 if ((r->dmp1 == NULL && dmp1 == NULL)
83 || (r->dmq1 == NULL && dmq1 == NULL)
84 || (r->iqmp == NULL && iqmp == NULL))
85 return 0;
86
87 if (dmp1 != NULL) {
88 BN_free(r->dmp1);
89 r->dmp1 = dmp1;
90 }
91 if (dmq1 != NULL) {
92 BN_free(r->dmq1);
93 r->dmq1 = dmq1;
94 }
95 if (iqmp != NULL) {
96 BN_free(r->iqmp);
97 r->iqmp = iqmp;
98 }
99
100 return 1;
101 }
102
RSA_get0_key(const RSA * r,const BIGNUM ** n,const BIGNUM ** e,const BIGNUM ** d)103 void RSA_get0_key(const RSA *r,
104 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
105 {
106 if (n != NULL)
107 *n = r->n;
108 if (e != NULL)
109 *e = r->e;
110 if (d != NULL)
111 *d = r->d;
112 }
113
RSA_get0_factors(const RSA * r,const BIGNUM ** p,const BIGNUM ** q)114 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
115 {
116 if (p != NULL)
117 *p = r->p;
118 if (q != NULL)
119 *q = r->q;
120 }
121
RSA_get0_crt_params(const RSA * r,const BIGNUM ** dmp1,const BIGNUM ** dmq1,const BIGNUM ** iqmp)122 void RSA_get0_crt_params(const RSA *r,
123 const BIGNUM **dmp1, const BIGNUM **dmq1,
124 const BIGNUM **iqmp)
125 {
126 if (dmp1 != NULL)
127 *dmp1 = r->dmp1;
128 if (dmq1 != NULL)
129 *dmq1 = r->dmq1;
130 if (iqmp != NULL)
131 *iqmp = r->iqmp;
132 }
133
DSA_get0_pqg(const DSA * d,const BIGNUM ** p,const BIGNUM ** q,const BIGNUM ** g)134 void DSA_get0_pqg(const DSA *d,
135 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
136 {
137 if (p != NULL)
138 *p = d->p;
139 if (q != NULL)
140 *q = d->q;
141 if (g != NULL)
142 *g = d->g;
143 }
144
DSA_set0_pqg(DSA * d,BIGNUM * p,BIGNUM * q,BIGNUM * g)145 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
146 {
147 /* If the fields p, q and g in d are NULL, the corresponding input
148 * parameters MUST be non-NULL.
149 */
150 if ((d->p == NULL && p == NULL)
151 || (d->q == NULL && q == NULL)
152 || (d->g == NULL && g == NULL))
153 return 0;
154
155 if (p != NULL) {
156 BN_free(d->p);
157 d->p = p;
158 }
159 if (q != NULL) {
160 BN_free(d->q);
161 d->q = q;
162 }
163 if (g != NULL) {
164 BN_free(d->g);
165 d->g = g;
166 }
167
168 return 1;
169 }
170
DSA_get0_key(const DSA * d,const BIGNUM ** pub_key,const BIGNUM ** priv_key)171 void DSA_get0_key(const DSA *d,
172 const BIGNUM **pub_key, const BIGNUM **priv_key)
173 {
174 if (pub_key != NULL)
175 *pub_key = d->pub_key;
176 if (priv_key != NULL)
177 *priv_key = d->priv_key;
178 }
179
DSA_set0_key(DSA * d,BIGNUM * pub_key,BIGNUM * priv_key)180 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
181 {
182 /* If the field pub_key in d is NULL, the corresponding input
183 * parameters MUST be non-NULL. The priv_key field may
184 * be left NULL.
185 */
186 if (d->pub_key == NULL && pub_key == NULL)
187 return 0;
188
189 if (pub_key != NULL) {
190 BN_free(d->pub_key);
191 d->pub_key = pub_key;
192 }
193 if (priv_key != NULL) {
194 BN_free(d->priv_key);
195 d->priv_key = priv_key;
196 }
197
198 return 1;
199 }
200
DSA_SIG_get0(const DSA_SIG * sig,const BIGNUM ** pr,const BIGNUM ** ps)201 void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
202 {
203 if (pr != NULL)
204 *pr = sig->r;
205 if (ps != NULL)
206 *ps = sig->s;
207 }
208
DSA_SIG_set0(DSA_SIG * sig,BIGNUM * r,BIGNUM * s)209 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
210 {
211 if (r == NULL || s == NULL)
212 return 0;
213 BN_clear_free(sig->r);
214 BN_clear_free(sig->s);
215 sig->r = r;
216 sig->s = s;
217 return 1;
218 }
219
ECDSA_SIG_get0(const ECDSA_SIG * sig,const BIGNUM ** pr,const BIGNUM ** ps)220 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
221 {
222 if (pr != NULL)
223 *pr = sig->r;
224 if (ps != NULL)
225 *ps = sig->s;
226 }
227
ECDSA_SIG_set0(ECDSA_SIG * sig,BIGNUM * r,BIGNUM * s)228 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
229 {
230 if (r == NULL || s == NULL)
231 return 0;
232 BN_clear_free(sig->r);
233 BN_clear_free(sig->s);
234 sig->r = r;
235 sig->s = s;
236 return 1;
237 }
238
DH_get0_pqg(const DH * dh,const BIGNUM ** p,const BIGNUM ** q,const BIGNUM ** g)239 void DH_get0_pqg(const DH *dh,
240 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
241 {
242 if (p != NULL)
243 *p = dh->p;
244 if (q != NULL)
245 *q = dh->q;
246 if (g != NULL)
247 *g = dh->g;
248 }
249
DH_set0_pqg(DH * dh,BIGNUM * p,BIGNUM * q,BIGNUM * g)250 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
251 {
252 /* If the fields p and g in d are NULL, the corresponding input
253 * parameters MUST be non-NULL. q may remain NULL.
254 */
255 if ((dh->p == NULL && p == NULL)
256 || (dh->g == NULL && g == NULL))
257 return 0;
258
259 if (p != NULL) {
260 BN_free(dh->p);
261 dh->p = p;
262 }
263 if (q != NULL) {
264 BN_free(dh->q);
265 dh->q = q;
266 }
267 if (g != NULL) {
268 BN_free(dh->g);
269 dh->g = g;
270 }
271
272 if (q != NULL) {
273 dh->length = BN_num_bits(q);
274 }
275
276 return 1;
277 }
278
DH_get0_key(const DH * dh,const BIGNUM ** pub_key,const BIGNUM ** priv_key)279 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
280 {
281 if (pub_key != NULL)
282 *pub_key = dh->pub_key;
283 if (priv_key != NULL)
284 *priv_key = dh->priv_key;
285 }
286
DH_set0_key(DH * dh,BIGNUM * pub_key,BIGNUM * priv_key)287 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
288 {
289 /* If the field pub_key in dh is NULL, the corresponding input
290 * parameters MUST be non-NULL. The priv_key field may
291 * be left NULL.
292 */
293 if (dh->pub_key == NULL && pub_key == NULL)
294 return 0;
295
296 if (pub_key != NULL) {
297 BN_free(dh->pub_key);
298 dh->pub_key = pub_key;
299 }
300 if (priv_key != NULL) {
301 BN_free(dh->priv_key);
302 dh->priv_key = priv_key;
303 }
304
305 return 1;
306 }
307
DH_set_length(DH * dh,long length)308 int DH_set_length(DH *dh, long length)
309 {
310 dh->length = length;
311 return 1;
312 }
313
EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX * ctx)314 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
315 {
316 return ctx->iv;
317 }
318
EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX * ctx)319 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
320 {
321 return ctx->iv;
322 }
323
EVP_MD_CTX_new(void)324 EVP_MD_CTX *EVP_MD_CTX_new(void)
325 {
326 return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
327 }
328
EVP_MD_CTX_free(EVP_MD_CTX * ctx)329 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
330 {
331 EVP_MD_CTX_cleanup(ctx);
332 OPENSSL_free(ctx);
333 }
334
RSA_meth_dup(const RSA_METHOD * meth)335 RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth)
336 {
337 RSA_METHOD *ret;
338
339 ret = OPENSSL_malloc(sizeof(RSA_METHOD));
340
341 if (ret != NULL) {
342 memcpy(ret, meth, sizeof(*meth));
343 ret->name = OPENSSL_strdup(meth->name);
344 if (ret->name == NULL) {
345 OPENSSL_free(ret);
346 return NULL;
347 }
348 }
349
350 return ret;
351 }
352
RSA_meth_set1_name(RSA_METHOD * meth,const char * name)353 int RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
354 {
355 char *tmpname;
356
357 tmpname = OPENSSL_strdup(name);
358 if (tmpname == NULL) {
359 return 0;
360 }
361
362 OPENSSL_free((char *)meth->name);
363 meth->name = tmpname;
364
365 return 1;
366 }
367
RSA_meth_set_priv_enc(RSA_METHOD * meth,int (* priv_enc)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))368 int RSA_meth_set_priv_enc(RSA_METHOD *meth,
369 int (*priv_enc) (int flen, const unsigned char *from,
370 unsigned char *to, RSA *rsa,
371 int padding))
372 {
373 meth->rsa_priv_enc = priv_enc;
374 return 1;
375 }
376
RSA_meth_set_priv_dec(RSA_METHOD * meth,int (* priv_dec)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))377 int RSA_meth_set_priv_dec(RSA_METHOD *meth,
378 int (*priv_dec) (int flen, const unsigned char *from,
379 unsigned char *to, RSA *rsa,
380 int padding))
381 {
382 meth->rsa_priv_dec = priv_dec;
383 return 1;
384 }
385
RSA_meth_set_finish(RSA_METHOD * meth,int (* finish)(RSA * rsa))386 int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa))
387 {
388 meth->finish = finish;
389 return 1;
390 }
391
RSA_meth_free(RSA_METHOD * meth)392 void RSA_meth_free(RSA_METHOD *meth)
393 {
394 if (meth != NULL) {
395 OPENSSL_free((char *)meth->name);
396 OPENSSL_free(meth);
397 }
398 }
399
RSA_bits(const RSA * r)400 int RSA_bits(const RSA *r)
401 {
402 return (BN_num_bits(r->n));
403 }
404
EVP_PKEY_get0_RSA(EVP_PKEY * pkey)405 RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
406 {
407 if (pkey->type != EVP_PKEY_RSA) {
408 return NULL;
409 }
410 return pkey->pkey.rsa;
411 }
412
413
414 #else
415
RAND_screen(void)416 void RAND_screen(void)
417 {
418 }
419
420
421 #endif /* OPENSSL_VERSION_NUMBER */
422