Add ECDH support.
[openssl.git] / crypto / asn1 / t_pkey.c
1 /* crypto/asn1/t_pkey.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 /* ====================================================================
59  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
60  * Binary polynomial ECC support in OpenSSL originally developed by 
61  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
62  */
63
64 #include <stdio.h>
65 #include "cryptlib.h"
66 #include <openssl/objects.h>
67 #include <openssl/buffer.h>
68 #include <openssl/bn.h>
69 #ifndef OPENSSL_NO_RSA
70 #include <openssl/rsa.h>
71 #endif
72 #ifndef OPENSSL_NO_DH
73 #include <openssl/dh.h>
74 #endif
75 #ifndef OPENSSL_NO_DSA
76 #include <openssl/dsa.h>
77 #endif
78 #ifndef OPENSSL_NO_EC
79 #include <openssl/ec.h>
80 #endif
81
82 static int print(BIO *fp,const char *str,BIGNUM *num,
83                 unsigned char *buf,int off);
84 #ifndef OPENSSL_NO_RSA
85 #ifndef OPENSSL_NO_FP_API
86 int RSA_print_fp(FILE *fp, const RSA *x, int off)
87         {
88         BIO *b;
89         int ret;
90
91         if ((b=BIO_new(BIO_s_file())) == NULL)
92                 {
93                 RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB);
94                 return(0);
95                 }
96         BIO_set_fp(b,fp,BIO_NOCLOSE);
97         ret=RSA_print(b,x,off);
98         BIO_free(b);
99         return(ret);
100         }
101 #endif
102
103 int RSA_print(BIO *bp, const RSA *x, int off)
104         {
105         char str[128];
106         const char *s;
107         unsigned char *m=NULL;
108         int ret=0;
109         size_t buf_len=0, i;
110
111         if (x->n)
112                 buf_len = (size_t)BN_num_bytes(x->n);
113         if (x->e)
114                 if (buf_len < (i = (size_t)BN_num_bytes(x->e)))
115                         buf_len = i;
116         if (x->d)
117                 if (buf_len < (i = (size_t)BN_num_bytes(x->d)))
118                         buf_len = i;
119         if (x->p)
120                 if (buf_len < (i = (size_t)BN_num_bytes(x->p)))
121                         buf_len = i;
122         if (x->q)
123                 if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
124                         buf_len = i;
125         if (x->dmp1)
126                 if (buf_len < (i = (size_t)BN_num_bytes(x->dmp1)))
127                         buf_len = i;
128         if (x->dmq1)
129                 if (buf_len < (i = (size_t)BN_num_bytes(x->dmq1)))
130                         buf_len = i;
131         if (x->iqmp)
132                 if (buf_len < (i = (size_t)BN_num_bytes(x->iqmp)))
133                         buf_len = i;
134
135         m=(unsigned char *)OPENSSL_malloc(buf_len+10);
136         if (m == NULL)
137                 {
138                 RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE);
139                 goto err;
140                 }
141
142         if (off)
143                 {
144                 if (off > 128) off=128;
145                 memset(str,' ',off);
146                 }
147         if (x->d != NULL)
148                 {
149                 if (off && (BIO_write(bp,str,off) <= 0)) goto err;
150                 if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->n))
151                         <= 0) goto err;
152                 }
153
154         if (x->d == NULL)
155                 sprintf(str,"Modulus (%d bit):",BN_num_bits(x->n));
156         else
157                 strcpy(str,"modulus:");
158         if (!print(bp,str,x->n,m,off)) goto err;
159         s=(x->d == NULL)?"Exponent:":"publicExponent:";
160         if (!print(bp,s,x->e,m,off)) goto err;
161         if (!print(bp,"privateExponent:",x->d,m,off)) goto err;
162         if (!print(bp,"prime1:",x->p,m,off)) goto err;
163         if (!print(bp,"prime2:",x->q,m,off)) goto err;
164         if (!print(bp,"exponent1:",x->dmp1,m,off)) goto err;
165         if (!print(bp,"exponent2:",x->dmq1,m,off)) goto err;
166         if (!print(bp,"coefficient:",x->iqmp,m,off)) goto err;
167         ret=1;
168 err:
169         if (m != NULL) OPENSSL_free(m);
170         return(ret);
171         }
172 #endif /* OPENSSL_NO_RSA */
173
174 #ifndef OPENSSL_NO_DSA
175 #ifndef OPENSSL_NO_FP_API
176 int DSA_print_fp(FILE *fp, const DSA *x, int off)
177         {
178         BIO *b;
179         int ret;
180
181         if ((b=BIO_new(BIO_s_file())) == NULL)
182                 {
183                 DSAerr(DSA_F_DSA_PRINT_FP,ERR_R_BUF_LIB);
184                 return(0);
185                 }
186         BIO_set_fp(b,fp,BIO_NOCLOSE);
187         ret=DSA_print(b,x,off);
188         BIO_free(b);
189         return(ret);
190         }
191 #endif
192
193 int DSA_print(BIO *bp, const DSA *x, int off)
194         {
195         char str[128];
196         unsigned char *m=NULL;
197         int ret=0;
198         size_t buf_len=0,i;
199
200         if (x->p)
201                 buf_len = (size_t)BN_num_bytes(x->p);
202         if (x->q)
203                 if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
204                         buf_len = i;
205         if (x->g)
206                 if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
207                         buf_len = i;
208         if (x->priv_key)
209                 if (buf_len < (i = (size_t)BN_num_bytes(x->priv_key)))
210                         buf_len = i;
211         if (x->pub_key)
212                 if (buf_len < (i = (size_t)BN_num_bytes(x->pub_key)))
213                         buf_len = i;
214
215         m=(unsigned char *)OPENSSL_malloc(buf_len+10);
216         if (m == NULL)
217                 {
218                 DSAerr(DSA_F_DSA_PRINT,ERR_R_MALLOC_FAILURE);
219                 goto err;
220                 }
221
222         if (off)
223                 {
224                 if (off > 128) off=128;
225                 memset(str,' ',off);
226                 }
227         if (x->priv_key != NULL)
228                 {
229                 if (off && (BIO_write(bp,str,off) <= 0)) goto err;
230                 if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->p))
231                         <= 0) goto err;
232                 }
233
234         if ((x->priv_key != NULL) && !print(bp,"priv:",x->priv_key,m,off))
235                 goto err;
236         if ((x->pub_key  != NULL) && !print(bp,"pub: ",x->pub_key,m,off))
237                 goto err;
238         if ((x->p != NULL) && !print(bp,"P:   ",x->p,m,off)) goto err;
239         if ((x->q != NULL) && !print(bp,"Q:   ",x->q,m,off)) goto err;
240         if ((x->g != NULL) && !print(bp,"G:   ",x->g,m,off)) goto err;
241         ret=1;
242 err:
243         if (m != NULL) OPENSSL_free(m);
244         return(ret);
245         }
246 #endif /* !OPENSSL_NO_DSA */
247
248 #ifndef OPENSSL_NO_EC
249 #ifndef OPENSSL_NO_FP_API
250 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
251         {
252         BIO *b;
253         int ret;
254
255         if ((b=BIO_new(BIO_s_file())) == NULL)
256                 {
257                 ECerr(EC_F_ECPKPARAMETERS_PRINT_FP,ERR_R_BUF_LIB);
258                 return(0);
259                 }
260         BIO_set_fp(b, fp, BIO_NOCLOSE);
261         ret = ECPKParameters_print(b, x, off);
262         BIO_free(b);
263         return(ret);
264         }
265
266 int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
267         {
268         BIO *b;
269         int ret;
270  
271         if ((b=BIO_new(BIO_s_file())) == NULL)
272                 {
273                 ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
274                 return(0);
275                 }
276         BIO_set_fp(b, fp, BIO_NOCLOSE);
277         ret = EC_KEY_print(b, x, off);
278         BIO_free(b);
279         return(ret);
280         }
281 #endif
282
283 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
284         {
285         char str[128];
286         unsigned char *buffer=NULL;
287         size_t  buf_len=0, i;
288         int     ret=0, reason=ERR_R_BIO_LIB;
289         BN_CTX  *ctx=NULL;
290         EC_POINT *point=NULL;
291         BIGNUM  *p=NULL, *a=NULL, *b=NULL, *gen=NULL,
292                 *order=NULL, *cofactor=NULL, *seed=NULL;
293         
294         static const char *gen_compressed = "Generator (compressed):";
295         static const char *gen_uncompressed = "Generator (uncompressed):";
296         static const char *gen_hybrid = "Generator (hybrid):";
297  
298         if (!x)
299                 {
300                 reason = ERR_R_PASSED_NULL_PARAMETER;
301                 goto err;
302                 }
303
304         if (EC_GROUP_get_asn1_flag(x))
305                 {
306                 /* the curve parameter are given by an asn1 OID */
307                 int nid;
308
309                 if (off)
310                         {
311                         if (off > 128)
312                                 off=128;
313                         memset(str, ' ', off);
314                         if (BIO_write(bp, str, off) <= 0)
315                                 goto err;
316                         }
317
318                 nid = EC_GROUP_get_nid(x);
319                 if (nid == 0)
320                         goto err;
321
322                 if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
323                         goto err;
324                 if (BIO_printf(bp, "\n") <= 0)
325                         goto err;
326                 }
327         else
328                 {
329                 /* explicit parameters */
330                 /* TODO */
331                 point_conversion_form_t form;
332
333                 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
334                         (b = BN_new()) == NULL || (order = BN_new()) == NULL ||
335                         (cofactor = BN_new()) == NULL)
336                         {
337                         reason = ERR_R_MALLOC_FAILURE;
338                         goto err;
339                         }
340
341                 if (EC_METHOD_get_field_type(EC_GROUP_method_of(x)) == NID_X9_62_prime_field) 
342                         {
343                         if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx))
344                                 {
345                                 reason = ERR_R_EC_LIB;
346                                 goto err;
347                                 }
348                         }
349                 else
350                         {
351                         if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx))
352                                 {
353                                 reason = ERR_R_EC_LIB;
354                                 goto err;
355                                 }
356                         }
357
358                 if ((point = EC_GROUP_get0_generator(x)) == NULL)
359                         {
360                         reason = ERR_R_EC_LIB;
361                         goto err;
362                         }
363                 if (!EC_GROUP_get_order(x, order, NULL) || 
364                         !EC_GROUP_get_cofactor(x, cofactor, NULL))
365                         {
366                         reason = ERR_R_EC_LIB;
367                         goto err;
368                         }
369                 
370                 form = EC_GROUP_get_point_conversion_form(x);
371
372                 if ((gen = EC_POINT_point2bn(x, point, 
373                                 form, NULL, ctx)) == NULL)
374                         {
375                         reason = ERR_R_EC_LIB;
376                         goto err;
377                         }
378
379                 buf_len = (size_t)BN_num_bytes(p);
380                 if (buf_len < (i = (size_t)BN_num_bytes(a)))
381                         buf_len = i;
382                 if (buf_len < (i = (size_t)BN_num_bytes(b)))
383                         buf_len = i;
384                 if (buf_len < (i = (size_t)BN_num_bytes(gen)))
385                         buf_len = i;
386                 if (buf_len < (i = (size_t)BN_num_bytes(order)))
387                         buf_len = i;
388                 if (buf_len < (i = (size_t)BN_num_bytes(cofactor))) 
389                         buf_len = i;
390
391                 if (EC_GROUP_get0_seed(x))
392                         {
393                         seed = BN_bin2bn(EC_GROUP_get0_seed(x),
394                                 EC_GROUP_get_seed_len(x), NULL);
395                         if (seed == NULL)
396                                 {
397                                 reason = ERR_R_BN_LIB;
398                                 goto err;
399                                 }
400                         if (buf_len < (i = (size_t)BN_num_bytes(seed))) 
401                                 buf_len = i;
402                         }
403
404                 buf_len += 10;
405                 if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
406                         {
407                         reason = ERR_R_MALLOC_FAILURE;
408                         goto err;
409                         }
410                 if (off)
411                         {
412                         if (off > 128) off=128;
413                         memset(str,' ',off);
414                         }
415   
416                 if ((p != NULL) && !print(bp, "P:   ", p, buffer, off)) 
417                         goto err;
418                 if ((a != NULL) && !print(bp, "A:   ", a, buffer, off)) 
419                         goto err;
420                 if ((b != NULL) && !print(bp, "B:   ", b, buffer, off))
421                         goto err;
422                 if (form == POINT_CONVERSION_COMPRESSED)
423                         {
424                         if ((gen != NULL) && !print(bp, gen_compressed, gen,
425                                 buffer, off))
426                                 goto err;
427                         }
428                 else if (form == POINT_CONVERSION_UNCOMPRESSED)
429                         {
430                         if ((gen != NULL) && !print(bp, gen_uncompressed, gen,
431                                 buffer, off))
432                                 goto err;
433                         }
434                 else /* form == POINT_CONVERSION_HYBRID */
435                         {
436                         if ((gen != NULL) && !print(bp, gen_hybrid, gen,
437                                 buffer, off))
438                                 goto err;
439                         }
440                 if ((order != NULL) && !print(bp, "Order: ", order, 
441                         buffer, off)) goto err;
442                 if ((cofactor != NULL) && !print(bp, "Cofactor: ", cofactor, 
443                         buffer, off)) goto err;
444                 if ((seed != NULL) && !print(bp, "Seed:", seed, 
445                         buffer, off)) goto err;
446                 }
447         ret=1;
448 err:
449         if (!ret)
450                 ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
451         if (p) 
452                 BN_free(p);
453         if (a) 
454                 BN_free(a);
455         if (b)
456                 BN_free(b);
457         if (gen)
458                 BN_free(gen);
459         if (order)
460                 BN_free(order);
461         if (cofactor)
462                 BN_free(cofactor);
463         if (seed) 
464                 BN_free(seed);
465         if (ctx)
466                 BN_CTX_free(ctx);
467         if (buffer != NULL) 
468                 OPENSSL_free(buffer);
469         return(ret);    
470         }
471
472 int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
473         {
474         char str[128];
475         unsigned char *buffer=NULL;
476         size_t  buf_len=0, i;
477         int     ret=0, reason=ERR_R_BIO_LIB;
478         BIGNUM  *pub_key=NULL;
479         BN_CTX  *ctx=NULL;
480  
481         if (!x || !x->group)
482                 {
483                 reason = ERR_R_PASSED_NULL_PARAMETER;
484                 goto err;
485                 }
486
487         if ((pub_key = EC_POINT_point2bn(x->group, x->pub_key,
488                 x->conv_form, NULL, ctx)) == NULL)
489                 {
490                 reason = ERR_R_EC_LIB;
491                 goto err;
492                 }
493
494         buf_len = (size_t)BN_num_bytes(pub_key);
495         if (x->priv_key)
496                 {
497                 if ((i = (size_t)BN_num_bytes(x->priv_key)) > buf_len)
498                         buf_len = i;
499                 }
500
501         buf_len += 10;
502         if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
503                 {
504                 reason = ERR_R_MALLOC_FAILURE;
505                 goto err;
506                 }
507         if (off)
508                 {
509                 if (off > 128) off=128;
510                 memset(str,' ',off);
511                 }
512         if (x->priv_key != NULL)
513                 {
514                 if (off && (BIO_write(bp, str, off) <= 0)) goto err;
515                 if (BIO_printf(bp, "Private-Key: (%d bit)\n", 
516                         BN_num_bits(x->priv_key)) <= 0) goto err;
517                 }
518   
519         if ((x->priv_key != NULL) && !print(bp, "priv:", x->priv_key, 
520                 buffer, off))
521                 goto err;
522         if ((pub_key != NULL) && !print(bp, "pub: ", pub_key,
523                 buffer, off))
524                 goto err;
525         if (!ECPKParameters_print(bp, x->group, off))
526                 goto err;
527         ret=1;
528 err:
529         if (!ret)
530                 ECerr(EC_F_EC_KEY_PRINT, reason);
531         if (pub_key) 
532                 BN_free(pub_key);
533         if (ctx)
534                 BN_CTX_free(ctx);
535         if (buffer != NULL)
536                 OPENSSL_free(buffer);
537         return(ret);
538         }
539 #endif /* OPENSSL_NO_EC */
540
541 static int print(BIO *bp, const char *number, BIGNUM *num, unsigned char *buf,
542              int off)
543         {
544         int n,i;
545         char str[128];
546         const char *neg;
547
548         if (num == NULL) return(1);
549         neg=(num->neg)?"-":"";
550         if (off)
551                 {
552                 if (off > 128) off=128;
553                 memset(str,' ',off);
554                 if (BIO_write(bp,str,off) <= 0) return(0);
555                 }
556
557         if (BN_num_bytes(num) <= BN_BYTES)
558                 {
559                 if (BIO_printf(bp,"%s %s%lu (%s0x%lx)\n",number,neg,
560                         (unsigned long)num->d[0],neg,(unsigned long)num->d[0])
561                         <= 0) return(0);
562                 }
563         else
564                 {
565                 buf[0]=0;
566                 if (BIO_printf(bp,"%s%s",number,
567                         (neg[0] == '-')?" (Negative)":"") <= 0)
568                         return(0);
569                 n=BN_bn2bin(num,&buf[1]);
570         
571                 if (buf[1] & 0x80)
572                         n++;
573                 else    buf++;
574
575                 for (i=0; i<n; i++)
576                         {
577                         if ((i%15) == 0)
578                                 {
579                                 str[0]='\n';
580                                 memset(&(str[1]),' ',off+4);
581                                 if (BIO_write(bp,str,off+1+4) <= 0) return(0);
582                                 }
583                         if (BIO_printf(bp,"%02x%s",buf[i],((i+1) == n)?"":":")
584                                 <= 0) return(0);
585                         }
586                 if (BIO_write(bp,"\n",1) <= 0) return(0);
587                 }
588         return(1);
589         }
590
591 #ifndef OPENSSL_NO_DH
592 #ifndef OPENSSL_NO_FP_API
593 int DHparams_print_fp(FILE *fp, const DH *x)
594         {
595         BIO *b;
596         int ret;
597
598         if ((b=BIO_new(BIO_s_file())) == NULL)
599                 {
600                 DHerr(DH_F_DHPARAMS_PRINT_FP,ERR_R_BUF_LIB);
601                 return(0);
602                 }
603         BIO_set_fp(b,fp,BIO_NOCLOSE);
604         ret=DHparams_print(b, x);
605         BIO_free(b);
606         return(ret);
607         }
608 #endif
609
610 int DHparams_print(BIO *bp, const DH *x)
611         {
612         unsigned char *m=NULL;
613         int reason=ERR_R_BUF_LIB,ret=0;
614         size_t buf_len=0, i;
615
616         if (x->p)
617                 buf_len = (size_t)BN_num_bytes(x->p);
618         if (x->g)
619                 if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
620                         buf_len = i;
621         m=(unsigned char *)OPENSSL_malloc(buf_len+10);
622         if (m == NULL)
623                 {
624                 reason=ERR_R_MALLOC_FAILURE;
625                 goto err;
626                 }
627
628         if (BIO_printf(bp,"Diffie-Hellman-Parameters: (%d bit)\n",
629                 BN_num_bits(x->p)) <= 0)
630                 goto err;
631         if (!print(bp,"prime:",x->p,m,4)) goto err;
632         if (!print(bp,"generator:",x->g,m,4)) goto err;
633         if (x->length != 0)
634                 {
635                 if (BIO_printf(bp,"    recommended-private-length: %d bits\n",
636                         (int)x->length) <= 0) goto err;
637                 }
638         ret=1;
639         if (0)
640                 {
641 err:
642                 DHerr(DH_F_DHPARAMS_PRINT,reason);
643                 }
644         if (m != NULL) OPENSSL_free(m);
645         return(ret);
646         }
647 #endif
648
649 #ifndef OPENSSL_NO_DSA
650 #ifndef OPENSSL_NO_FP_API
651 int DSAparams_print_fp(FILE *fp, const DSA *x)
652         {
653         BIO *b;
654         int ret;
655
656         if ((b=BIO_new(BIO_s_file())) == NULL)
657                 {
658                 DSAerr(DSA_F_DSAPARAMS_PRINT_FP,ERR_R_BUF_LIB);
659                 return(0);
660                 }
661         BIO_set_fp(b,fp,BIO_NOCLOSE);
662         ret=DSAparams_print(b, x);
663         BIO_free(b);
664         return(ret);
665         }
666 #endif
667
668 int DSAparams_print(BIO *bp, const DSA *x)
669         {
670         unsigned char *m=NULL;
671         int reason=ERR_R_BUF_LIB,ret=0;
672         size_t buf_len=0,i;
673
674         if (x->p)
675                 buf_len = (size_t)BN_num_bytes(x->p);
676         if (x->q)
677                 if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
678                         buf_len = i;
679         if (x->g)
680                 if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
681                         buf_len = i;
682         m=(unsigned char *)OPENSSL_malloc(buf_len+10);
683         if (m == NULL)
684                 {
685                 reason=ERR_R_MALLOC_FAILURE;
686                 goto err;
687                 }
688
689         if (BIO_printf(bp,"DSA-Parameters: (%d bit)\n",
690                 BN_num_bits(x->p)) <= 0)
691                 goto err;
692         if (!print(bp,"p:",x->p,m,4)) goto err;
693         if (!print(bp,"q:",x->q,m,4)) goto err;
694         if (!print(bp,"g:",x->g,m,4)) goto err;
695         ret=1;
696 err:
697         if (m != NULL) OPENSSL_free(m);
698         DSAerr(DSA_F_DSAPARAMS_PRINT,reason);
699         return(ret);
700         }
701
702 #endif /* !OPENSSL_NO_DSA */
703
704 #ifndef OPENSSL_NO_EC
705 #ifndef OPENSSL_NO_FP_API
706 int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
707         {
708         BIO *b;
709         int ret;
710  
711         if ((b=BIO_new(BIO_s_file())) == NULL)
712                 {
713                 ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
714                 return(0);
715                 }
716         BIO_set_fp(b, fp, BIO_NOCLOSE);
717         ret = ECParameters_print(b, x);
718         BIO_free(b);
719         return(ret);
720         }
721 #endif
722
723 int ECParameters_print(BIO *bp, const EC_KEY *x)
724         {
725         int     reason=ERR_R_EC_LIB, ret=0;
726         BIGNUM  *order=NULL;
727  
728         if (!x || !x->group)
729                 {
730                 reason = ERR_R_PASSED_NULL_PARAMETER;;
731                 goto err;
732                 }
733
734         if ((order = BN_new()) == NULL)
735                 {
736                 reason = ERR_R_MALLOC_FAILURE;
737                 goto err;
738                 }
739
740         if (!EC_GROUP_get_order(x->group, order, NULL))
741                 {
742                 reason = ERR_R_EC_LIB;
743                 goto err;
744                 }
745  
746         if (BIO_printf(bp, "ECDSA-Parameters: (%d bit)\n", 
747                 BN_num_bits(order)) <= 0)
748                 goto err;
749         if (!ECPKParameters_print(bp, x->group, 4))
750                 goto err;
751         ret=1;
752 err:
753         if (order)
754                 BN_free(order);
755         ECerr(EC_F_ECPARAMETERS_PRINT, reason);
756         return(ret);
757         }
758   
759 #endif