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