xref: /onnv-gate/usr/src/common/openssl/crypto/asn1/t_pkey.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/asn1/t_pkey.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include "cryptlib.h"
61*0Sstevel@tonic-gate #include <openssl/buffer.h>
62*0Sstevel@tonic-gate #include <openssl/bn.h>
63*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
64*0Sstevel@tonic-gate #include <openssl/rsa.h>
65*0Sstevel@tonic-gate #endif
66*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
67*0Sstevel@tonic-gate #include <openssl/dh.h>
68*0Sstevel@tonic-gate #endif
69*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
70*0Sstevel@tonic-gate #include <openssl/dsa.h>
71*0Sstevel@tonic-gate #endif
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate static int print(BIO *fp,const char *str,BIGNUM *num,
74*0Sstevel@tonic-gate 		unsigned char *buf,int off);
75*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA
76*0Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
77*0Sstevel@tonic-gate int RSA_print_fp(FILE *fp, const RSA *x, int off)
78*0Sstevel@tonic-gate         {
79*0Sstevel@tonic-gate         BIO *b;
80*0Sstevel@tonic-gate         int ret;
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate         if ((b=BIO_new(BIO_s_file())) == NULL)
83*0Sstevel@tonic-gate 		{
84*0Sstevel@tonic-gate 		RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB);
85*0Sstevel@tonic-gate                 return(0);
86*0Sstevel@tonic-gate 		}
87*0Sstevel@tonic-gate         BIO_set_fp(b,fp,BIO_NOCLOSE);
88*0Sstevel@tonic-gate         ret=RSA_print(b,x,off);
89*0Sstevel@tonic-gate         BIO_free(b);
90*0Sstevel@tonic-gate         return(ret);
91*0Sstevel@tonic-gate         }
92*0Sstevel@tonic-gate #endif
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate int RSA_print(BIO *bp, const RSA *x, int off)
95*0Sstevel@tonic-gate 	{
96*0Sstevel@tonic-gate 	char str[128];
97*0Sstevel@tonic-gate 	const char *s;
98*0Sstevel@tonic-gate 	unsigned char *m=NULL;
99*0Sstevel@tonic-gate 	int ret=0;
100*0Sstevel@tonic-gate 	size_t buf_len=0, i;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	if (x->n)
103*0Sstevel@tonic-gate 		buf_len = (size_t)BN_num_bytes(x->n);
104*0Sstevel@tonic-gate 	if (x->e)
105*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->e)))
106*0Sstevel@tonic-gate 			buf_len = i;
107*0Sstevel@tonic-gate 	if (x->d)
108*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->d)))
109*0Sstevel@tonic-gate 			buf_len = i;
110*0Sstevel@tonic-gate 	if (x->p)
111*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->p)))
112*0Sstevel@tonic-gate 			buf_len = i;
113*0Sstevel@tonic-gate 	if (x->q)
114*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
115*0Sstevel@tonic-gate 			buf_len = i;
116*0Sstevel@tonic-gate 	if (x->dmp1)
117*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->dmp1)))
118*0Sstevel@tonic-gate 			buf_len = i;
119*0Sstevel@tonic-gate 	if (x->dmq1)
120*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->dmq1)))
121*0Sstevel@tonic-gate 			buf_len = i;
122*0Sstevel@tonic-gate 	if (x->iqmp)
123*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->iqmp)))
124*0Sstevel@tonic-gate 			buf_len = i;
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	m=(unsigned char *)OPENSSL_malloc(buf_len+10);
127*0Sstevel@tonic-gate 	if (m == NULL)
128*0Sstevel@tonic-gate 		{
129*0Sstevel@tonic-gate 		RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE);
130*0Sstevel@tonic-gate 		goto err;
131*0Sstevel@tonic-gate 		}
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	if (x->d != NULL)
134*0Sstevel@tonic-gate 		{
135*0Sstevel@tonic-gate 		if(!BIO_indent(bp,off,128))
136*0Sstevel@tonic-gate 		   goto err;
137*0Sstevel@tonic-gate 		if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->n))
138*0Sstevel@tonic-gate 			<= 0) goto err;
139*0Sstevel@tonic-gate 		}
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	if (x->d == NULL)
142*0Sstevel@tonic-gate 		BIO_snprintf(str,sizeof str,"Modulus (%d bit):",BN_num_bits(x->n));
143*0Sstevel@tonic-gate 	else
144*0Sstevel@tonic-gate 		BUF_strlcpy(str,"modulus:",sizeof str);
145*0Sstevel@tonic-gate 	if (!print(bp,str,x->n,m,off)) goto err;
146*0Sstevel@tonic-gate 	s=(x->d == NULL)?"Exponent:":"publicExponent:";
147*0Sstevel@tonic-gate 	if (!print(bp,s,x->e,m,off)) goto err;
148*0Sstevel@tonic-gate 	if (!print(bp,"privateExponent:",x->d,m,off)) goto err;
149*0Sstevel@tonic-gate 	if (!print(bp,"prime1:",x->p,m,off)) goto err;
150*0Sstevel@tonic-gate 	if (!print(bp,"prime2:",x->q,m,off)) goto err;
151*0Sstevel@tonic-gate 	if (!print(bp,"exponent1:",x->dmp1,m,off)) goto err;
152*0Sstevel@tonic-gate 	if (!print(bp,"exponent2:",x->dmq1,m,off)) goto err;
153*0Sstevel@tonic-gate 	if (!print(bp,"coefficient:",x->iqmp,m,off)) goto err;
154*0Sstevel@tonic-gate 	ret=1;
155*0Sstevel@tonic-gate err:
156*0Sstevel@tonic-gate 	if (m != NULL) OPENSSL_free(m);
157*0Sstevel@tonic-gate 	return(ret);
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate #endif /* OPENSSL_NO_RSA */
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
162*0Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
163*0Sstevel@tonic-gate int DSA_print_fp(FILE *fp, const DSA *x, int off)
164*0Sstevel@tonic-gate 	{
165*0Sstevel@tonic-gate 	BIO *b;
166*0Sstevel@tonic-gate 	int ret;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	if ((b=BIO_new(BIO_s_file())) == NULL)
169*0Sstevel@tonic-gate 		{
170*0Sstevel@tonic-gate 		DSAerr(DSA_F_DSA_PRINT_FP,ERR_R_BUF_LIB);
171*0Sstevel@tonic-gate 		return(0);
172*0Sstevel@tonic-gate 		}
173*0Sstevel@tonic-gate 	BIO_set_fp(b,fp,BIO_NOCLOSE);
174*0Sstevel@tonic-gate 	ret=DSA_print(b,x,off);
175*0Sstevel@tonic-gate 	BIO_free(b);
176*0Sstevel@tonic-gate 	return(ret);
177*0Sstevel@tonic-gate 	}
178*0Sstevel@tonic-gate #endif
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate int DSA_print(BIO *bp, const DSA *x, int off)
181*0Sstevel@tonic-gate 	{
182*0Sstevel@tonic-gate 	unsigned char *m=NULL;
183*0Sstevel@tonic-gate 	int ret=0;
184*0Sstevel@tonic-gate 	size_t buf_len=0,i;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	if (x->p)
187*0Sstevel@tonic-gate 		buf_len = (size_t)BN_num_bytes(x->p);
188*0Sstevel@tonic-gate 	if (x->q)
189*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
190*0Sstevel@tonic-gate 			buf_len = i;
191*0Sstevel@tonic-gate 	if (x->g)
192*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
193*0Sstevel@tonic-gate 			buf_len = i;
194*0Sstevel@tonic-gate 	if (x->priv_key)
195*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->priv_key)))
196*0Sstevel@tonic-gate 			buf_len = i;
197*0Sstevel@tonic-gate 	if (x->pub_key)
198*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->pub_key)))
199*0Sstevel@tonic-gate 			buf_len = i;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 	m=(unsigned char *)OPENSSL_malloc(buf_len+10);
202*0Sstevel@tonic-gate 	if (m == NULL)
203*0Sstevel@tonic-gate 		{
204*0Sstevel@tonic-gate 		DSAerr(DSA_F_DSA_PRINT,ERR_R_MALLOC_FAILURE);
205*0Sstevel@tonic-gate 		goto err;
206*0Sstevel@tonic-gate 		}
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 	if (x->priv_key != NULL)
209*0Sstevel@tonic-gate 		{
210*0Sstevel@tonic-gate 		if(!BIO_indent(bp,off,128))
211*0Sstevel@tonic-gate 		   goto err;
212*0Sstevel@tonic-gate 		if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->p))
213*0Sstevel@tonic-gate 			<= 0) goto err;
214*0Sstevel@tonic-gate 		}
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 	if ((x->priv_key != NULL) && !print(bp,"priv:",x->priv_key,m,off))
217*0Sstevel@tonic-gate 		goto err;
218*0Sstevel@tonic-gate 	if ((x->pub_key  != NULL) && !print(bp,"pub: ",x->pub_key,m,off))
219*0Sstevel@tonic-gate 		goto err;
220*0Sstevel@tonic-gate 	if ((x->p != NULL) && !print(bp,"P:   ",x->p,m,off)) goto err;
221*0Sstevel@tonic-gate 	if ((x->q != NULL) && !print(bp,"Q:   ",x->q,m,off)) goto err;
222*0Sstevel@tonic-gate 	if ((x->g != NULL) && !print(bp,"G:   ",x->g,m,off)) goto err;
223*0Sstevel@tonic-gate 	ret=1;
224*0Sstevel@tonic-gate err:
225*0Sstevel@tonic-gate 	if (m != NULL) OPENSSL_free(m);
226*0Sstevel@tonic-gate 	return(ret);
227*0Sstevel@tonic-gate 	}
228*0Sstevel@tonic-gate #endif /* !OPENSSL_NO_DSA */
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate static int print(BIO *bp, const char *number, BIGNUM *num, unsigned char *buf,
231*0Sstevel@tonic-gate 	     int off)
232*0Sstevel@tonic-gate 	{
233*0Sstevel@tonic-gate 	int n,i;
234*0Sstevel@tonic-gate 	const char *neg;
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 	if (num == NULL) return(1);
237*0Sstevel@tonic-gate 	neg=(num->neg)?"-":"";
238*0Sstevel@tonic-gate 	if(!BIO_indent(bp,off,128))
239*0Sstevel@tonic-gate 		return 0;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 	if (BN_num_bytes(num) <= BN_BYTES)
242*0Sstevel@tonic-gate 		{
243*0Sstevel@tonic-gate 		if (BIO_printf(bp,"%s %s%lu (%s0x%lx)\n",number,neg,
244*0Sstevel@tonic-gate 			(unsigned long)num->d[0],neg,(unsigned long)num->d[0])
245*0Sstevel@tonic-gate 			<= 0) return(0);
246*0Sstevel@tonic-gate 		}
247*0Sstevel@tonic-gate 	else
248*0Sstevel@tonic-gate 		{
249*0Sstevel@tonic-gate 		buf[0]=0;
250*0Sstevel@tonic-gate 		if (BIO_printf(bp,"%s%s",number,
251*0Sstevel@tonic-gate 			(neg[0] == '-')?" (Negative)":"") <= 0)
252*0Sstevel@tonic-gate 			return(0);
253*0Sstevel@tonic-gate 		n=BN_bn2bin(num,&buf[1]);
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 		if (buf[1] & 0x80)
256*0Sstevel@tonic-gate 			n++;
257*0Sstevel@tonic-gate 		else	buf++;
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 		for (i=0; i<n; i++)
260*0Sstevel@tonic-gate 			{
261*0Sstevel@tonic-gate 			if ((i%15) == 0)
262*0Sstevel@tonic-gate 				{
263*0Sstevel@tonic-gate 				if(BIO_puts(bp,"\n") <= 0
264*0Sstevel@tonic-gate 				   || !BIO_indent(bp,off+4,128))
265*0Sstevel@tonic-gate 				    return 0;
266*0Sstevel@tonic-gate 				}
267*0Sstevel@tonic-gate 			if (BIO_printf(bp,"%02x%s",buf[i],((i+1) == n)?"":":")
268*0Sstevel@tonic-gate 				<= 0) return(0);
269*0Sstevel@tonic-gate 			}
270*0Sstevel@tonic-gate 		if (BIO_write(bp,"\n",1) <= 0) return(0);
271*0Sstevel@tonic-gate 		}
272*0Sstevel@tonic-gate 	return(1);
273*0Sstevel@tonic-gate 	}
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DH
276*0Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
277*0Sstevel@tonic-gate int DHparams_print_fp(FILE *fp, const DH *x)
278*0Sstevel@tonic-gate         {
279*0Sstevel@tonic-gate         BIO *b;
280*0Sstevel@tonic-gate         int ret;
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate         if ((b=BIO_new(BIO_s_file())) == NULL)
283*0Sstevel@tonic-gate 		{
284*0Sstevel@tonic-gate 		DHerr(DH_F_DHPARAMS_PRINT_FP,ERR_R_BUF_LIB);
285*0Sstevel@tonic-gate                 return(0);
286*0Sstevel@tonic-gate 		}
287*0Sstevel@tonic-gate         BIO_set_fp(b,fp,BIO_NOCLOSE);
288*0Sstevel@tonic-gate         ret=DHparams_print(b, x);
289*0Sstevel@tonic-gate         BIO_free(b);
290*0Sstevel@tonic-gate         return(ret);
291*0Sstevel@tonic-gate         }
292*0Sstevel@tonic-gate #endif
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate int DHparams_print(BIO *bp, const DH *x)
295*0Sstevel@tonic-gate 	{
296*0Sstevel@tonic-gate 	unsigned char *m=NULL;
297*0Sstevel@tonic-gate 	int reason=ERR_R_BUF_LIB,ret=0;
298*0Sstevel@tonic-gate 	size_t buf_len=0, i;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 	if (x->p)
301*0Sstevel@tonic-gate 		buf_len = (size_t)BN_num_bytes(x->p);
302*0Sstevel@tonic-gate 	if (x->g)
303*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
304*0Sstevel@tonic-gate 			buf_len = i;
305*0Sstevel@tonic-gate 	m=(unsigned char *)OPENSSL_malloc(buf_len+10);
306*0Sstevel@tonic-gate 	if (m == NULL)
307*0Sstevel@tonic-gate 		{
308*0Sstevel@tonic-gate 		reason=ERR_R_MALLOC_FAILURE;
309*0Sstevel@tonic-gate 		goto err;
310*0Sstevel@tonic-gate 		}
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate 	if (BIO_printf(bp,"Diffie-Hellman-Parameters: (%d bit)\n",
313*0Sstevel@tonic-gate 		BN_num_bits(x->p)) <= 0)
314*0Sstevel@tonic-gate 		goto err;
315*0Sstevel@tonic-gate 	if (!print(bp,"prime:",x->p,m,4)) goto err;
316*0Sstevel@tonic-gate 	if (!print(bp,"generator:",x->g,m,4)) goto err;
317*0Sstevel@tonic-gate 	if (x->length != 0)
318*0Sstevel@tonic-gate 		{
319*0Sstevel@tonic-gate 		if (BIO_printf(bp,"    recommended-private-length: %d bits\n",
320*0Sstevel@tonic-gate 			(int)x->length) <= 0) goto err;
321*0Sstevel@tonic-gate 		}
322*0Sstevel@tonic-gate 	ret=1;
323*0Sstevel@tonic-gate 	if (0)
324*0Sstevel@tonic-gate 		{
325*0Sstevel@tonic-gate err:
326*0Sstevel@tonic-gate 		DHerr(DH_F_DHPARAMS_PRINT,reason);
327*0Sstevel@tonic-gate 		}
328*0Sstevel@tonic-gate 	if (m != NULL) OPENSSL_free(m);
329*0Sstevel@tonic-gate 	return(ret);
330*0Sstevel@tonic-gate 	}
331*0Sstevel@tonic-gate #endif
332*0Sstevel@tonic-gate 
333*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA
334*0Sstevel@tonic-gate #ifndef OPENSSL_NO_FP_API
335*0Sstevel@tonic-gate int DSAparams_print_fp(FILE *fp, const DSA *x)
336*0Sstevel@tonic-gate         {
337*0Sstevel@tonic-gate         BIO *b;
338*0Sstevel@tonic-gate         int ret;
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate         if ((b=BIO_new(BIO_s_file())) == NULL)
341*0Sstevel@tonic-gate 		{
342*0Sstevel@tonic-gate 		DSAerr(DSA_F_DSAPARAMS_PRINT_FP,ERR_R_BUF_LIB);
343*0Sstevel@tonic-gate                 return(0);
344*0Sstevel@tonic-gate 		}
345*0Sstevel@tonic-gate         BIO_set_fp(b,fp,BIO_NOCLOSE);
346*0Sstevel@tonic-gate         ret=DSAparams_print(b, x);
347*0Sstevel@tonic-gate         BIO_free(b);
348*0Sstevel@tonic-gate         return(ret);
349*0Sstevel@tonic-gate         }
350*0Sstevel@tonic-gate #endif
351*0Sstevel@tonic-gate 
352*0Sstevel@tonic-gate int DSAparams_print(BIO *bp, const DSA *x)
353*0Sstevel@tonic-gate 	{
354*0Sstevel@tonic-gate 	unsigned char *m=NULL;
355*0Sstevel@tonic-gate 	int reason=ERR_R_BUF_LIB,ret=0;
356*0Sstevel@tonic-gate 	size_t buf_len=0,i;
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 	if (x->p)
359*0Sstevel@tonic-gate 		buf_len = (size_t)BN_num_bytes(x->p);
360*0Sstevel@tonic-gate 	if (x->q)
361*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
362*0Sstevel@tonic-gate 			buf_len = i;
363*0Sstevel@tonic-gate 	if (x->g)
364*0Sstevel@tonic-gate 		if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
365*0Sstevel@tonic-gate 			buf_len = i;
366*0Sstevel@tonic-gate 	m=(unsigned char *)OPENSSL_malloc(buf_len+10);
367*0Sstevel@tonic-gate 	if (m == NULL)
368*0Sstevel@tonic-gate 		{
369*0Sstevel@tonic-gate 		reason=ERR_R_MALLOC_FAILURE;
370*0Sstevel@tonic-gate 		goto err;
371*0Sstevel@tonic-gate 		}
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate 	if (BIO_printf(bp,"DSA-Parameters: (%d bit)\n",
374*0Sstevel@tonic-gate 		BN_num_bits(x->p)) <= 0)
375*0Sstevel@tonic-gate 		goto err;
376*0Sstevel@tonic-gate 	if (!print(bp,"p:",x->p,m,4)) goto err;
377*0Sstevel@tonic-gate 	if (!print(bp,"q:",x->q,m,4)) goto err;
378*0Sstevel@tonic-gate 	if (!print(bp,"g:",x->g,m,4)) goto err;
379*0Sstevel@tonic-gate 	ret=1;
380*0Sstevel@tonic-gate err:
381*0Sstevel@tonic-gate 	if (m != NULL) OPENSSL_free(m);
382*0Sstevel@tonic-gate 	DSAerr(DSA_F_DSAPARAMS_PRINT,reason);
383*0Sstevel@tonic-gate 	return(ret);
384*0Sstevel@tonic-gate 	}
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate #endif /* !OPENSSL_NO_DSA */
387*0Sstevel@tonic-gate 
388