1 /* $OpenBSD: dsa_lib.c,v 1.29 2018/04/14 07:09:21 tb Exp $ */
2 /* $OpenBSD: rsa_lib.c,v 1.37 2018/04/14 07:09:21 tb Exp $ */
3 /* $OpenBSD: evp_lib.c,v 1.17 2018/09/12 06:35:38 djm Exp $ */
4 /* $OpenBSD: dh_lib.c,v 1.32 2018/05/02 15:48:38 tb Exp $ */
5 /* $OpenBSD: p_lib.c,v 1.24 2018/05/30 15:40:50 tb Exp $ */
6 /* $OpenBSD: digest.c,v 1.30 2018/04/14 07:09:21 tb Exp $ */
7 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
8  * All rights reserved.
9  *
10  * This package is an SSL implementation written
11  * by Eric Young (eay@cryptsoft.com).
12  * The implementation was written so as to conform with Netscapes SSL.
13  *
14  * This library is free for commercial and non-commercial use as long as
15  * the following conditions are aheared to.  The following conditions
16  * apply to all code found in this distribution, be it the RC4, RSA,
17  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
18  * included with this distribution is covered by the same copyright terms
19  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
20  *
21  * Copyright remains Eric Young's, and as such any Copyright notices in
22  * the code are not to be removed.
23  * If this package is used in a product, Eric Young should be given attribution
24  * as the author of the parts of the library used.
25  * This can be in the form of a textual message at program startup or
26  * in documentation (online or textual) provided with the package.
27  *
28  * Redistribution and use in source and binary forms, with or without
29  * modification, are permitted provided that the following conditions
30  * are met:
31  * 1. Redistributions of source code must retain the copyright
32  *    notice, this list of conditions and the following disclaimer.
33  * 2. Redistributions in binary form must reproduce the above copyright
34  *    notice, this list of conditions and the following disclaimer in the
35  *    documentation and/or other materials provided with the distribution.
36  * 3. All advertising materials mentioning features or use of this software
37  *    must display the following acknowledgement:
38  *    "This product includes cryptographic software written by
39  *     Eric Young (eay@cryptsoft.com)"
40  *    The word 'cryptographic' can be left out if the rouines from the library
41  *    being used are not cryptographic related :-).
42  * 4. If you include any Windows specific code (or a derivative thereof) from
43  *    the apps directory (application code) you must include an acknowledgement:
44  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
45  *
46  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  *
58  * The licence and distribution terms for any publically available version or
59  * derivative of this code cannot be changed.  i.e. this code cannot simply be
60  * copied and put under another distribution licence
61  * [including the GNU Public Licence.]
62  */
63 
64 /* $OpenBSD: dsa_asn1.c,v 1.22 2018/06/14 17:03:19 jsing Exp $ */
65 /* $OpenBSD: ecs_asn1.c,v 1.9 2018/03/17 15:24:44 tb Exp $ */
66 /* $OpenBSD: digest.c,v 1.30 2018/04/14 07:09:21 tb Exp $ */
67 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
68  * project 2000.
69  */
70 /* ====================================================================
71  * Copyright (c) 2000-2005 The OpenSSL Project.  All rights reserved.
72  *
73  * Redistribution and use in source and binary forms, with or without
74  * modification, are permitted provided that the following conditions
75  * are met:
76  *
77  * 1. Redistributions of source code must retain the above copyright
78  *    notice, this list of conditions and the following disclaimer.
79  *
80  * 2. Redistributions in binary form must reproduce the above copyright
81  *    notice, this list of conditions and the following disclaimer in
82  *    the documentation and/or other materials provided with the
83  *    distribution.
84  *
85  * 3. All advertising materials mentioning features or use of this
86  *    software must display the following acknowledgment:
87  *    "This product includes software developed by the OpenSSL Project
88  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
89  *
90  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
91  *    endorse or promote products derived from this software without
92  *    prior written permission. For written permission, please contact
93  *    licensing@OpenSSL.org.
94  *
95  * 5. Products derived from this software may not be called "OpenSSL"
96  *    nor may "OpenSSL" appear in their names without prior written
97  *    permission of the OpenSSL Project.
98  *
99  * 6. Redistributions of any form whatsoever must retain the following
100  *    acknowledgment:
101  *    "This product includes software developed by the OpenSSL Project
102  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
103  *
104  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
105  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
106  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
107  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
108  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
109  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
110  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
111  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
112  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
113  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
114  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
115  * OF THE POSSIBILITY OF SUCH DAMAGE.
116  * ====================================================================
117  *
118  * This product includes cryptographic software written by Eric Young
119  * (eay@cryptsoft.com).  This product includes software written by Tim
120  * Hudson (tjh@cryptsoft.com).
121  *
122  */
123 
124 /*	$OpenBSD: rsa_meth.c,v 1.2 2018/09/12 06:35:38 djm Exp $	*/
125 /*
126  * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
127  *
128  * Permission to use, copy, modify, and distribute this software for any
129  * purpose with or without fee is hereby granted, provided that the above
130  * copyright notice and this permission notice appear in all copies.
131  *
132  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
133  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
134  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
135  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
136  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
137  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
138  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
139  */
140 
141 #include "includes.h"
142 
143 #ifdef WITH_OPENSSL
144 
145 #include <sys/types.h>
146 
147 #include <stdlib.h>
148 #include <string.h>
149 
150 #include <openssl/err.h>
151 #include <openssl/bn.h>
152 #include <openssl/dsa.h>
153 #include <openssl/rsa.h>
154 #include <openssl/evp.h>
155 #ifdef OPENSSL_HAS_ECC
156 #include <openssl/ecdsa.h>
157 #endif
158 #include <openssl/dh.h>
159 
160 #ifndef HAVE_DSA_GET0_PQG
161 void
DSA_get0_pqg(const DSA * d,const BIGNUM ** p,const BIGNUM ** q,const BIGNUM ** g)162 DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
163 {
164 	if (p != NULL)
165 		*p = d->p;
166 	if (q != NULL)
167 		*q = d->q;
168 	if (g != NULL)
169 		*g = d->g;
170 }
171 #endif /* HAVE_DSA_GET0_PQG */
172 
173 #ifndef HAVE_DSA_SET0_PQG
174 int
DSA_set0_pqg(DSA * d,BIGNUM * p,BIGNUM * q,BIGNUM * g)175 DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
176 {
177 	if ((d->p == NULL && p == NULL) || (d->q == NULL && q == NULL) ||
178 	    (d->g == NULL && g == NULL))
179 		return 0;
180 
181 	if (p != NULL) {
182 		BN_free(d->p);
183 		d->p = p;
184 	}
185 	if (q != NULL) {
186 		BN_free(d->q);
187 		d->q = q;
188 	}
189 	if (g != NULL) {
190 		BN_free(d->g);
191 		d->g = g;
192 	}
193 
194 	return 1;
195 }
196 #endif /* HAVE_DSA_SET0_PQG */
197 
198 #ifndef HAVE_DSA_GET0_KEY
199 void
DSA_get0_key(const DSA * d,const BIGNUM ** pub_key,const BIGNUM ** priv_key)200 DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
201 {
202 	if (pub_key != NULL)
203 		*pub_key = d->pub_key;
204 	if (priv_key != NULL)
205 		*priv_key = d->priv_key;
206 }
207 #endif /* HAVE_DSA_GET0_KEY */
208 
209 #ifndef HAVE_DSA_SET0_KEY
210 int
DSA_set0_key(DSA * d,BIGNUM * pub_key,BIGNUM * priv_key)211 DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
212 {
213 	if (d->pub_key == NULL && pub_key == NULL)
214 		return 0;
215 
216 	if (pub_key != NULL) {
217 		BN_free(d->pub_key);
218 		d->pub_key = pub_key;
219 	}
220 	if (priv_key != NULL) {
221 		BN_free(d->priv_key);
222 		d->priv_key = priv_key;
223 	}
224 
225 	return 1;
226 }
227 #endif /* HAVE_DSA_SET0_KEY */
228 
229 #ifndef HAVE_RSA_GET0_KEY
230 void
RSA_get0_key(const RSA * r,const BIGNUM ** n,const BIGNUM ** e,const BIGNUM ** d)231 RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
232 {
233 	if (n != NULL)
234 		*n = r->n;
235 	if (e != NULL)
236 		*e = r->e;
237 	if (d != NULL)
238 		*d = r->d;
239 }
240 #endif /* HAVE_RSA_GET0_KEY */
241 
242 #ifndef HAVE_RSA_SET0_KEY
243 int
RSA_set0_key(RSA * r,BIGNUM * n,BIGNUM * e,BIGNUM * d)244 RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
245 {
246 	if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
247 		return 0;
248 
249 	if (n != NULL) {
250 		BN_free(r->n);
251 		r->n = n;
252 	}
253 	if (e != NULL) {
254 		BN_free(r->e);
255 		r->e = e;
256 	}
257 	if (d != NULL) {
258 		BN_free(r->d);
259 		r->d = d;
260 	}
261 
262 	return 1;
263 }
264 #endif /* HAVE_RSA_SET0_KEY */
265 
266 #ifndef HAVE_RSA_GET0_CRT_PARAMS
267 void
RSA_get0_crt_params(const RSA * r,const BIGNUM ** dmp1,const BIGNUM ** dmq1,const BIGNUM ** iqmp)268 RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
269     const BIGNUM **iqmp)
270 {
271 	if (dmp1 != NULL)
272 		*dmp1 = r->dmp1;
273 	if (dmq1 != NULL)
274 		*dmq1 = r->dmq1;
275 	if (iqmp != NULL)
276 		*iqmp = r->iqmp;
277 }
278 #endif /* HAVE_RSA_GET0_CRT_PARAMS */
279 
280 #ifndef HAVE_RSA_SET0_CRT_PARAMS
281 int
RSA_set0_crt_params(RSA * r,BIGNUM * dmp1,BIGNUM * dmq1,BIGNUM * iqmp)282 RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
283 {
284 	if ((r->dmp1 == NULL && dmp1 == NULL) ||
285 	    (r->dmq1 == NULL && dmq1 == NULL) ||
286 	    (r->iqmp == NULL && iqmp == NULL))
287 	       	return 0;
288 
289 	if (dmp1 != NULL) {
290 		BN_free(r->dmp1);
291 		r->dmp1 = dmp1;
292 	}
293 	if (dmq1 != NULL) {
294 		BN_free(r->dmq1);
295 		r->dmq1 = dmq1;
296 	}
297 	if (iqmp != NULL) {
298 		BN_free(r->iqmp);
299 		r->iqmp = iqmp;
300 	}
301 
302 	return 1;
303 }
304 #endif /* HAVE_RSA_SET0_CRT_PARAMS */
305 
306 #ifndef HAVE_RSA_GET0_FACTORS
307 void
RSA_get0_factors(const RSA * r,const BIGNUM ** p,const BIGNUM ** q)308 RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
309 {
310 	if (p != NULL)
311 		*p = r->p;
312 	if (q != NULL)
313 		*q = r->q;
314 }
315 #endif /* HAVE_RSA_GET0_FACTORS */
316 
317 #ifndef HAVE_RSA_SET0_FACTORS
318 int
RSA_set0_factors(RSA * r,BIGNUM * p,BIGNUM * q)319 RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
320 {
321 	if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL))
322 		return 0;
323 
324 	if (p != NULL) {
325 		BN_free(r->p);
326 		r->p = p;
327 	}
328 	if (q != NULL) {
329 		BN_free(r->q);
330 		r->q = q;
331 	}
332 
333 	return 1;
334 }
335 #endif /* HAVE_RSA_SET0_FACTORS */
336 
337 #ifndef HAVE_EVP_CIPHER_CTX_GET_IV
338 int
EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX * ctx,unsigned char * iv,size_t len)339 EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx, unsigned char *iv, size_t len)
340 {
341 	if (ctx == NULL)
342 		return 0;
343 	if (EVP_CIPHER_CTX_iv_length(ctx) < 0)
344 		return 0;
345 	if (len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
346 		return 0;
347 	if (len > EVP_MAX_IV_LENGTH)
348 		return 0; /* sanity check; shouldn't happen */
349 	/*
350 	 * Skip the memcpy entirely when the requested IV length is zero,
351 	 * since the iv pointer may be NULL or invalid.
352 	 */
353 	if (len != 0) {
354 		if (iv == NULL)
355 			return 0;
356 # ifdef HAVE_EVP_CIPHER_CTX_IV
357 		memcpy(iv, EVP_CIPHER_CTX_iv(ctx), len);
358 # else
359 		memcpy(iv, ctx->iv, len);
360 # endif /* HAVE_EVP_CIPHER_CTX_IV */
361 	}
362 	return 1;
363 }
364 #endif /* HAVE_EVP_CIPHER_CTX_GET_IV */
365 
366 #ifndef HAVE_EVP_CIPHER_CTX_SET_IV
367 int
EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX * ctx,const unsigned char * iv,size_t len)368 EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX *ctx, const unsigned char *iv, size_t len)
369 {
370 	if (ctx == NULL)
371 		return 0;
372 	if (EVP_CIPHER_CTX_iv_length(ctx) < 0)
373 		return 0;
374 	if (len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
375 		return 0;
376 	if (len > EVP_MAX_IV_LENGTH)
377 		return 0; /* sanity check; shouldn't happen */
378 	/*
379 	 * Skip the memcpy entirely when the requested IV length is zero,
380 	 * since the iv pointer may be NULL or invalid.
381 	 */
382 	if (len != 0) {
383 		if (iv == NULL)
384 			return 0;
385 # ifdef HAVE_EVP_CIPHER_CTX_IV_NOCONST
386 		memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, len);
387 # else
388 		memcpy(ctx->iv, iv, len);
389 # endif /* HAVE_EVP_CIPHER_CTX_IV_NOCONST */
390 	}
391 	return 1;
392 }
393 #endif /* HAVE_EVP_CIPHER_CTX_SET_IV */
394 
395 #ifndef HAVE_DSA_SIG_GET0
396 void
DSA_SIG_get0(const DSA_SIG * sig,const BIGNUM ** pr,const BIGNUM ** ps)397 DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
398 {
399 	if (pr != NULL)
400 		*pr = sig->r;
401 	if (ps != NULL)
402 		*ps = sig->s;
403 }
404 #endif /* HAVE_DSA_SIG_GET0 */
405 
406 #ifndef HAVE_DSA_SIG_SET0
407 int
DSA_SIG_set0(DSA_SIG * sig,BIGNUM * r,BIGNUM * s)408 DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
409 {
410 	if (r == NULL || s == NULL)
411 		return 0;
412 
413 	BN_clear_free(sig->r);
414 	sig->r = r;
415 	BN_clear_free(sig->s);
416 	sig->s = s;
417 
418 	return 1;
419 }
420 #endif /* HAVE_DSA_SIG_SET0 */
421 
422 #ifdef OPENSSL_HAS_ECC
423 #ifndef HAVE_ECDSA_SIG_GET0
424 void
ECDSA_SIG_get0(const ECDSA_SIG * sig,const BIGNUM ** pr,const BIGNUM ** ps)425 ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
426 {
427 	if (pr != NULL)
428 		*pr = sig->r;
429 	if (ps != NULL)
430 		*ps = sig->s;
431 }
432 #endif /* HAVE_ECDSA_SIG_GET0 */
433 
434 #ifndef HAVE_ECDSA_SIG_SET0
435 int
ECDSA_SIG_set0(ECDSA_SIG * sig,BIGNUM * r,BIGNUM * s)436 ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
437 {
438 	if (r == NULL || s == NULL)
439 		return 0;
440 
441 	BN_clear_free(sig->r);
442 	BN_clear_free(sig->s);
443 	sig->r = r;
444 	sig->s = s;
445 	return 1;
446 }
447 #endif /* HAVE_ECDSA_SIG_SET0 */
448 #endif /* OPENSSL_HAS_ECC */
449 
450 #ifndef HAVE_DH_GET0_PQG
451 void
DH_get0_pqg(const DH * dh,const BIGNUM ** p,const BIGNUM ** q,const BIGNUM ** g)452 DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
453 {
454 	if (p != NULL)
455 		*p = dh->p;
456 	if (q != NULL)
457 		*q = dh->q;
458 	if (g != NULL)
459 		*g = dh->g;
460 }
461 #endif /* HAVE_DH_GET0_PQG */
462 
463 #ifndef HAVE_DH_SET0_PQG
464 int
DH_set0_pqg(DH * dh,BIGNUM * p,BIGNUM * q,BIGNUM * g)465 DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
466 {
467 	if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL))
468 		return 0;
469 
470 	if (p != NULL) {
471 		BN_free(dh->p);
472 		dh->p = p;
473 	}
474 	if (q != NULL) {
475 		BN_free(dh->q);
476 		dh->q = q;
477 	}
478 	if (g != NULL) {
479 		BN_free(dh->g);
480 		dh->g = g;
481 	}
482 
483 	return 1;
484 }
485 #endif /* HAVE_DH_SET0_PQG */
486 
487 #ifndef HAVE_DH_GET0_KEY
488 void
DH_get0_key(const DH * dh,const BIGNUM ** pub_key,const BIGNUM ** priv_key)489 DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
490 {
491 	if (pub_key != NULL)
492 		*pub_key = dh->pub_key;
493 	if (priv_key != NULL)
494 		*priv_key = dh->priv_key;
495 }
496 #endif /* HAVE_DH_GET0_KEY */
497 
498 #ifndef HAVE_DH_SET0_KEY
499 int
DH_set0_key(DH * dh,BIGNUM * pub_key,BIGNUM * priv_key)500 DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
501 {
502 	if (pub_key != NULL) {
503 		BN_free(dh->pub_key);
504 		dh->pub_key = pub_key;
505 	}
506 	if (priv_key != NULL) {
507 		BN_free(dh->priv_key);
508 		dh->priv_key = priv_key;
509 	}
510 
511 	return 1;
512 }
513 #endif /* HAVE_DH_SET0_KEY */
514 
515 #ifndef HAVE_DH_SET_LENGTH
516 int
DH_set_length(DH * dh,long length)517 DH_set_length(DH *dh, long length)
518 {
519 	if (length < 0 || length > INT_MAX)
520 		return 0;
521 
522 #if !defined(OPENSSL_IS_BORINGSSL)
523 	dh->length = length;
524 #endif
525 	return 1;
526 }
527 #endif /* HAVE_DH_SET_LENGTH */
528 
529 #ifndef HAVE_RSA_METH_FREE
530 void
RSA_meth_free(RSA_METHOD * meth)531 RSA_meth_free(RSA_METHOD *meth)
532 {
533 	if (meth != NULL) {
534 		free((char *)meth->name);
535 		free(meth);
536 	}
537 }
538 #endif /* HAVE_RSA_METH_FREE */
539 
540 #ifndef HAVE_RSA_METH_DUP
541 RSA_METHOD *
RSA_meth_dup(const RSA_METHOD * meth)542 RSA_meth_dup(const RSA_METHOD *meth)
543 {
544 	RSA_METHOD *copy;
545 
546 	if ((copy = calloc(1, sizeof(*copy))) == NULL)
547 		return NULL;
548 	memcpy(copy, meth, sizeof(*copy));
549 	if ((copy->name = strdup(meth->name)) == NULL) {
550 		free(copy);
551 		return NULL;
552 	}
553 
554 	return copy;
555 }
556 #endif /* HAVE_RSA_METH_DUP */
557 
558 #ifndef HAVE_RSA_METH_SET1_NAME
559 int
RSA_meth_set1_name(RSA_METHOD * meth,const char * name)560 RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
561 {
562 	char *copy;
563 
564 	if ((copy = strdup(name)) == NULL)
565 		return 0;
566 	free((char *)meth->name);
567 	meth->name = copy;
568 	return 1;
569 }
570 #endif /* HAVE_RSA_METH_SET1_NAME */
571 
572 #ifndef HAVE_RSA_METH_GET_FINISH
573 int
RSA_meth_get_finish(const RSA_METHOD * meth)574 (*RSA_meth_get_finish(const RSA_METHOD *meth))(RSA *rsa)
575 {
576 	return meth->finish;
577 }
578 #endif /* HAVE_RSA_METH_GET_FINISH */
579 
580 #ifndef HAVE_RSA_METH_SET_PRIV_ENC
581 int
RSA_meth_set_priv_enc(RSA_METHOD * meth,int (* priv_enc)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))582 RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc)(int flen,
583     const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
584 {
585 	meth->rsa_priv_enc = priv_enc;
586 	return 1;
587 }
588 #endif /* HAVE_RSA_METH_SET_PRIV_ENC */
589 
590 #ifndef HAVE_RSA_METH_SET_PRIV_DEC
591 int
RSA_meth_set_priv_dec(RSA_METHOD * meth,int (* priv_dec)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))592 RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec)(int flen,
593     const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
594 {
595 	meth->rsa_priv_dec = priv_dec;
596 	return 1;
597 }
598 #endif /* HAVE_RSA_METH_SET_PRIV_DEC */
599 
600 #ifndef HAVE_RSA_METH_SET_FINISH
601 int
RSA_meth_set_finish(RSA_METHOD * meth,int (* finish)(RSA * rsa))602 RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa))
603 {
604 	meth->finish = finish;
605 	return 1;
606 }
607 #endif /* HAVE_RSA_METH_SET_FINISH */
608 
609 #ifndef HAVE_EVP_PKEY_GET0_RSA
610 RSA *
EVP_PKEY_get0_RSA(EVP_PKEY * pkey)611 EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
612 {
613 	if (pkey->type != EVP_PKEY_RSA) {
614 		/* EVPerror(EVP_R_EXPECTING_AN_RSA_KEY); */
615 		return NULL;
616 	}
617 	return pkey->pkey.rsa;
618 }
619 #endif /* HAVE_EVP_PKEY_GET0_RSA */
620 
621 #ifndef HAVE_EVP_MD_CTX_NEW
622 EVP_MD_CTX *
EVP_MD_CTX_new(void)623 EVP_MD_CTX_new(void)
624 {
625 	return calloc(1, sizeof(EVP_MD_CTX));
626 }
627 #endif /* HAVE_EVP_MD_CTX_NEW */
628 
629 #ifndef HAVE_EVP_MD_CTX_FREE
630 void
EVP_MD_CTX_free(EVP_MD_CTX * ctx)631 EVP_MD_CTX_free(EVP_MD_CTX *ctx)
632 {
633 	if (ctx == NULL)
634 		return;
635 
636 	EVP_MD_CTX_cleanup(ctx);
637 
638 	free(ctx);
639 }
640 #endif /* HAVE_EVP_MD_CTX_FREE */
641 
642 #endif /* WITH_OPENSSL */
643